控制台程序咋和GUI程序通信( 二 )
#include \u0026lt;windows.h\u0026gt;#include \u0026lt;tchar.h\u0026gt;#include \u0026lt;stdio.h\u0026gt;#include \u0026lt;strsafe.h\u0026gt;#include \u0026lt;string.h\u0026gt;#define BUFSIZE 1024CHAR chBuf = {0};HANDLE g_hChildStd_IN_Rd = NULL;HANDLE g_hChildStd_IN_Wr = NULL;HANDLE g_hChildStd_OUT_Rd = NULL;HANDLE g_hChildStd_OUT_Wr = NULL;HANDLE g_hInputFile = NULL;void CreateChildProcess(void);void WriteToPipe(void);void ReadFromPipe(void);void ErrorExit(PTSTR);int length;int _tmain(int argc, TCHAR *argv){\tSECURITY_ATTRIBUTES saAttr;\tprintf("\-\u0026gt;Start of parent execution.\");\tsaAttr.nLength = sizeof(SECURITY_ATTRIBUTES);\tsaAttr.bInheritHandle = TRUE;\tsaAttr.lpSecurityDescriptor = NULL;\tif (!CreatePipe(\u0026amp;g_hChildStd_OUT_Rd, \u0026amp;g_hChildStd_OUT_Wr, \u0026amp;saAttr, 0))\t\tErrorExit(TEXT("StdoutRd CreatePipe"));\tif (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))\t\tErrorExit(TEXT("Stdout SetHandleInformation"));\tif (!CreatePipe(\u0026amp;g_hChildStd_IN_Rd, \u0026amp;g_hChildStd_IN_Wr, \u0026amp;saAttr, 0))\t\tErrorExit(TEXT("Stdin CreatePipe"));\tif (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))\t\tErrorExit(TEXT("Stdin SetHandleInformation"));\tCreateChildProcess();\tscanf("%s", chBuf);\tlength = strlen(chBuf); WriteToPipe();\tReadFromPipe();\tprintf("\-\u0026gt;End of parent execution.\");\treturn 0;}void CreateChildProcess(){\tTCHAR szCmdline = TEXT("test");\tPROCESS_INFORMATION piProcInfo;\tSTARTUPINFO siStartInfo;\tBOOL bSuccess = FALSE;\tZeroMemory(\u0026amp;piProcInfo, sizeof(PROCESS_INFORMATION));\tZeroMemory(\u0026amp;siStartInfo, sizeof(STARTUPINFO));\tsiStartInfo.cb = sizeof(STARTUPINFO);\tsiStartInfo.hStdError = g_hChildStd_OUT_Wr;\tsiStartInfo.hStdOutput = g_hChildStd_OUT_Wr;\tsiStartInfo.hStdInput = g_hChildStd_IN_Rd;\tsiStartInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;\tbSuccess = CreateProcess(NULL,\t\tszCmdline,\t // command line\t\tNULL,\t\t\t // process security attributes\t\tNULL,\t\t\t // primary thread security attributes\t\tTRUE,\t\t\t // handles are inherited\t\t0,\t\t\t\t // creation flags\t\tNULL,\t\t\t // use parent\u0026#39;s environment\t\tNULL,\t\t\t // use parent\u0026#39;s current directory\t\t\u0026amp;siStartInfo, // STARTUPINFO pointer\t\t\u0026amp;piProcInfo); // receives PROCESS_INFORMATION\tif (!bSuccess)\t\tErrorExit(TEXT("CreateProcess"));\telse\t{\t\tCloseHandle(piProcInfo.hProcess);\t\tCloseHandle(piProcInfo.hThread);\t}}void WriteToPipe(void){\tDWORD dwWritten;\tBOOL bSuccess = FALSE;\t//for (;;)\t//{\t\tbSuccess = WriteFile(g_hChildStd_IN_Wr, chBuf,length, \u0026amp;dwWritten, NULL);\t//\tif (!bSuccess) break;\t//} printf("写入管道:%d\", int(dwWritten));\tif (!CloseHandle(g_hChildStd_IN_Wr))\t\tErrorExit(TEXT("StdInWr CloseHandle"));}void ReadFromPipe(void){\tDWORD dwRead, dwWritten;\tCHAR chBuf = {0};\tBOOL bSuccess = FALSE;\tHANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE); for(;;) { bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, \u0026amp;dwRead, NULL); if (!bSuccess || dwRead == 0) break; bSuccess = WriteFile(hParentStdOut, chBuf, dwRead, \u0026amp;dwWritten, NULL); if (!bSuccess || dwRead == length+2) break; }}void ErrorExit(PTSTR lpszFunction){\tLPVOID lpMsgBuf;\tLPVOID lpDisplayBuf;\tDWORD dw = GetLastError();\tFormatMessage(\t\tFORMAT_MESSAGE_ALLOCATE_BUFFER |\t\tFORMAT_MESSAGE_FROM_SYSTEM |\t\tFORMAT_MESSAGE_IGNORE_INSERTS,\t\tNULL,\t\tdw,\t\tMAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\t\t(LPTSTR)\u0026amp;lpMsgBuf,\t\t0, NULL);\tlpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,\t\t(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40)*sizeof(TCHAR));\tStringCchPrintf((LPTSTR)lpDisplayBuf,\t\tLocalSize(lpDisplayBuf) / sizeof(TCHAR),\t\tTEXT("%s failed with error %d: %s"),\t\tlpszFunction, dw, lpMsgBuf);\tMessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);\tLocalFree(lpMsgBuf);\tLocalFree(lpDisplayBuf);\tExitProcess(1);}以上。
推荐阅读
- 招聘都要学历,何来程序员不看学历
- 银行系统的研发岗(程序员)是不是很难进(校招)推广到国企的研发岗(程序员)呢
- 坐标合肥,请问在哪里能捕捉到程序员这种生物他们大都出现在哪里呢
- 程序猿和产品汪能愉快的一起谈恋爱嘛
- 怎样成为一名合格的Python程序员?
- 我是程序猿养了条狗,取啥名字好
- 送程序员男友苹果无线耳机还是专业品牌耳机
- 女程序媛怎样追求男程序员
- 阿里程序员网上招人的情况为何如此多见
- 朋友圈权限被破解,怎样反破解