How to pass a string to CreateProcess function?

I want to pass a string to my CreateProcess function so that I can use this function for all my operations. How to do it right?

Below is my code:

CString ExecuteExternalProgram(CString pictureName) { CString parameterOne = _T(" -format \"%h\" C:\\"); CString filename = pictureName; CString parameterLast = _T("\""); CString parameterFull = parameterOne + filename + parameterLast; CreateProcess(_T("C:\\identify.exe"), parameterFull,0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); CloseHandle(wPipe); ....... } 

Error:

Error 2 of error C2664: "CreateProcessW": cannot convert parameter 2 from "ATL :: CString" to "LPWSTR" c: \ a.cpp

+4
source share
1 answer

You need to do something like:

 CreateProcess(L"C:\\identify.exe",csExecute.GetBuffer(),0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); 

CreateProcess() for some reason wants to write a buffer for the command line parameter, so the implicit conversion of CString to a plain old pointer will not happen (since it will only perform the implicit conversion if it is a const pointer).

If this is not the problem you are experiencing, send more details about the error or unexpected behavior that you are working in.

As an example, the following program launches a small utility program that resets the command line specified to it:

 int main() { CString csExecute = "some string data"; STARTUPINFO sInfo = {0}; sInfo.cb = sizeof(sInfo); PROCESS_INFORMATION pInfo = {0}; CreateProcess(L"C:\\util\\echoargs.exe",csExecute.GetBuffer(),0,0,TRUE, NORMAL_PRIORITY_CLASS,0,0,&sInfo,&pInfo); return 0; } 
+2
source

Source: https://habr.com/ru/post/1334150/


All Articles