积极答复者
Win7下CreateFile失败 Show error message of GetLastError系统找不到指定的文件

问题
-
已关闭WIN7的UAC控制,操作系统为Win7-64位旗舰版 编译环境为 VS2013 Express
#include <Windows.h> #include <stdio.h> #define BUF_SIZE 256 //Show error message of GetLastError() void ShowSystemError(); int main(int argc, LPTSTR argv[]) { HANDLE hIn, hOut; DWORD nIn, nOut; CHAR buffer[BUF_SIZE]; if (argc != 3) { printf("Usage: cpw file1 file2\n"); return 1; }
printf("%s\n", argv[1]);
printf("%s\n", argv[2]);hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIn == INVALID_HANDLE_VALUE) { printf("Cannot open input file. Error: %x\n", GetLastError()); ShowSystemError(); return 2; } ....
D:\>dir *.txt
驱动器 D 中的卷是 工作
卷的序列号是 4899-7761
D:\ 的目录
2014/02/09 22:45 18 A.txt.txt
1 个文件 18 字节
0 个目录 251,909,156,864 可用字节
D:\>cpw A.txt B.txt
A.txt
B.txt
Cannot open input file. Error: 2void ShowSystemError()函数弹出对话框 系统找不到指定的文件
同样代码在32位xp下VS2010 能够正常运行
试过各种办法。。。百思不得其解
- 已编辑 ByLee_SEU 2014年2月9日 14:54
答案
全部回复
-
你好,
在Windows Server 2003 和 Windows XP中可以使用CreateFile 函数直接对磁盘和卷进行访问。
但是, 在 Windows Vista 和 Windows Server 2008 中对文件系统和存储堆栈进行的更改可限制对磁盘和卷的直接访问 .
更多信息请打开上面的链接查看。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
改为
hIn = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
或者
hIn =CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
麻烦把正确答案设为解答。