CreateFile打开设备I/O问题

nanluming7891 2017-10-23 05:10:39
我在VS2010中用MFC写一个硬盘读写测试遇到一个问题
如下
HANDLE hFILE = (_T("\\.\C:"),GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

上边这一段在以只写的方式打开设备程序中编译运行是没有问题的。
假如我获取到的盘符为D:我需要传给CreateFile第一个参数一个变量(就是我另一个控件获取到盘符的变量)
不管我怎么写一直在报错
比如我如下这么写

CString n = disk[drive];
HANDLE hFILE = (_T("\\.\n:"),GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

disk[]是存放盘符的数组 dirve下标。如上这么写就会报错
各位帮帮忙看一下 谢谢
...全文
646 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
red-fly 2017-10-24
  • 打赏
  • 举报
回复
把 _T("\\.\n:") 的结果打印出来,看下是否正常,如果这就是你的实际写法,那肯定是不对的, TCHAR part[16 = {0}; _stprintf(part, _T("\\\\.\\%c:"), n);
赵4老师 2017-10-24
  • 打赏
  • 举报
回复
C++ Character Constants Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes. Microsoft Specific For Microsoft C++, the source and execution character sets are both ASCII. END Microsoft Specific There are three kinds of character constants: Normal character constants Multicharacter constants Wide-character constants Note Use wide-character constants in place of multicharacter constants to ensure portability. Character constants are specified as one or more characters enclosed in single quotation marks. For example: char ch = 'x'; // Specify normal character constant. int mbch = 'ab'; // Specify system-dependent // multicharacter constant. wchar_t wcch = L'ab'; // Specify wide-character constant. Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message. Syntax character-constant : 'c-char-sequence' L'c-char-sequence' c-char-sequence : c-char c-char-sequence c-char c-char : any member of the source character set except the single quotation mark ('), backslash (\), or newline character escape-sequence escape-sequence : simple-escape-sequence octal-escape-sequence hexadecimal-escape-sequence simple-escape-sequence : one of \' \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence : \octal-digit \octal-digit octal-digit \octal-digit octal-digit octal-digit hexadecimal-escape-sequence : \xhexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.) The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example: char schar = 'x'; // Normal character constant wchar_t wchar = L'\x81\x19'; // Wide-character constant Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences. Table 1.2 C++ Reserved or Nongraphic Characters Character ASCII Representation ASCII Value Escape Sequence Newline NL (LF) 10 or 0x0a \n Horizontal tab HT 9 \t Vertical tab VT 11 or 0x0b \v Backspace BS 8 \b Carriage return CR 13 or 0x0d \r Formfeed FF 12 or 0x0c \f Alert BEL 7 \a Backslash \ 92 or 0x5c \\ Question mark ? 63 or 0x3f \? Single quotation mark ' 39 or 0x27 \' Double quotation mark " 34 or 0x22 \" Octal number ooo — \ooo Hexadecimal number hhh — \xhhh Null character NUL 0 \0 If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued. Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence. Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example: wchar_t och = L'\076a'; // Sequence terminates at a char ch = '\233'; // Sequence terminates after 3 characters Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit. Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)
zgl7903 2017-10-24
  • 打赏
  • 举报
回复
本来的格式是要求 "\\.\C:" 格式化时 _T("\\\\.\\C:")
Eleven 2017-10-23
  • 打赏
  • 举报
回复
格式化一下就好了~~
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
要改为 disk.Addstring("\\\\.\\A:"); 吧?
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
可以 但 disk.Addstring("A"); 要改为 disk.Addstring("\\.\\A:"); ......
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
我明白了,太傻了 CString *n= & disk[drive]; so 不就可以了嘛 对不对
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
我只需要知道怎么把 disk数组中drive这个内容(盘符)赋给变量,并且在CreateFile中有效就可以了
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
版主大大求解释
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
CStringArray disk; disk.Addstring("A"); disk.Addstring("B"); disk.Addstring("C"); disk.Addstring("D"); CString n; int drive=3; n = "\\.\\" + disk[drive]; // D n +=":";
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 8 楼 nanluming7891 的回复:
[quote=引用 7 楼 schlafenhamster 的回复:] CString disk="D"; CString n; n = "\\.\\" + disk;//[drive]; n +=":";
如果我获取到的盘符不是D怎么办,A B C 呢 [/quote] 可以帮忙解释解释么
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 7 楼 schlafenhamster 的回复:
CString disk="D"; CString n; n = "\\.\\" + disk;//[drive]; n +=":";
如果我获取到的盘符不是D怎么办,A B C 呢
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
CString disk="D"; CString n; n = "\\.\\" + disk;//[drive]; n +=":";
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
应该怎么写呢 ,蟹蟹
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster 的回复:
("\\.\n:") 这个 n 就是 'n' 而不是变量 CString n
应该怎么写
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
比如CString n =
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
("\\.\n:") 这个 n 就是 'n' 而不是变量 CString n
oyljerry 2017-10-23
  • 打赏
  • 举报
回复
你要把变量n用CString的Format组合字符串。
nanluming7891 2017-10-23
  • 打赏
  • 举报
回复
第一个参数中(“\\.\%c”,n)报错,实参过多

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧