Feb 21, 2019 03:36 PM
(This post was last modified: Feb 21, 2019 03:37 PM by Keat.)
I am trying to use CreateProcessW() to run cmd.exe and run commands. I am then trying to get unicode back so I can support all languages.
I have working code that does what I need it to. The issue I am running into is only built in command prompt commands will output unicode with the "/u" argument given.
Works - will give me back unicode.
don't - will not give me unicode
I have taken a picture of the bytes it returns back to me.
This is "works". It shows that I am getting unicode back.
https://i.imgur.com/KQepszB.png
This is "dont. Showing it only gives me ANSI.
https://i.imgur.com/YKpSqru.png
Does anyone know of a better way to go about what I am trying to accomplish. I am basically going to end up building a pseudo remote shell. I want the user to be able to send commands and get the output, but support on non English computers as well.
Full test code that is handling this:
I have working code that does what I need it to. The issue I am running into is only built in command prompt commands will output unicode with the "/u" argument given.
Code
WCHAR works[] = L"cmd.exe /u /c dir C:\\windows";
WCHAR dont[] = L"cmd.exe /u /c ipconfig";
BOOL fSuccess = CreateProcessW(NULL, dont, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);Works - will give me back unicode.
don't - will not give me unicode
I have taken a picture of the bytes it returns back to me.
This is "works". It shows that I am getting unicode back.
https://i.imgur.com/KQepszB.png
This is "dont. Showing it only gives me ANSI.
https://i.imgur.com/YKpSqru.png
Does anyone know of a better way to go about what I am trying to accomplish. I am basically going to end up building a pseudo remote shell. I want the user to be able to send commands and get the output, but support on non English computers as well.
Full test code that is handling this:
Code
#include <Windows.h>
void main() {
HANDLE hPipeRead, hPipeWrite;
SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES) };
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
return;
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hPipeWrite;
si.hStdError = hPipeWrite;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi = { 0 };
WCHAR works[] = L"cmd.exe /u /c dir C:\\windows";
WCHAR dont[] = L"cmd.exe /u /c ipconfig";
BOOL fSuccess = CreateProcessW(NULL, dont, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (!fSuccess) {
CloseHandle(hPipeWrite);
CloseHandle(hPipeRead);
return;
}
BOOL bProcessEnded = FALSE;
for (; !bProcessEnded;) {
bProcessEnded = WaitForSingleObject(pi.hProcess, 50) == WAIT_OBJECT_0;
while (TRUE) {
CHAR *buf = NULL;
DWORD dwRead = 0;
DWORD dwAvail = 0;
if (!PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwAvail, NULL))
break;
if (!dwAvail)
break;
buf = HeapAlloc(GetProcessHeap(), 0, dwAvail);
if (buf == NULL) {
return;
}
if (!ReadFile(hPipeRead, buf, dwAvail, &dwRead, NULL) || !dwRead)
break;
}
}
CloseHandle(hPipeWrite);
CloseHandle(hPipeRead);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return;
}

