64,654
社区成员
发帖
与我相关
我的任务
分享
#include <Windows.h>
#include <iostream>
#include <stdio.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
setlocale(LC_ALL, "chs");
std::cout << "我" <<std::endl;
return 0;
}
#include <Windows.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
SECURITY_ATTRIBUTES security_attributes = { 0 };
security_attributes.nLength = sizeof(security_attributes);
security_attributes.bInheritHandle = TRUE;
security_attributes.lpSecurityDescriptor = NULL;
HANDLE hReadPipe = NULL, hWritePipe = NULL;
if (::CreatePipe(&hReadPipe, &hWritePipe, &security_attributes, 0) != TRUE)
{
assert(false);
}
PROCESS_INFORMATION pipeProcessInfo = { 0 };
STARTUPINFOW startupInfo = { 0 };
startupInfo.cb = sizeof(startupInfo);
startupInfo.hStdError = hWritePipe;
startupInfo.hStdOutput = hWritePipe;
startupInfo.hStdInput = hReadPipe;
startupInfo.dwFlags = STARTF_USESTDHANDLES;
wchar_t commandLine[1024] = { L"A.exe" };
if (::CreateProcessW(NULL, commandLine, NULL, NULL, TRUE, 0, NULL, NULL, &startupInfo, &pipeProcessInfo) != TRUE)
{
assert(false);
}
else
{
::CloseHandle(pipeProcessInfo.hProcess);
::CloseHandle(pipeProcessInfo.hThread);
}
while (true)
{
#define BUFFER_SIZE 4096
uint8_t readBuffer[BUFFER_SIZE] = { 0 };
DWORD dwRead = 0;
::PeekNamedPipe(hReadPipe, readBuffer, BUFFER_SIZE, &dwRead, 0, 0);
if (dwRead > 0)
{
if (::ReadFile(hReadPipe, readBuffer, dwRead, &dwRead, NULL) == TRUE)
{
//最终要在这里,把readBuffer的字节流内容,转换成wstring,内容是"我"
}
}
}
return 0;
}
//A.exe
#include <iostream>
#include <locale>
int main()
{
std::locale chs("chs");
std::wcout.imbue(chs);
std::wcout << L"我";
return 0;
}
//B.exe
// crt_popen.c
/* This program uses _popen and _pclose to receive a
* stream of text from a system process.
*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
wchar_t psBuffer[128];
FILE *pPipe;
/* Run DIR so that it writes its output to a pipe. Open this
* pipe with read text attribute so that we can read it
* like a text file.
*/
if( (pPipe = _popen( ".\\A.exe", "rt" )) == NULL )
exit( 1 );
/* Read pipe until end of file, or an error occurs. */
while(fgetws(psBuffer, 128, pPipe))
{
wprintf(psBuffer);
}
/* Close pipe and print return value of pPipe. */
if (feof( pPipe))
{
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}
else
{
printf( "Error: Failed to read the pipe to the end.\n");
}
}