Com (Component Object Model, Component Object Model) is a binary and network standard created by Microsoft, as well as Microsoft to promote and have been widely recognized. In the COM standard, the COM object is well encapsulated. The customer cannot access the details of the object, providing the unique access path to the user to access through the COM interface. There are two meanings for the COM interface: first it is a set of functions that can be modified, whereby the customer can make certain things; second, it is also more important, the interface is between the components and their clients. protocol. In other words, the interface not only defines what function available, and it defines what the object is to do when the function is called. The Windows operating system itself is a large COM component object, as well as some necessary COM interfaces to the client, so we can program the Windows housing directly through these COM interfaces.
There is a little affirmation before the program is officially written: The COM interface needs to be used in the program. To operate the COM object. So first add to the code initialization COM and terminate the COM. Generally, the initialization COM and terminate COM code are usually added to the beginning of the application class's initInstance () function:
... Coinitialize (NULL); // Initialization com ... counitialize (); // Terminate COM code ...
The above two functions can be used well in the MFC program and non-MFC programs. In addition, if the program framework is based on an MFC, then simply call the AFXoleinit () function can achieve the same purpose. And don't have to explicitly call the code for terminating the COM. In the COM standard, the only way to access the COM object is a COM interface, so the COM interface is first available in writing a WINDOWS system shell. The COM interface used is iShellDispatch, which is derived from the IDispatch interface. There is definition in the VC98 / include / EXDISP.H header file of the VC installation directory. The following is some interface definitions that will be used:
! ...... EXTERN_C const IID IID_IShellDispatch; #if defined (__ cplusplus) && defined (CINTERFACE) interface DECLSPEC_UUID ( "D8F015C0-C278-11CE-A49E-444553540000") IShellDispatch: public IDispatch {public: ...... virtual HRESULT STDMETHODCALLTYPE MinimizeAll (void) = 0; virtual HRESULT STDMETHODCALLTYPE UndoMinimizeALL (void) = 0; virtual HRESULT STDMETHODCALLTYPE FileRun (void) = 0; virtual HRESULT STDMETHODCALLTYPE CascadeWindows (void) = 0; virtual HRESULT STDMETHODCALLTYPE TileVertically (void) = 0; virtual HRESULT STDMETHODCALLTYPE TileHorizontally (void) = 0; virtual HRESULT STDMETHODCALLTYPE ShutdownWindows (void) = 0; virtual HRESULT STDMETHODCALLTYPE Suspend (void) = 0; virtual HRESULT STDMETHODCALLTYPE SetTime (void) = 0; virtual HRESULT STDMETHODCALLTYPE TrayProperties (void) = 0; virtual HRESULT STDMETHODCALLTYPE Help (void) = 0; Virtual HResult stdmethodCallType FindFiles (Void) = 0; Virtual HResult stdmethodCallType FindComputer (void) = 0;}; ...... This interface will get pointers to their pointers when creating a COM object, through this function customer Program can avoid significant The same type is derived. In fact, this function also calls the COGETClassObject () function to get the COM object class, but it is encapsulated by the process of creating objects through the class factory, just specify the CLSID of the object class and to output The interface pointer and interface ID, apparently created a COM object is very convenient. After getting the COM object pointer, you can access the method of calling the COM object through this pointer to implement the various functions of the Windows shell. Realize some of the key code for this function:
... hResult sc; // Return the result ishelldispatch * pshelldisp = null; // Initialization interface pointer // Directly create a COM object sc = COCREATEINSTANCE (CLSID_SHELL, // Specify the COM object identifier null to be created, // Specify when aggregated The external object interface pointer clsctx_server, / / Specify the component category, you can specify the process within the process. Id_idispatch, // specify the interface ID, you need to pay attention to the COM created to be // The interface ID of the object, not the interface identifier (lpvoid *) & pshelldisp of the class object; // The interface pointer of the object returned to the function returned / * In the above code, COCREATEINSTANCE first calls the CogetClassObject function to create a class factory object, and then use The interface pointer of the obtained class factory object creates a real COM object, and finally release the class object object and returns so that it is very easy to put the class factory, which is easier to use. * / if (failed (sc)) // must use Failed or SUCCECED to determine if the COM object creates a success Return; pshelldisp-> findFiles (); // Call the method in the COM object pshelldisp-> release (); // release The application-connected pointer is called the FindFiles () method of the COM object to perform the system shell operation of the lookup file by PshellDisp interface pointer. Similarly, the corresponding housing operation can be performed according to the actual needs of the actual needs to be flexible, mainly with the following methods:
Minimizeall all window minimization
UndominimizeAll recovery window minimization
Filerun Start menu "Run ..."
CascadeWindows laminated window
TileVertically vertical tile
TILEHORIZONTALLY horizontal tile
ShutdownWindows Close Windows
SUSPEND hangs computer
Settime setting time
TRAYPROPERTIES Task Bar Properties
Help Windows Help
Findfiles lookup file
FindComputer lookup computer
......
These interfaces are defined in the VC98 / Include / EXDISP.H header file of the VC installation directory, which can be written in the case where the response is written.