#define WIN32_LEAN_AND_MEAN #include #include #define TEXT_SIZE (64 * 64) HINSTANCE hInst; LPCTSTR szWindowClass = TEXT("CreatePipe"); LPCTSTR szTitle = TEXT("CreatePipe Test"); ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); BOOL CreatePipeTest(HWND hEditWindow, char *MainMemory); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } BOOL bRet; while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) { if(bRet == -1) break; if (!TranslateAccelerator(msg.hwnd, NULL, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, NULL); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, NULL); return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { RECT rect; static HWND hEditWindow; static char *MainMemory; switch (message) { case WM_CREATE: if((MainMemory = (char *)VirtualAlloc( NULL, TEXT_SIZE, MEM_COMMIT, PAGE_READWRITE)) == NULL){ MessageBox(hWnd, "必要な領域を確保できませんでした", "Error", MB_OK); DestroyWindow(hWnd); } GetClientRect(hWnd, &rect); hEditWindow = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | ES_WANTRETURN | ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | WS_HSCROLL, 0, 0, rect.right, rect.bottom, hWnd, NULL, hInst, NULL); SendMessage(hEditWindow, EM_SETLIMITTEXT, (WPARAM)TEXT_SIZE, 0); CreatePipeTest(hEditWindow, MainMemory); break; case WM_SIZE: GetClientRect(hWnd, &rect); MoveWindow(hEditWindow, rect.left, rect.top, rect.right, rect.bottom, TRUE); break; case WM_DESTROY: VirtualFree(MainMemory, TEXT_SIZE, MEM_DECOMMIT); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } BOOL CreatePipeTest(HWND hEditWindow, char *MainMemory) { HANDLE pfd_in[2], pfd_out[2], pfd_err[2]; HANDLE fd_read, fd_write, fd_err; short int R = 0, W = 1; HANDLE hParent = GetCurrentProcess(); SECURITY_ATTRIBUTES SA; SA.lpSecurityDescriptor = NULL; SA.bInheritHandle = TRUE; SA.nLength = sizeof(SA); CreatePipe(&pfd_out[R], &pfd_out[W], &SA, 0); DuplicateHandle(hParent, pfd_out[R], hParent, &fd_write, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(pfd_out[R]); CreatePipe(&pfd_err[R], &pfd_err[W], &SA, 0); DuplicateHandle(hParent, pfd_err[R], hParent, &fd_err, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(pfd_err[R]); CreatePipe(&pfd_in[R], &pfd_in[W], &SA, 0); DuplicateHandle(hParent, pfd_in[W], hParent, &fd_read, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(pfd_in[W]); STARTUPINFO SI; ZeroMemory(&SI, sizeof(SI)); SI.cb = sizeof(SI); SI.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; SI.wShowWindow = SW_HIDE; SI.hStdInput = pfd_in[R]; SI.hStdOutput = pfd_out[W]; SI.hStdError = pfd_err[W]; PROCESS_INFORMATION PI; if(CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &SI, &PI) != TRUE){ MessageBox(NULL, "CreateProcessが実行できません。", "ERROR", NULL); CloseHandle(fd_read); CloseHandle(fd_write); CloseHandle(fd_err); return TRUE; } DWORD Len; WriteFile(fd_read, "dir\r\n", 5, &Len, NULL); WriteFile(fd_read, "exit\r\n", 6, &Len, NULL); FlushFileBuffers(fd_write); FlushFileBuffers(fd_read); WaitForSingleObject(PI.hProcess, INFINITE); ReadFile(fd_write, MainMemory, TEXT_SIZE - 1, &Len, NULL); MainMemory[Len] = '\0'; CloseHandle(PI.hProcess); CloseHandle(fd_read); CloseHandle(fd_write); CloseHandle(fd_err); Edit_SetText(hEditWindow, MainMemory); return FALSE; }