Reputation: 6807
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 correctly?
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 error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'ATL::CString' to 'LPWSTR' c:\a.cpp
Upvotes: 4
Views: 3481
Reputation: 340218
You'll 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()
wants a writeable buffer for the command line parameter for some reason, so the implicit conversion of CString
to a plain old pointer doesn't happen (since it'll only perform the implicit conversion if it's to a const pointer).
If this isn't the problem you're running into, post more details about the error or unexpected behavior you're running into.
As an example, the following runs a little utilty program that dumps the commandline it's given:
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;
}
Upvotes: 2