0

这是我的代码。

char BPP[5];
int result, err;

result = GetPrivateProfileStringA("abc", "cba", NULL, BPP, 5, "D:\\aefeaf.ini"); // result = 0
result = _get_errno(&err); // result = 0, err = 0
result = GetLastError(); // result = 0

以及来自MSDN的描述:In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.

最后一个参数是随机的,文件不存在。但是 GetLastError() 仍然返回 0。有人可以向我解释为什么它没有返回 2 吗?

编辑:正如@JochenKalmbach 建议的那样,我确保我的项目没有使用 C++/CLI。@claptrap 说 errorno 是一个错字(应该是errno),我在上面的代码中添加了 _get_errno 。但是,所有错误代码返回都是 0。非常感谢任何帮助。

4

1 回答 1

0

希望您没有使用 C++/CLI ...这会弄乱“GetLastError”的值,因为代码内部使用“IJW”(它只是工作)并执行一堆 Win32 操作...。

对于本机应用程序,这可以按预期工作:

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <crtdbg.h>

int _tmain(int argv, char *argc[]) 
{
  char szStr[5];
  int result = GetPrivateProfileStringA("abc", "cba", NULL, szStr, 5, "D:\\aefeaf.ini");
  _ASSERTE(result == 0);
  result = GetLastError();
  _ASSERTE(result == 2);
}

如果您使用的是 C++/CLI,那么您应该在方法周围加上

#pragma managed(push, off)
// Place the method here
#pragma managed(pop);
于 2013-07-19T08:30:19.270 回答