COM (Component Object Model, Component Object Model) is a binary and network standard created by Microsoft. It is also a component standard that Microsoft has vigorously promoted and widely recognized. In the com standard, the COM object is well encapsulated, and the customer cannot access the implementation details of the object. The only access channel provided to the user is access through the COM interface. The COM interface has two meanings: first, it is a group of functions that can be called, so that the user can let the object do something. Second, it is more important, interfaces are protocols between components and their customer programs. That is to say, the interface not only defines the available functions, but also defines what the object will do when these functions are called. As a large COM component object, the Windows operating system also provides some necessary com interfaces to the client program. Therefore, we can use these com interfaces to program the Windows Shell directly.
Before the program is formally written and designed, it is certain that the COM interface must be used in the program to operate the COM object. Therefore, you must first add the code for initializing and terminating COM. Generally, the initialization COM and termination com code are added at the beginning of the initinstance () function of the application class and before the return:
...... Coinitialize (null); // initialize com ...... Couninitialize (); // terminate the com code ...... |
The above two functions can be used well in both the MFC program and the non-MFC program. In addition, if the program framework is based on MFC, you can simply call the afxoleinit () function to achieve the same purpose. And you do not need to explicitly call the code to terminate COM. In the com standard, the only way to access the COM object is the COM interface. Therefore, you must first obtain the COM interface provided by the Program for operating the Windows system shell. The COM interface used is ishelldispatch, which is derived from the idispatch interface and is in the vc98/include/exdisp directory of the VC installation directory. the H header file has definitions. Some interface definitions to be used are excerpted below:
...... 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 a pointer to the COM object when the cocreateinstance () function creates a COM Object. Through this function client program, you can avoid dealing with similar factories explicitly, in fact, this function also calls the cogetclassobject () function internally to obtain the class factory of COM objects, but it encapsulates the process of creating objects through the class factory, you only need to specify the CLSID of the object class and the interface pointer and Interface ID to be output. Obviously, it is very convenient to directly create a COM object, after obtaining the COM Object Pointer, you can use this pointer to access methods in the COM object to call various functions of the Windows Shell. The following is some key code to implement this function:
...... Hresult SC; // return the result Ishelldispatch * pshelldisp = NULL; // initialize the interface pointer // Directly create a COM Object SC = cocreateinstance (clsid_shell, // specifies the COM object identifier to be created Null, // specifies the interface pointer of the external object to be aggregated Clsctx_server, // specifies the component category. You can specify components outside the process or control objects in the process. Iid_idispatch, // specify the interface ID. Note that // The Interface ID of the created COM Object, not the interface identifier of the class factory object (Lpvoid *) & pshelldisp); // stores the interface pointer of the object returned by the function/* In the above Code, cocreateinstance first calls the cogetclassobject function to create a class factory object, and then uses the obtained class factory object interface pointer to create a real COM object, finally, release the class factory object and return it. This effectively shields the class factory and makes it easier for users to use it. */ If (failed (SC) // you must use failed or succecced to determine whether the COM object is successfully created. Return; Pshelldisp-> findfiles (); // call the methods in the COM Object Pshelldisp-> release (); // release the requested interface pointer ...... |
Here, the pshelldisp interface pointer is used to call the findfiles () method of the COM object to find the system shell of the file. Similarly, you can flexibly call the response method to execute shell operations as needed, mainly including the following methods:
Minimizeall: minimize all windows
Undominimizeall: minimized recovery window
"Run..." in the filerun Start Menu... "
Cascadewindows cascade window
Tilevertically vertical Tile
Tilehorizontally horizontal Tile
Shutdownwindows Shut Down Windows
Suspend hangs the computer
Settime
Trayproperties taskbar Properties
Help Windows Help
Findfiles
Findcomputer
......
These interfaces are defined in the vc98/include/exdisp. h header file in the VC installation directory. You can compile the response shell operation code by viewing the file.