Search code examples
c++filestartupreadfile

Reading file on system startup from registry [C++]


I want my program to read a config file which is right next to the "exe file. That's working properly, but when the program starts with system from run folder in registry (HKEY_CURRENT_USER), it simply can't read the file. The exe file and the config file are in one folder in "Program Files(x86)". Here is a snippet of a code starting on program startup:

    TCHAR szPath[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, szPath);
    char filepath[MAX_PATH];
    sprintf(filepath, "%s%s", szPath, "\\data.cfg");
    if(!(file = fopen(filepath, "rt")))
    {
        MessageBox(hwnd, "Could not load config file", "ERROR", MB_OK);
    }
    else
    {
        fgets(ReadLine, 20, file);
        sscanf(ReadLine, "#KEYSTROKE %s", &ReadChar);

        fgets(ReadLine2, 20, file);
        sscanf(ReadLine2, "#STARTUP %s", &ReadChar2);

        fgets(ReadLine3, 20, file);
        sscanf(ReadLine3, "#CTRL %s", &ReadChar3);

        fclose(file);
    }

Any help would be appreciated. Thanks in advance.


Solution

  • You should use GetModuleFileName() and extract folder info from there.