#define UNICODE #include <windows.h> #ifdef UNICODE #define _T(x) L ## x typedef unsigned short TCHAR; #else #define _T(x) x typedef char TCHAR; #endif DWORD __stdcall Err_ShowLast(TCHAR* lpTitle) { DWORD nLastError; LPTSTR lpMessageBuffer; nLastError = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (void*)&lpMessageBuffer, 0, NULL); MessageBox(NULL, lpMessageBuffer, lpTitle, MB_OK | MB_ICONERROR); LocalFree(lpMessageBuffer); return nLastError; } void* __stdcall Mem_Set(void* lpArea, BYTE nValue, DWORD nSize) { BYTE* lpByteArea; DWORD nI; lpByteArea = (BYTE*)lpArea; for (nI = 0; nI < nSize; nI++) lpByteArea[nI] = nValue; return lpArea; } DWORD __stdcall ReadPipe(HANDLE hPipe) { HANDLE hOutput; char lpBuffer[512]; DWORD nBytesRead; DWORD nBytesWritten; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); while (ReadFile(hPipe, lpBuffer, 512, &nBytesRead, NULL) && nBytesRead) WriteFile(hOutput, lpBuffer, nBytesRead, &nBytesWritten, NULL); return 0; } int __cdecl mainCRTStartup() { SECURITY_ATTRIBUTES securityAttribs; HANDLE hPipeInputRead; HANDLE hPipeInputWrite; HANDLE hPipeOutputRead; HANDLE hPipeOutputWrite; STARTUPINFO startupInfo; PROCESS_INFORMATION processInfo; TCHAR lpCommandLine[512]; /* Le paramètre lpCommandLine de CreateProcessW ne doit pas être read-only */ HANDLE hReadPipe; DWORD nBytesWritten; DWORD nResult; nResult = 1; /* Création des pipes de lecture et d'écriture */ securityAttribs.nLength = sizeof(securityAttribs); securityAttribs.lpSecurityDescriptor = NULL; securityAttribs.bInheritHandle = TRUE; if (! CreatePipe(&hPipeInputRead, &hPipeInputWrite, &securityAttribs, 0)) { Err_ShowLast(_T("Cannot create input pipe")); goto the_end; } if (! CreatePipe(&hPipeOutputRead, &hPipeOutputWrite, &securityAttribs, 0)) { Err_ShowLast(_T("Cannot create output pipe")); goto close_input_pipe; } /* Préparation des infos de lancement du processus avec pipe et affichage de la fenêtre */ Mem_Set(&startupInfo, 0, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; startupInfo.wShowWindow = SW_SHOW; startupInfo.hStdInput = hPipeInputRead; startupInfo.hStdOutput = hPipeOutputWrite; startupInfo.hStdError = hPipeOutputWrite; /* Lancement du processus */ lstrcpy(lpCommandLine, _T("cmd /C set /P a= && echo %a%")); if (! CreateProcess(NULL, lpCommandLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInfo)) { Err_ShowLast(_T("Cannot create process")); goto close_output_pipe; } /* Lancement du thread de lecture du pipe */ hReadPipe = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ReadPipe, hPipeOutputRead, 0, NULL); if (! hReadPipe) { Err_ShowLast(_T("Cannot create thread")); goto close_process; } /* Envoie d'une valeure pour a */ WriteFile(hPipeInputWrite, "toto\n", 5, &nBytesWritten, NULL); WaitForSingleObject(processInfo.hProcess, INFINITE); nResult = 0; CloseHandle(hReadPipe); close_process: CloseHandle(processInfo.hThread); CloseHandle(processInfo.hProcess); close_output_pipe: CloseHandle(hPipeInputRead); CloseHandle(hPipeInputWrite); close_input_pipe: CloseHandle(hPipeOutputWrite); CloseHandle(hPipeOutputRead); the_end: ExitProcess(nResult); return 0; }
lstrcpy(lpCommandLine, _T("ftp")); ... WriteFile(hPipeInputWrite, "help\nquit\n", 10, &nBytesWritten, NULL);
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question