eusrh eusrh - 26 days ago 9
C++ Question

In win10 , why the lastest Parameters can't be using "&" in CreateProcess()?

enter image description here

there is my code:

int main()
{
STARTUPINFO si = { sizeof(si) };
PPROCESS_INFORMATION pi;
TCHAR szCommandLine[] = TEXT("C:\Windows\notepad.exe");
auto is_suc = CreateProcess(NULL, szCommandLine, NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
return 0;
}


Is exist some problem in win10 CreateProcess() API ?

Answer

You have:

PPROCESS_INFORMATION pi;  

This means (note that you have a P before PROCESS_INFORMATION, making the actual type a pointer to PROCESS_INFORMATION):

// PPROCESS_INFORMATION = PROCESS_INFORMATION*
PROCESS_INFORMATION *pi;  

CreateProcess expects a pointer to PROCESS_INFORMATION as last parameter, but with &pi you are passing a pointer to pointer (double indirection).

You can fix that by changing the definition of your pi variable:

PROCESS_INFORMATION pi;

Then &pi will correctly be a PROCESS_INFORMATION*, as CreateProcess expects. And, on success, the pi variable will receive the process identification information as expected.

P.S. For better code hygiene, I would suggest to clear the PROCESS_INFORMATION structure as well:

PROCESS_INFORMATION pi{ 0 };
Comments