2017-05-31 146 views
1

文件。我需要从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 

任何帮助高度赞赏

谢谢

+0

['GetPrivateProfileString'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v = vs.85).aspx)仅适用于16位兼容性,应该避免,如果您需要所有用户共享配置,请考虑使用注册表或配置文件。 – Mgetz

+0

正如在[文档](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v = vs.85).aspx)中所述:'注意此功能仅用于兼容性与16位基于Windows的应用程序。应用程序应该在注册表中存储初始化信息。也许你应该将你的信息存储在ini文件中,而不是注册表中。或者编写你自己的ini文件解析器,因为这个WinAPI函数已经被弃用了。 –

+1

函数成功(从返回有效字符串的意义上说)。成功后调用'GetLastError'将返回垃圾。即使它失败了,你也调用'GetLastError'太迟了。你在调用'GetPrivateProfileStringA'和调用'GetLastError'之间做了很多其他的事情,其中​​任何一个都可能重置'GetLastError'。 –

回答

0

您收到的ERROR_FILE_NOT_FOUND错误代码也会在GetPrivateProfileString无法找到节或值时设置。

你的代码在你的ini文件的Win10上正常工作。

使用十六进制查看器/编辑器来验证您的ldap.ini实际上是ASCII,并且它不包含BOM

+0

仍然没有奏效。使用了一个开源库https://inireader.codeplex.com/ – Ravi

相关问题