【发布时间】:2017-08-20 17:41:10
【问题描述】:
所以我一直在尝试从我的 Windows 服务的 .ini 文件中读取和写入。我将 ini 文件临时存储在 'C:\test\CppWindowsService.ini' 中。程序可以很好地读取 ini,但 在设置 ini 文件中的值时遇到问题。它似乎更新了值,但实际上并没有做任何事情。我正在使用 github 上提供的 brofield 的 SimpleIni,我在网上看到很多人使用它来满足他们的 ini 解析需求。你可以找到它here。
到目前为止我已经尝试/检查过的事情:
- 安全设置:管理员、域管理员、系统和我都有读/写权限。这部分代码是以我的域管理员身份运行的。
非常感谢任何帮助,并在此先感谢!
bool readini()
{
// Load .ini file and retrieve ClientGuid **
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile("C:\\test\\CppWindowsService.ini");
const char * pVal = ini.GetValue("GUID", "Clientguid", NULL);
std::cout << "pVal:" << pVal << std::endl;
std::string test(pVal); // Had to convert pVal to string for unknow reason:
if (test == "noguid") // const char * "noguid" != const char[7] "noguid" ???
{ // Aren't these the exact same thing?... Compiler wasn't
// generate guid ** // throwing the error, but the if statement wouldn't pass
GUID initguid; // the condition until I did this.
HRESULT hCreateGuid = CoCreateGuid(&initguid);
// generate bstr from guid **
wchar_t* bstrGuid;
StringFromCLSID(initguid, &bstrGuid);
std::wcout << bstrGuid << std::endl;
// transform bstr to str **
std::stringstream ss;
ss << bstrGuid;
std::string guid = ss.str();
// set .ini clientguid with generated guid **
SI_Error rc = ini.SetValue("GUID", "Clientguid", guid.c_str());
if (rc < 0)
{
return false;
}
printf("key: %s\n", rc == SI_INSERTED ?
"inserted" : "updated");
::CoTaskMemFree(bstrGuid);
}
}
三个不同的 cout/wcout/print 语句的输出:
pval: noguid
{D002CD3F-6434-47E9-8246-06884FDE1FEA}
key: updated
Ini 格式:
[GUID]
Clientguid = noguid
我只包含了 SimpleIni.h 文件,因为其他文件看起来对我没有什么用处。这似乎没问题,据我所知,这不是问题......但如果我错了,请告诉我!
【问题讨论】: