【发布时间】:2012-04-20 04:33:52
【问题描述】:
我有一个用于创建目录的函数。它使用 CreateDirectoryA()
CreateDirectory 报告它失败,但是当我使用 GetLastError() 检查错误代码时,它报告 ERROR_SUCCESS
代码:
BOOL isDirCreated = CreateDirectoryA(dirName.c_str(), NULL);
DWORD dw = GetLastError();
if (isDirCreated) {
if (!SetFileAttributesA(dirName.c_str(), attributes)) {
printf("SetFileAttributes() %s failed with (%d)", dirName.c_str(), GetLastError()));
return;
}
} else {
printf("CreateDirectory() %s Failed with (%d)", dirName.c_str(), dw));
if(ERROR_ALREADY_EXISTS != dw) {
return;
}
}
这会返回:(对函数的多次调用)
CreateDirectory() testDir Failed with (0)
CreateDirectory() testDir\dir Failed with (183)
即使 CreateDirectoryA 返回 false,目录也会被创建。失败总是发生在第一次调用该函数时。所有后续调用都按预期工作。
关于 CreateDirectory 在成功创建目录时返回 false 的任何想法。
这是一个类似的帖子,但该解决方案对我不起作用:
ReadFile() says it failed, but the error code is ERROR_SUCCESS
更新 事实证明,这个错误是因为代码中包含的另一个标头有一个函数“GetLastError”,另一个函数位于单独的命名空间中,因此解决方案是调用 GetLastError,如下所示。
/*
* the :: will tell it to use the GetLastError that is available on the global
* scope. Most of Microsoft's calls don't have any namespace.
*/
DWORD dw = ::GetLastError();
【问题讨论】:
-
您需要寻找某种 DLL 注入您的进程并与 winapi 混淆。病毒扫描仪之类的。您可以在 VS 输出窗口中看到加载的 DLL。
-
如果我理解您的意思,您认为某些 DLL 正在被加载并干扰我的进程,导致它在生成目录时不正确地报告成功或失败?
标签: c++ windows error-code create-directory