■ 在应用程序中启动其他程序的方法:
1. UINT WinExec(LPCSTR lpCmdLine,UINT uCmdShow)
Eg: if(WinExec(“cdplayer.exe”,SW_MAXIMIZE)<=31)
AfxMessageBox(“运行CD播放器失败”);
Eg: WinExec("winmine.exe",SW_SHOWNORMAL);
WinExec("C://WINDOWS//System32//sol.exe",SW_SHOWNORMAL);
2. STARTUPINFO stinfo;
PROCESSINFO procinfo;
CreateProcess(NULL,”calc.exe”,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,& stinfo, & procinfo);
3. ShellExecute(handle,”open”,”cdplayer.exe”,NULL,SW_SHOWNORMAL);
Eg: 浏览C:/windows文件夹
ShellExecute(handle,”explore”,”c:/windows”,NULL,NULL,SW_SHOWNORMAL);
■ DLL中的导出函数的明确申明:
__declspec(dllexport) int Fun(int x);
应用程序中明确相应输入函数:
__declspec(dllimport) int Fun(int x);
■ DLL实例:
//DLL01.cpp(dll建立)
extern "C" _declspec(dllexport) void ShowMessage()
{
AfxMessageBox("The function is in the MFC Dll");
}
//调用
HMODULE hdll=::LoadLibrary(“D:/vc/DLL01/Debug/DLL01.dll”);//加载链接库
if(hdll==NULL){
MessageBox(“加载动态链接库失败”,"Warning",MB_OK|MB_ICONWARNING);
return;}
typedef void (*pShow)(); //类型、参数要符合dll函数
pShow Show=(pShow)::GetProcAddress(hdll,"ShowMessage");
if(Show==NULL)
MessageBox(“The function can’t be found!”,”Warning”,MB_OK|MB_ICONWARNING);
else
Show(); //调用加载动态链接库失败
FreeLibrary(hdll);
■ 打开文件对话框选择文件路径
CString filter,m_Path;
filter="Dll(*.dll)|*.dll|All Files(*.*)|*.*||";
CFileDialog dlg(TRUE,".cpp","dll01.cpp",OFN_HIDEREADONLY,filter);
if(dlg.DoModal()==IDOK)
m_path=dlg.GetPathName();
UpdateData(false);