#include <stdio.h>
#include <windows.h>
#pragma warning(disable : 4996)
int main(int argc, wchar_t** argv)
{
DWORD bytes_read;
WCHAR* buffer[4096];
LPWSTR str;
STARTUPINFO start_info = { 0 };
PROCESS_INFORMATION process_info = { 0 };
ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, 4096, &bytes_read, NULL);
str = malloc(bytes_read);
memcpy(str, buffer, bytes_read);
// debug - stdout + file
wprintf(L"\nTrying to run -> \"%LS\"", str);
FILE *f = fopen("file.txt", "w");
fwprintf(f, L"%LS", str);
fclose(f);
BOOL result = CreateProcessW(
NULL,
str,
NULL,
NULL,
TRUE,
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
NULL,
NULL,
&start_info,
&process_info
);
if (!result) {
DWORD err = GetLastError();
WCHAR *errstr;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errstr, 0, NULL
);
wprintf(L"\nError -> %ls", errstr);
return 1;
}
return 0;
}
运行它会产生以下结果
C:\Users\Admin\source\repos\ConsoleApplication1\Debug
λ php -r "echo \"C:\\Windows\\System32\\PING.EXE\0\";"|ConsoleApplication1.exe
Trying to run -> "C:\Windows\System32\PING.EXE"
Error -> The system cannot find the file specified.
因此,似乎使用了正确的输入。同样在file.txt中,用十六进制编辑器查看时
似乎没有任何可疑之处。当像下面这样硬编码str而不是使用stdin
wchar_t cmd[] = L"C:\\Windows\\System32\\PING.EXE";
有效。
要使CreateProcessW与stdin输入一起工作,我需要做什么?
答案 0 :(得分:0)
WCHAR* buffer[4096];
这将创建一个字符串数组。您只需要一个宽字符缓冲区。
使用L"%s"
格式说明符进行测试:
wprintf(L"%s\n", buffer)
如果可能,将Unicode文本作为命令行传递,例如
php -r "echo \"Test...\0\";"|app.exe Unicode
Unicode文本可以包含UTF16字符集。
int main(void)
{
wprintf(L"GetCommandLineW %s\n", GetCommandLineW());
DWORD bytes_read = 0;
char buffer[4096] = { 0 };
ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer,
sizeof(buffer), &bytes_read, NULL);
int codepage = CP_UTF8;//or CP_ACP
int wlen = MultiByteToWideChar(codepage, 0, buffer, -1, 0, 0);
wchar_t *wstr = malloc(wlen * 2);
MultiByteToWideChar(codepage, 0, buffer, -1, wstr, wlen);
wprintf(L"pipe %s\n", wstr);
free(wstr);
return 0;
}