Start and close another program from a program

xiaoxiao2021-03-06  82

The program you are writing today is a plugin. The plug-in is actually a dynamic link library, which can be loaded into the memory and call the function written in the plugin according to the main program. Since the plug-in is subject to the main program, I must additionally provide a program that can extend the plugin. So, I wrote a more complex program to complete more features, but in order to make this program that can appear to be integrated with the plugin, you must start the plug-in process, Also start me later written procedures, and you have to close the program I wrote before before the plugin main program is turned off. To achieve this, shellexecuteEx () and FindWindow () were used. Shellexecute () and shellexecuteEx () are designed to start a program through the system. In order to perform the program correctly, you have to specify the correct directory and program name for SHELLEXECUTE () and shellexecuteEx (). Here is an example of using ShellExecuteEx: SHELLEXECUTEINFO ShExecInfo; ShExecInfo.cbSize = sizeof (SHELLEXECUTEINFO); ShExecInfo.fMask = NULL; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; ShExecInfo.lpFile = _T ( "C: // MyProgram .exe "); the implementation of the program name ShExecInfo.lpParameters // = NULL; ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_MAXIMIZE; // this program displays a full-screen ShExecInfo.hInstApp = NULL; ShellExecuteEx (& ShExecInfo); if ShellExecuteEx () No correct, getLastError will help you find the problem. If SHELLEXECUTEEX is correct, then this function will return true. To close a program, we can find this window via FindWindow (), then send a closing message to the window, you can. The problem is how to find the correct window. HWnd FindWindow (LPCTSTSTR LPCLASSNAME, // Class Name LPCTSTR LPWINDOWNAME // WINDOW NAME); the two parameters of FindWindow can help you locate a window. If you really know the window name of a window, you can use it. HWND HWND = :: FindWindow (NULL, _T ("NOTEPAD")); if (null! = Hwnd) {:: seundMessage (hwnd, wm_close, 0, 0);} If the title of the window is variable, then To use the class name of the window. Class names can be obtained by using SPY . If this window is written by yourself, you have to register a well-known window class name, register this window before creating a window. Bool CMYWND :: Create (DWORD DWSTYLE, CRECT & RECT, CWND * PParent, Uint Nid) {WNDCLASS WNDCLS; Hinstance Hinst = AFXGETITINSTANCEHANDLE (); 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);}

转载请注明原文地址:https://www.9cbs.com/read-95543.html

New Post(0)