この部分の主な目的は、Pythonプログラムの起動時にコマンドラインウィンドウを非表示にして閉じることです(厳密に言えば、起動後でなければなりません)。
VSバージョン:VS2017 Community Edition
OSバージョン:win7-64ビット
私の参考記事-巨人の肩の上に立つ:
メソッドを検索するときに次の3つの記事を参照しましたが、主にcreateProcess関数を使用したことがなく、その定義を注意深く見ずに、オンラインの対応するコードを盲目的にコピーして試行したため、多くの奇妙な問題が発生しました。最後に、Microsoftの公式ドキュメント(https://docs.microsoft.com/zh-cn/windows/win32/procthread/creating-processes )をよく読んでいるAberdeenは、違い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 <iostream>
#include "windows.h"
#include <WinBase.h>
#include <minwinbase.h>
#include <string.h>
#include <tchar.h>
#include <assert.h>
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);
}
次の部分は個人的なテストコードです。無視してください。----次の厄介なコードの影響を受けないでください
// call_python_test4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 此文件测试CreateProcess函数调用外部exe程序。一般情况会先打开cmd窗口,然后通过cmd控制台打开外部exe程序。
// 经过测试,可以做到启动外部程序后,瞬间关闭命令行窗口
#include "pch.h"
#include <iostream>
#include "windows.h"
#include <WinBase.h>
#include <minwinbase.h>
#include <string.h>
#include <tchar.h>
#include <assert.h>
//#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;
}