WinExec
原型:
UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);
用於十六位操作系統及兼容系統.
例如:
WinExec("notepad.exe f:\調用程序.txt",SW_SHOW);
WinExec("notepad.exe ",SW_SHOW);
不同的參數用空格分開,故路徑中不能有空格,而大部分程序默認是安裝在"...Program Files...",如Word,這極大的限制了WinExec的應用范圍.
以上可不帶路徑:
1,程序所在目錄.
2,當前路徑.
3,系統目錄,可以用GetSystemDirectory得到.
4,Windows 目錄. 可以用TheGetWindowsDirectory得到.
5,在環境變量中設置的目錄.
ShellExecute
原型:
HINSTANCE ShellExecute(
HWND hwnd, //父窗口句柄
LPCTSTR lpOperation, //操作,"open","print","explore"
LPCTSTR lpFile, //文件名,前面可加路徑
LPCTSTR lpParameters, //參數
LPCTSTR lpDirectory, //默認文件夾
INT nShowCmd //顯示方式
);
打開一個應用程序
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );
或
ShellExecute(this->m_hWnd,"open","notepad.exe","c:MyLog.log","",SW_SHOW );
打開一個同系統程序相關連的文檔
ShellExecute(this->m_hWnd,"open","c:abc.txt","","",SW_SHOW );
激活相關程序,發送EMAIL
ShellExecute(this->m_hWnd,"open","mailto:[email protected]","","", SW_SHOW );
用系統打印機打印文檔
ShellExecute(this->m_hWnd,"print","c:abc.txt","","", SW_HIDE);
lpParameters的用法示例:
一,建立一個可以接受參數的程序call.exe,添加如下代碼:
BOOL CCallApp::InitInstance()
{
int n = __argc;
for(int i = 1 ; i < n ; i++)
AfxMessageBox(__targv[i]);
//__targv[0]存儲的是程序的文件名
...
}
二,Alt + F7的進行Project setting, Debug -> program argurments ->"1 2 3 4 5".
如果有多個參數,用空格分開.
三,運行.
四,執行ShellExecute(NULL,NULL,"f:\call.exe","1 2 3 4 5",NULL,SW_SHOW);
BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWord dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
STARTUPINFO startupInfo;
memset(&startupInfo,0,sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
示例:
//程序最啟動時最大化
startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOWMAXIMIZED;
//運行....exe
PROCESS_INFORMATION ProcessInfo;
BOOL bCreate = ::CreateProcess
(
"f:\call.exe",// 1 2 3 4",
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&ProcessInfo);
//等到call.exe執行完畢
WaitForSingleObject(ProcessInfo.hProcess,1000000);
MessageBox("調用程序結束!");