windows c++窗口應用程序靜默運行

一、靜默運行

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章