Alexito
Alexito

Reputation: 483

c++ read config file parameters using GetPrivateProfileString

I have a win32 console application in C++, Visual Studio 2012. I cannot get the config parameter from the ini file. I tried different possibilities with file path,like placing the .ini file in source folder, write the full path to is, placing in the folder with generated .exe file. The output on the console after executing of the .exe file is 0 or (null) for string in every case. What I am doing wrong? How can I read the parameters?

v1:
LPCTSTR path = L".\\config.ini";
TCHAR protocolChar[32];
int port = GetPrivateProfileString(_T("PORT"), _T("SETTINGS"), _T(""), protocolChar, 32, path);
printf("***%d***\n", port);

v2:
int port = GetPrivateProfileInt(_T("PORT"), _T("SETTINGS"), 0, _T("config.ini"));

config.ini contains:

[SETTINGS]
USER_NUM_MAX = 256  ; Maximum number of users
PORT = 8080; 

Upvotes: 2

Views: 5860

Answers (2)

Mathe
Mathe

Reputation: 131

I faced this problem when I updated from VS2010 to VS2012.

On VS 2010 I simply called the function with the file name of the .ini-file as argument for lpFileName (see MSDN Documentation).

This was not working for VS 2012 any more, so I changed to go for the complete path like this:

char directoryPath[MAX_PATH];
char readParameter[MAX_STR_LEN];

GetCurrentDirectory( directoryPath, MAX_PATH );

string directoryPathAsString(directoryPath);
directoryPathAsString = directoryPathAsString + "\\" + filename;

GetPrivateProfileString("section","parameter","0",readParameter,MAX_STR_LEN, directoryPathAsString.c_str());

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148890

Oups, under Windows hitting a ini file in not that easy. In both tries (v1 and v2), you look for the file in current directory and then in Windows directory but not in the directory where the executable file is.

The easy way is to put all ini files under Windows directory. If you find cleaner to have the ini file along with the exe one, you have some more work to do :

  • find the executable file full path
  • replace the exe end with ini
  • use that full path to get access to your private ini file

To get the name of the executable file, simply use GetModuleFileName with a NULL HMODULE :

LPCTSTR getExecPath() {
    DWORD len = 64;
    for (;;) {
        LPTSTR fileName = new TCHAR[len];
        if (len == ::GetModuleFileName(NULL, fileName, len)) {
            delete fileName;
            len *= 2;
        }
        else {
            return fileName;
        }
    }
}

or if you prefere to directly get the ini file name :

LPCTSTR getIniName() {
    DWORD len = 4;
    for (;;) {
        LPTSTR fileName = new TCHAR[len];
        if (len == ::GetModuleFileName(NULL, fileName, len)) {
            delete fileName;
            len *= 2;
        }
        else {
            ::lstrcpy(fileName + lstrlen(fileName) - 3, "ini");
            return fileName;
        }
    }
}

and to not forget to delete the file name when done since it is allocated with new ...

Edit per comment :

For reference, the windows directory may depend on windows version. But it can always be retrieved by the API function GetWindowsDirectory. Extract from the reference page :

UINT WINAPI GetWindowsDirectory(
  _Out_  LPTSTR lpBuffer,
  _In_   UINT uSize
);

Parameters

  • lpBuffer [out] A pointer to a buffer that receives the path. This path does not end with a backslash unless the Windows directory is the root directory. For example, if the Windows directory is named Windows on drive C, the path of the Windows directory retrieved by this function is C:\Windows. If the system was installed in the root directory of drive C, the path retrieved is C:.
  • uSize [in] The maximum size of the buffer specified by the lpBuffer parameter, in TCHARs. This value should be set to MAX_PATH.

Return value

If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.

If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call GetLastError. *

Upvotes: 2

Related Questions