ShellExecute()和ShellExecuteEx()被设计可以通过系统来启动一个程序。为了可以正确执行程序,那么就要为ShellExecute()和ShellExecuteEx()指定正确的目录和程序名。下面是一个使用ShellExecuteEx的例子:SHELLEXECUTEINFO ShExecInfo;ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);ShExecInfo.fMask = NULL;ShExecInfo.hwnd = NULL;ShExecInfo.lpVerb = NULL;ShExecInfo.lpFile = _T(“C:/MyProgram.exe“); // 执行的程序名ShExecInfo.lpParameters = NULL;ShExecInfo.lpDirectory = NULL;ShExecInfo.nShow = SW_MAXIMIZE; // 全屏显示这个程序ShExecInfo.hInstApp = NULL;ShellExecuteEx(&ShExecInfo);如果ShellExecuteEx()没有执行正确,GetLastError 会帮助你找到问题所在。如果ShellExecuteEx执行正确,那么这个函数会返回TRUE.要关闭一个程序,我们可以通过FindWindow()找到这个窗口,然后向窗口发送关闭消息,就可以了。问题就在于如何找到正确的窗口。HWND FindWindow( LPCTSTR lpClassName, // class name LPCTSTR lpWindowName // window name);FindWindow的两个参数可以帮助你定位一个窗口。如果你确实知道一个窗口的窗口名称,那么可以这样用。HWND hWnd = ::FindWindow(NULL, _T(“NotePad”));if (NULL != hWnd) ...{ ::SendMessage(hWnd, WM_CLOSE, 0, 0);}如果窗口的标题是可变的,那么就要利用窗口的类名。类名可以通过使用Spy++得到。如果这个窗口是自己编写的,就要注册一个好记的窗口类名,在创建窗口之前,注册这个窗口。BOOL CMyWnd::Create(DWORD dwStyle, CRect& rect, CWnd* pParent, UINT nID)...{ WNDCLASS wndcls; HINSTANCE hinst = AfxGetInstanceHandle(); LPCTSTR lpszClassName = _T(“MyWindow“); if(!(::GetClassInfo(hinst, lpszClassName,&wndcls))) ...{ //not yet,so register it wndcls.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW; wndcls.lpfnWndProc = ::DefWindowProc; wndcls.cbClsExtra = wndcls.cbWndExtra = 0; wndcls.hInstance = hinst; wndcls.hIcon = NULL; wndcls.hCursor = NULL; wndcls.lpszMenuName = NULL; wndcls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); wndcls.lpszClassName = lpszClassName; if(!AfxRegisterClass(&wndcls)) ...{ AfxThrowResourceException(); } } return CWnd::Create(lpszClassName, NULL, dwStyle, rect, pParentWnd,NULL));}/**//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////HWND hWnd = ::FindWindow(_T(“MyWindow“), NULL);if (NULL != hWnd) ...{ ::SendMessage(hWnd, WM_CLOSE, 0, 0);}