TheEggSample TheEggSample - 11 months ago 114
Ini Question

Writing to .ini file - SimpleIni SetValue not doing anything, despite appearing to succeed

So I've been trying to read and write from my windows service's .ini file. I am storing the ini file temporarily at 'C:\test\CppWindowsService.ini'. The program reads the ini just fine, but has trouble setting the value in the ini file. It appears to update the value, but in reality does not do anything. I am using brofield's SimpleIni available at github, something I've seen numerous people online using for their ini parsing needs. You can find it here.

Things I've tried/checked thus far:


  • Security Settings: the Administrators, Domain Admins, System and I all have read/write access. This section of code is being ran as my Domain Admin.



Any help is greatly appreciated, and thanks in advance!

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);
}
}


The output from the three various cout/wcout/print statements:

pval: noguid
{D002CD3F-6434-47E9-8246-06884FDE1FEA}
key: updated


Ini format:

[GUID]
Clientguid = noguid


I've only included the SimpleIni.h file, as the others look like they aren't really useful for me. This seems to be ok, and as far as I can tell, it isn't the issue... but please let me know if I'm wrong!

Answer Source

You need to save the INI file using the command:

ini.SaveFile("C:\\test\\CppWindowsService.ini");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download