ShellExecute Performs an operation on a specified file. HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd );使用上面的函数打开一个程序(记事本),参数是c:\abc.txt, TerminateProcess The TerminateProcess function terminates the specified process and all of its threads. BOOL TerminateProcess( HANDLE hProcess, // handle to the process UINT uExitCode // exit code for the process ); 使用这个函数关闭一个程序
Performs an operation on a specified file. HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);使用上面的函数打开一个程序(记事本),参数是c:\abc.txt,
TerminateProcess
The TerminateProcess function terminates the specified process and all of its threads. BOOL TerminateProcess(
HANDLE hProcess, // handle to the process
UINT uExitCode // exit code for the process
);
使用这个函数关闭一个程序
"c:\\abc.txt","",SW_SHOW );
最好用CreateProcess创建进程,然后获得这个进程的instance,只要获得了进程的聚丙,爱怎么做都可以了(TerminateProcess)
HINSTANCE hInstance=ShellExecute(this->m_hWnd,"open","notepad.exe",
"c:\\abc.txt","",SW_SHOW );
关闭
TerminateProcess(hInstance,0);
再初始化为 m_hPro = NULL;
下面两段分别是创建一个记事本进程,和关闭这个进程~
void CCtrlOtherPrcssDlg::OnButtono() //打开
{
// TODO: Add your control notification handler code here
PROCESS_INFORMATION pi;
STARTUPINFO si; memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW; BOOL fRet = CreateProcess(NULL,
"c:\\windows\\notepad.exe",
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi); if(!fRet) //failed
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
AfxMessageBox((LPCTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
}
else
{
AfxMessageBox("CreateProcess OK");
m_hPro = pi.hProcess;
}
}void CCtrlOtherPrcssDlg::OnButtonc() //关闭
{
// TODO: Add your control notification handler code here
if(m_hPro){ //Close
if(!TerminateProcess(m_hPro, 0)) //Error
{
LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
AfxMessageBox((LPCTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
}
else
{
AfxMessageBox("TerminateProcess OK");
}
m_hPro = NULL;
}
else
AfxMessageBox("m_hPro is NULL");
}
方法一:CFileFind ff;
if(ff.FindFile("sql.exe"))
{
ShellExecute(NULL,"open","sql.exe",NULL,NULL,SW_SHOW);
}
else
{
AfxMessageBox("文件没找到!");
}
方法二CFileFind ff;
if(ff.FindFile("sql.exe"))
{
WinExec("sql.exe",SW_SHOW);
}
else
{
AfxMessageBox("文件没找到!");
}
终止:
TerminateProcess()
方法一:CFileFind ff;
if(ff.FindFile("sql.exe"))
{
ShellExecute(NULL,"open","sql.exe",NULL,NULL,SW_SHOW);
}
else
{
AfxMessageBox("文件没找到!");
}
方法二CFileFind ff;
if(ff.FindFile("sql.exe"))
{
WinExec("sql.exe",SW_SHOW);
}
else
{
AfxMessageBox("文件没找到!");
}
终止:
TerminateProcess()