第一个程序:调用WinExec()函数,但是打开应用程序后,得手动关闭,没有自行结束的方法。
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
int main()
{
if(WinExec("C://Program Files//WinRAR//WinRAR.exe D://My_Document_ZL//Study//C_code_example//my_printf.tgz", 1));
printf("success/n");
return 0;
}
第二个程序:调用CreatProcess()函数,它可以用TerminateProcess()函数使之关闭,还可以使之延迟时间。
#include <process.h>
#include <Windows.h>
#include <Winuser.h>
#include <stdio.h>
void main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
char* szCmdline[]={"D://My_Document_ZL//C_test//test//Debug//atio.exe"};
//int i;
int ret;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
printf("Create Process beginning.../n");
ret = CreateProcess(NULL, szCmdline[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
// 等待5秒后,将强行关闭进程。
if(WaitForSingleObject(pi.hProcess, 5000) == WAIT_TIMEOUT)
{
TerminateProcess(pi.hProcess, 0);
}
CloseHandle(pi[0].hThread);
CloseHandle(pi[0].hProcess);
}