Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

C++: deprecated conversion from string constant to 'LPSTR {aka char*}' [-Wwrite-strings] - warning. How to avoid?

While compiling C++ code I get this warning:

deprecated conversion from string constant to 'LPSTR {aka char*}' [-Wwrite-strings]
&pi)) // Pointer to PROCESS_INFORMATION structure.
     ^

Code is:

STARTUPINFO si;
memset(&si, 0, sizeof (STARTUPINFO));
si.cb = sizeof (STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;

PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof (PROCESS_INFORMATION));

if (!CreateProcess("C:\\Program Files\\Java\\jre7\\bin\\javaw.exe",
        " -jar install.jar", // Command line.
        NULL, // Process handle not inheritable.
        NULL, // Thread handle not inheritable.
        0, // Set handle inheritance to FALSE.
        CREATE_NO_WINDOW, // ON VISTA/WIN7, THIS CREATES NO WINDOW
        NULL, // Use parent's environment block.
        NULL, // Use parent's starting directory.
        &si, // Pointer to STARTUPINFO structure.
        &pi)) // Pointer to PROCESS_INFORMATION structure. //Warning comes from this line - variable &pi
{
    printf("CreateProcess failed\n");
    return 0;
}

Is it possible to do something to avoid that (I mean not using - suppress warnings)?

Upvotes: 4

Views: 5881

Answers (2)

David Heffernan
David Heffernan

Reputation: 612993

The compiler is telling you that the second parameter is of CreateProcess is of type char* but you are passing const char*. In other words, the second parameter of CreateProcess expects a modifiable buffer, but you are passing a non-modifiable literal.

Declare a modifiable string like this:

char cmdline[] = "-jar install.jar";

Upvotes: 8

Mantosh Kumar
Mantosh Kumar

Reputation: 5731

The 2nd argument from CreateProcess() API should be as follows:

_Inout_opt_ LPTSTR lpCommandLine,

now here you are directly passing the const char* value, hence you are getting the warning.

char cmdline[] = " -jar install.jar";

should avoid this warning.

Upvotes: 0

Related Questions