文件。我需要从ini文件读取一些配置。下面的代码不起作用。有人可以帮我修复它。GetPrivateProfileStringA正在返回“系统找不到指定的文件。”尽管CPP Visual studio 2010中存在
char * path = "C:\\NotBackedUp\\Workspaces\\LDAP-DLL\\LDAPTestApp\\bin\\Debug\\conf\\ldap.ini";
std::wcout << "path: " << path << std::endl;
if(!ATLPath::FileExists(path))
{
HRESULT hr = ATL::AtlHresultFromLastError();
ATLTRACE("%x\n",hr);//system could not find the file specified!
std::cout << "File not found " << std::endl;
return 0;
}
else
{
std::cout << "File found " << std::endl;
}
char valueRead[320];
int a = GetPrivateProfileStringA("ldap", "url", "error", valueRead, 320, path);
std::cout << "Value Read " << valueRead << std::endl;
std::cout << "Error String " << GetLastErrorAsString();
上面的代码生成下面的日志,你可以看到ATLPath :: FILEEXISTS时返回true,但仍是GetLastError函数说明系统找不到指定的文件
path: C:\NotBackedUp\Workspaces\LDAP-DLL\LDAPTestApp\bin\Debug\conf\ldap.ini
File found
Value Read error
Error String The system cannot find the file specified.
我ldap.ini文件具有下面的行,并提供在上述路径
[ldap]
url=ldap://testserver
任何帮助高度赞赏
谢谢
['GetPrivateProfileString'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v = vs.85).aspx)仅适用于16位兼容性,应该避免,如果您需要所有用户共享配置,请考虑使用注册表或配置文件。 – Mgetz
正如在[文档](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v = vs.85).aspx)中所述:'注意此功能仅用于兼容性与16位基于Windows的应用程序。应用程序应该在注册表中存储初始化信息。也许你应该将你的信息存储在ini文件中,而不是注册表中。或者编写你自己的ini文件解析器,因为这个WinAPI函数已经被弃用了。 –
函数成功(从返回有效字符串的意义上说)。成功后调用'GetLastError'将返回垃圾。即使它失败了,你也调用'GetLastError'太迟了。你在调用'GetPrivateProfileStringA'和调用'GetLastError'之间做了很多其他的事情,其中任何一个都可能重置'GetLastError'。 –