本部分主要实现的是 在python的程序启动的时候(严格说应该是启动后)隐藏、关闭命令行窗口。
VS版本:VS2017社区版
OS版本:win7-64位
我的参考文章-站在巨人的肩膀上:
本人在查找方法的时候参考了以下3篇文章,但是遇到了很多奇怪的问题,主要是因为没用过createProcess这个函数,盲目copy网上同行的代码去尝试,没有仔细去看它的定义。最后仔细阅读了微软的官方文档 (https://docs.microsoft.com/zh-cn/windows/win32/procthread/creating-processes )才理解3篇文章中的差异,解决了自己的问题。微软依旧是巨人,看源文档才是王道。
https://www.cnblogs.com/King2019Blog/p/11296711.html
https://www.cnblogs.com/liuweilinlin/archive/2012/08/20/2647168.html
https://blog.csdn.net/yuyan987/article/details/78644922
下图是效果图,图后面是源代码。大家可以参考。
// call_python_test4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 此文件测试CreateProcess函数调用外部exe程序。一般情况会先打开cmd窗口,然后通过cmd控制台打开外部exe程序。
// 经过测试,可以做到启动外部程序后,瞬间关闭命令行窗口
#include "pch.h"
#include
#include "windows.h"
#include
#include
#include
#include
#include
void createProcess_nocmdwindow();
int main()
{
createProcess_nocmdwindow();
return 0;
}
/*OK*/
void createProcess_nocmdwindow()
{
STARTUPINFO StartupInfo;//创建进程所需的信息结构变量
PROCESS_INFORMATION ProcessInfo;
GetStartupInfo(&StartupInfo);
StartupInfo.lpReserved = NULL;
StartupInfo.lpDesktop = NULL;
StartupInfo.lpTitle = NULL;
StartupInfo.dwX = 0;
StartupInfo.dwY = 0;
StartupInfo.dwXSize = 0;
StartupInfo.dwYSize = 0;
StartupInfo.dwXCountChars = 500;
StartupInfo.dwYCountChars = 500;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
//说明进程将以隐藏的方式在后台执行
StartupInfo.cbReserved2 = 0;
StartupInfo.lpReserved2 = NULL;
StartupInfo.hStdInput = stdin;
StartupInfo.hStdOutput = stdout;
StartupInfo.hStdError = stderr;
BOOL bRet;
TCHAR appName[] = _T("E:\\study_python\\genera_sine_wav_file\\release\\release_win7\\general_2ch_sinwav_file.v1.300.exe");//OK no cmd window
//TCHAR szCmdline[] = _T("D:\\Python37\\python.exe E:\\general_2ch_sinwav_file.v1.300.py"); //this str is tesed, OK
//TCHAR szCmdline[] = _T("D:\\Python37\\python.exe D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw"); //this str is tesed, OK
//TCHAR szCmdline[] = _T("\"D:\\Python37\\python.exe\" D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw"); //this str is tesed, OK
/* MS参考code:https://docs.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw */
//LPTSTR szCmdlineline[] = _tcsdup(TEXT("\"C:\\Program Files\\MyApp\" -L -S"));
bRet = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
以下部分是个人测试code,请忽略。----不要被下面混乱的代码影响了你的判断
// call_python_test4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 此文件测试CreateProcess函数调用外部exe程序。一般情况会先打开cmd窗口,然后通过cmd控制台打开外部exe程序。
// 经过测试,可以做到启动外部程序后,瞬间关闭命令行窗口
#include "pch.h"
#include
#include "windows.h"
#include
#include
#include
#include
#include
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
int method1_test();
int method2_test();
void createProcess();
void createProcess_nocmdwindow();
int main()
{
//return method1_test();
//return method2_test();
createProcess_nocmdwindow();
return 0;
createProcess();
return 0;
}
/*OK*/
void createProcess()
{
STARTUPINFO StartupInfo;//创建进程所需的信息结构变量
PROCESS_INFORMATION ProcessInfo;
GetStartupInfo(&StartupInfo);
StartupInfo.lpReserved = NULL;
StartupInfo.lpDesktop = NULL;
StartupInfo.lpTitle = NULL;
StartupInfo.dwX = 0;
StartupInfo.dwY = 0;
StartupInfo.dwXSize = 0;
StartupInfo.dwYSize = 0;
StartupInfo.dwXCountChars = 500;
StartupInfo.dwYCountChars = 500;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
//说明进程将以隐藏的方式在后台执行
StartupInfo.cbReserved2 = 0;
StartupInfo.lpReserved2 = NULL;
StartupInfo.hStdInput = stdin;
StartupInfo.hStdOutput = stdout;
StartupInfo.hStdError = stderr;
BOOL bRet;
//char *appName = (char *)malloc(256);
//appName = "E:\\work\\project0\\spider\\bin\\Release\\spider1.exe";
/*method 1: successfull*/
TCHAR appName[] = _T("E:\\study_python\\genera_sine_wav_file\\release\\release_win7\\general_2ch_sinwav_file.v1.300.exe");//OK no cmd window
bRet = CreateProcess(appName, NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
//TCHAR appName[] = _T("E:\\createProcess_test_1.exe"); // OK no cmd window
//bRet = CreateProcess(appName, NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo); //ok
/*method 2: fail, can not run*/
//TCHAR appName[] = _T("D:\\Python37\\python.exe D:\\Python37\\general_2ch_sinwav_file.v1.300.py");//error, can not run
//TCHAR appName[] = _T("\"D:\\Python37\\python.exe\" D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw");//error, can not run
//bRet = CreateProcess(appName, NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
/*method 3: error*/
/*TCHAR appName[] = _T("D:\\Python37\\python.exe");
TCHAR szCmdline[] = _T("D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw");
bRet = CreateProcess(appName, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);*/
/*method 4: successfull*/
/*TCHAR szCmdline[] = _T("\"D:\\Python37\\python.exe\" D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw");
bRet = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);*/
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
/*OK*/
void createProcess_nocmdwindow()
{
STARTUPINFO StartupInfo;//创建进程所需的信息结构变量
PROCESS_INFORMATION ProcessInfo;
GetStartupInfo(&StartupInfo);
StartupInfo.lpReserved = NULL;
StartupInfo.lpDesktop = NULL;
StartupInfo.lpTitle = NULL;
StartupInfo.dwX = 0;
StartupInfo.dwY = 0;
StartupInfo.dwXSize = 0;
StartupInfo.dwYSize = 0;
StartupInfo.dwXCountChars = 500;
StartupInfo.dwYCountChars = 500;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
//说明进程将以隐藏的方式在后台执行
StartupInfo.cbReserved2 = 0;
StartupInfo.lpReserved2 = NULL;
StartupInfo.hStdInput = stdin;
StartupInfo.hStdOutput = stdout;
StartupInfo.hStdError = stderr;
BOOL bRet;
TCHAR szCmdline[] = _T("D:\\Python37\\python.exe E:\\general_2ch_sinwav_file.v1.300.py"); //this str is tesed, OK
//TCHAR szCmdline[] = _T("D:\\Python37\\python.exe D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw"); //this str is tesed, OK
//TCHAR szCmdline[] = _T("\"D:\\Python37\\python.exe\" D:\\Python37\\general_2ch_sinwav_file.v1.300.pyw"); //this str is tesed, OK
/* MS参考code:https://docs.microsoft.com/zh-cn/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw */
//LPTSTR szCmdlineline[] = _tcsdup(TEXT("\"C:\\Program Files\\MyApp\" -L -S"));
bRet = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
void ExecDOSCMD(const char * szCommand)
{
STARTUPINFO si;
memset(&si, 0, sizeof(STARTUPINFO));
//进程信息
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
// 取得当前进程的StartupInfo,并根据当前进程进行修改
GetStartupInfo(&si);
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
char lpAppName[4096] = "";//此处不加cmd /k
strcat_s(lpAppName, 4096, szCommand);
CreateProcess((LPWSTR)(lpAppName), NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
}
/*fail*/
int method1_test()
{
//ExecDOSCMD(("calc.exe"));
//ExecDOSCMD(("E:\\study_python\\genera_sine_wav_file\\release\\release_win7\\general_2ch_sinwav_file.v1.300.exe"));
ExecDOSCMD(("E:\\call_python_test3.exe"));
return 0;
}
/*test fail*/
int method2_test()
{
HDESK hDesk = CreateDesktop(_T("MyDesk"), NULL, NULL, 0, GENERIC_ALL, NULL);
assert(hDesk);
STARTUPINFO si = { sizeof(si) };
si.lpDesktop = NULL;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi = { 0 };
TCHAR szCmdline[MAX_PATH] = _T("E:\\call_python_test3.exe");
if (CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
return 0;
}