一个noconsole程序

貌似是一个外国人写的,作用应该是让控制台的程序运行的时候不会弹出那个控制台黑框。想用来让它不显示 php-cgi.exe 运行后的窗口,可是效果不是预期的。

项目在 github 的位置:https://github.com/mtstickney/noconsole

项目源码:

 1 #include <windows.h>
 2 #include <shellapi.h>
 3 #include <stdio.h>
 4 
 5 #ifdef UNICODE
 6 #define MAIN wWinMain
 7 #else
 8 #define MAIN WinMain
 9 #endif
10 
11 int WINAPI MAIN(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR pCmdLine, int nCmdShow)
12 {
13     int argnum = 1;
14     STARTUPINFO si;
15     PROCESS_INFORMATION pi;
16     DWORD exit_code;
17     
18     ZeroMemory(&si, sizeof(si));
19     si.cb = sizeof(si);
20     si.dwFlags = STARTF_USESHOWWINDOW;
21     si.wShowWindow = SW_HIDE;
22     ZeroMemory(&pi, sizeof(pi));
23 
24     /* Note: argnum is not like argc... */
25     if (argnum < 1) {
26         fputs("Usage: noconsole <program> [<args>...]
", stderr);
27         ExitProcess(1);
28     }
29 
30     /* Start the child process */
31     if (!CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, /* Note that the previous should maybe be set to the cwd of the program being run */
32             &si,
33             &pi))
34     {
35         fprintf(stderr, "Error starting process, code %d.
", (int)GetLastError());
36         ExitProcess(1);
37     }
38 
39     /* Wait for child exit */
40     WaitForSingleObject(pi.hProcess, INFINITE);
41 
42     GetExitCodeProcess(pi.hProcess, &exit_code);
43 
44     /* Clean up the handles */
45     CloseHandle(pi.hProcess);
46     CloseHandle(pi.hThread);
47 
48     ExitProcess(exit_code);
49     return 0;
50 }


 第35行是手动添加上的强制类型转换,不然在我的64位win8中会出错。

原文地址:https://www.cnblogs.com/vanwoos/p/4855990.html