-2
LPCSTR      __FileName = "program.exe";   

void ProcessRun(LPCSTR pszExeName)
    {
        PROCESS_INFORMATION piProcInfoGPS;
        STARTUPINFO siStartupInfo;
        SECURITY_ATTRIBUTES saProcess, saThread;
        ZeroMemory(&siStartupInfo, sizeof(siStartupInfo));
        siStartupInfo.cb = sizeof(siStartupInfo);
        saProcess.nLength = sizeof(saProcess);
        saProcess.lpSecurityDescriptor = NULL;
        saProcess.bInheritHandle = true;
        saThread.nLength = sizeof(saThread);
        saThread.lpSecurityDescriptor = NULL;
        saThread.bInheritHandle = true;

        CreateProcess(NULL, (LPTSTR)pszExeName, &saProcess, &saThread, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcInfoGPS);

        __hProcess = piProcInfoGPS.hProcess;
        __ProcessID = piProcInfoGPS.dwProcessId;
    }

If I pass __FileName to the function the program will run. However, when I read the filename from my ini file

[Launcher]
FileName=program.exe


char INIValue[256];
GetPrivateProfileString("Launcher", "FileName", "nan.exe", INIValue, 256, ".\\BackgroundConfig.ini");

string temp(INIValue);
__FileName = temp.c_str();

And then try to pass the filename to the function, it doesn't run. What in the world is causing this? The file name is exactly the same.

Zute
  • 49
  • 8
  • Check your current directory, most likely the ini file is not being found. Check your return values and error codes. Use a debugger and see what value (if any) is being read from the file. There are lots of things you could try before asking on here. – Jonathan Potter Dec 13 '14 at 13:43
  • @JonathanPotter Hello. The ini file is being found and read. I have checks for this. The value is correctly read, see http://i.imgur.com/tqv7w6T.png. If I simply comment out the read part and manually set the LPCSTR to "Nightcore.exe" the process will start, which I find weird. – Zute Dec 13 '14 at 13:47
  • Is your second code snippet within the same function that is calling `ProcessRun`? I suspect your `string` might be getting destroyed before you call `ProcessRun` – wakjah Dec 13 '14 at 13:48
  • @wakjah Hello. No it is not. But I am assigning the value to a non-local variable, so how does this create an issue? Never thought of testing but the received string in ProcessRun is indeed NULL. Why is this? – Zute Dec 13 '14 at 13:56

1 Answers1

2

You haven't shown enough code to be certain that this is the issue, but consider the following:

char * someString;

void foo()
{
    std::string str("whatever");
    doSomethingWithCharStar(str.c_str()); // fine: str's data has not been destroyed yet
    someString = str.c_str();
} // uh-oh, here the std::string's memory will be deleted

void bar()
{
    foo(); // sets someString
    doSomethingWithCharStar(someString); // by the time we get here, though, the memory pointed to by someString is freed
}

The code above invokes undefined behaviour (assuming doSomethingWithCharStar dereferences the pointer passed in). Instead of storing a char* as a global, you should store a std::string. Better yet, don't use a global at all:

std::string foo()
{
    return std::string("whatever");
}

void bar()
{
    std::string value(foo());
    doSomethingWithCharStar(value.c_str());
}

Note that the issue encountered in the first snippet above is essentially the same issue as when Returning a reference to a local variable.

Community
  • 1
  • 1
wakjah
  • 4,541
  • 1
  • 18
  • 23
  • Hello, thanks. My bad, I thought it would be okay if I passed str's data to another non-local variable. Making both variables non-local solved the problem. Thanks again. – Zute Dec 13 '14 at 14:13