双管道测试问题
我在学双管道的时候,出了点问题,在PreTranslateMessage(MSG* pMsg)函数中调用_beginthreadex(NULL, 0, TelnetThread, NULL, 0, NULL);但是,半天也没显示出来,下面是TelnetThread函数,麻烦大家帮忙看下
- C/C++ code
unsigned _stdcall TelnetThread(void *pParam){ SECURITY_ATTRIBUTES sa; HANDLE hRead, hWrite; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; CString strcmd; g_dlg->m_cmdshell.GetWindowText(strcmd); // 获取命令 strcmd = _T("cmd /k ") + strcmd; if (strcmd.IsEmpty()) { return 1; } // 创建命名管道 if (!CreatePipe(&hRead, &hWrite, &sa, NULL)) {// 创建管道失败 return 0; } STARTUPINFO si; PROCESS_INFORMATION pi; si.cb = sizeof(STARTUPINFO); GetStartupInfo(&si); si.hStdInput = hRead; si.hStdError = hWrite; si.hStdOutput = hWrite;// si.wShowWindow = SW_SHOW; si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; wchar_t cmdline[256] = {0}; // 得到系统路径 GetSystemDirectory(cmdline, sizeof(cmdline)); wcscat_s(cmdline, _T("\\cmd.exe")); // 创建cmd进程// if (CreateProcess(cmdline, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) == 0)// {// printf("CreateProcess Error\n");// } if (!CreateProcess(cmdline, strcmd.GetBuffer(strcmd.GetLength()), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) {// 创建线程失败 MessageBox(g_dlg->m_hWnd, _T("建立线程失败"), _T("提示"), MB_OK); return 0; } if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED) { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } CloseHandle(hWrite); char buffer[1024 * 4] = {0};// wchar_t buffer[1024 * 4] = {0}; DWORD byteRead; CString strResult = _T(""); while (true) { memset(buffer, 0, 1024 * 4); // 从命令管道中读取数据 if (ReadFile(hRead, buffer, 4095, &byteRead, NULL) != NULL) { g_dlg->m_show.GetWindowText(strResult); strResult += buffer; // 显示到界面上 g_dlg->m_show.SetWindowText(strResult); g_dlg->m_cmdshell.SetWindowText(_T("")); break; } else { break; } } CloseHandle(hRead); return 0;}[解决办法]