欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

windows c++窗口应用程序静默运行

程序员文章站 2022-07-06 16:27:20
...

一、静默运行

1、在代码中加上以下代码,在cmd命令窗口执行xxx.exe时,会静默执行

#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#pragma comment(linker, "/ENTRY:mainCRTStartup")

2、启动脚本start.bat

::不回显
@echo off
::查看进程是否已启动
tasklist | findstr agent.exe
if %errorlevel%==0 (
	echo The process has started!!!
) else (
    ::后台启动agent.ext,并将命令行参数传给agent.ext,%*表示所有命令行参数
	start /b .\agent.exe %*
    ::等待5s
	timeout /t 5 /nobreak > nul
    ::查看进程是否启动成功, && 表示前一个命令运行成功,后一个命令才运行
	tasklist | findstr agent.exe && echo The process start success. && goto end
	echo The process start failed!!!
)
:end

3、停止脚本stop.bat

@echo off
tasklist | findstr pagent.exe && taskkill /f /fi "IMAGENAME eq pcdn_router_agent.exe"

二、后台运行,通过vbscript控制

'定义变量
Dim wShell, exec
'变量赋值
Set wShell = CreateObject( "WScript.Shell" )
Set exec = wShell.Exec ("cmd.exe /c tasklist | findstr pcdn_router_agent.exe") 
'获取命令返回结果
str1 = exec.StdOut.ReadAll

if str1 <> "" then
	msgbox "Process has started, Do not start repeat!!!"
else
    '获取命令行参数
	Set oArgs = WScript.Arguments
	Dim param
	param = ".\Release\pcdn_router_agent.exe"
    '获取命令行传入的每一个参数
	For Each s In oArgs
		param = param & " " & s
	Next
	Set oArgs = Nothing
    '后台运行命令,0表示后台运行
	wShell.run param,0
	WScript.sleep 5000
	Set exec = wShell.Exec ("cmd.exe /c tasklist | findstr pcdn_router_agent.exe") 
	str1 = exec.StdOut.ReadAll
	if str1 <> "" then
		msgbox "Process start success"
	else
		msgbox "Process start failed!!!"
	end if
end if

 

相关标签: C++