Write the Sample of MMC Snapin with ATL

zhaozj2021-02-17  63

MMC related programming interface MMC implements the following interfaces: IColumnData (new in MMC 1.2) IConsole2 IConsoleNameSpace2 IConsoleVerb IContextMenuCallback IContextMenuProvider IControlbar IDisplayHelp IHeaderCtrl2 (new in MMC 1.2) IImageList IMenuButton IPropertySheetCallback IPropertySheetProvider IRequiredExtensions IResultData IToolbar Snap-ins implements the following interfaces: IComponent IComponentData IEnumTASK IExtendContextMenu IExtendControlbar IExtendPropertySheet2 IExtendTaskPad IRequiredExtensions IResultDataCompare IResultDataCompareEx (new in MMC 1.2) IResultOwnerData ISnapinAbout ISnapinHelp2IComponent interface represents the results of the column on the right (result pane) in an object, the object is to use this interface MMC communication needs and the results column; IComponentData interface represents The object in the Scopet PANE (Scopet PANE) (these objects can be called node), the MMC needs to use this interface when communicating with objects in the directory bar; IEXTendContextMenu interface, whether the user right-click the node on the left ( Scope PANE ITEM or the Right Item, MMC pops up a default association menu, and MMC will use this interface to ask the object (Item), do you need to add your own menu item. IEXTendControlbar and IextendpropertySheet2 are also similar. IsnapINabout implements an interface on dialog boxes, MMC understands Snapin's version, description information, provider information, dialog icon through this interface, and is particularly important that MMC has obtained all nodes of Snapin through this interface. The default icon, the icon to modify the root node can also be provided to the MMC through this function getStaticFolderImage. For a detailed description, please refer to Platform SDK. Snapin does not need to implement all interfaces, SAMPLE only implemented: IComponent IComponentData IextendContextMenu IExtendControlbar iextendPropertySheet2 ISNAPINABOUT MMC ATL Implementation Support Class Introduction Samples Model ATL implements almost all interfaces, providing the default processing code. The code generated by ATL WIZZARD provides basic programming framework, and SNAPIN features depends on the overload of certain functions of CMYSNAPINDATA to modify the default behavior. In the above figure, the base class IExtendContextMenuimpl, IEXTendContextPropertySheet, which relies on several of CMYSNAPINDATA, but only because the line is too chaotic.

Snapin DLL Sample Generates STEP BY Step1, New ATL Project, if you need to use the MFC, you can use the wizzard option to support the MFC project named6; 2, "New ATL Object", select "MMC Snapin"; 3, name "mysnapin", and the "MMC Snapin" second property page, select IextendContextMenu, IextendControlbar, IextendPropertySheet, IsnapinAbout; and removing the support for persistence; 4, after determining, ATL Wizzard generated CMySnapin us, CCMySnapinComponent, CMySnapinData, CMySnapinAbout, CMySnapinPage ; 5, compile; then run MMC.exe, add a management unit "mysnapin", saving as mysnapin.msc; code introduction CMYSNAPIN implements IComponentData, IExtendContextMenu, IEXtendPropertySheet three interfaces. CMYSNAPINComponent implements IComponent, IextendContextMenu, IExtendControlbar, IExtendpropertySheet four interfaces, CMYSNAPINABOUT implements the ISNAPINABOUT interface. DLL exposes both CMYSNAPIN and CMYSNAPINABOUT to the outside. So MMC can find ISnapinAbout and IComponentData, IExtendContextMenu ,, IExtendPropertySheet three interfaces, and MMC objects created by CreateComponent CMySnapinComponent IComponentData method, which in turn provides CMySnapinComponent of IComponent, IExtendContextMenu, IExtendControlbar, IExtendPropertySheet four interfaces. As can be seen from the figure, the ATL interface implementation class implements the default behavior of the partial method, and then passes several important methods such as Notify, such as NOTINDATA objects, and continue to process. CMYSNAPINDATA inherits from CSNAPiniteMPL, CSNAPinItem IMPL implements a CSNAPinItem interface that defines all the behaviors of the ITEM object (the left node (Scope Pane Item). CMYSNAPIN creates CMYSNAPINDATA in the constructor as root point. All behaviors of an ITEM can modify the default behavior by overloading certain methods. For example, add a few child nodes below the root node, you can process MMCN_EXPAND messages in the overloaded Notify method.

MMC Snap-in Wizardmmc Snap-in Wizard in MSDN told MMC Snap-in FAQ: 1. How do I add a Toolbar Resource to the Snap-in Object? 2. How do I add menu items, Control Bar Buttons, and A Property Sheet To The Snap-In Object? 3. How do I Enumerate The child? 4. How do i add custom- ney Item type to the Snap-in Object? Please refer to MSDN for your own content The above method can make a complete function of Snapin. However, very unfortunately, ATL Wizzard is provided whether it is a menu, toolbar or property page is all Item common, whether Scope Item or Result Item. Especially when there are multiple different types of nodes, this is almost uncomfortable. How to generate a sub-node (Scope Item) on demand custom Item? After the code framework is generated in accordance with the above steps, run mysnapin.msc can see that there is only one root node, the name is mysnapin. Modify the name of this root point, only need to modify the const olechar * cmysnapindata :: m_szdisplay_name = OLESTR ("MySnapin") in mysnapin.cpp file; this line can be made. As mentioned earlier, the ATL puts the item abstraction into a CSNAPinitem interface, and the CSNAPiniteMimpl class implements most of the default behavior of this interface, so all Item should inherit from this class. Considering CMYSNAPINDATA as our root node, ATL has generated some code for us, from CMYSNAPINDATA inheritance is a better choice. (Recounted code actually only the initialization code in the CMYSNAPINDATA constructor, but the greatest advantage of CMYSNAPINDATA is to place a public implementation in the CMYSNAPINDATA class without the need for each Item class to implement one of the same code.) This article will inherit from the CMYSNAPINDATA class, as well as the CMYSNAPindata will be implemented as a base class, so we need another root point. Next, we will generate three primary sub-nodes, which are similar to "Services", "Event Viewer", "Account Management", and then regenerate into a secondary subtock in "Event Viewer" and "Account Management". Basically contain custom menus, attribute pages, toolbars, etc. Inherited from CMYSNAPINDATA, generating CiteMroot, CServicesContainer, CeventsContainer, CaccountsContainer. Note that in the mysnapin.h file, join a line of #include "test6.h". In CMYSNAPIN's constructor, we change the root node to Citemroot. In the Citemroot class, overload the Notify method, add the code to process MMCN_expand, which gives us the opportunity to add a child node (Scope Item).

case MMCN_EXPAND: {CComQIPtr spConsoleNameSpace (spConsole); m_pServicesContainer-> m_scopeDataItem.mask | = SDI_PARENT; m_pServicesContainer-> m_scopeDataItem.relativeID = param; spConsoleNameSpace-> InsertItem (& m_pServicesContainer-> m_scopeDataItem); m_pEventsContainer-> m_scopeDataItem. mask | = SDI_PARENT; m_pEventsContainer-> m_scopeDataItem.relativeID = param; spConsoleNameSpace-> InsertItem (& m_pEventsContainer-> m_scopeDataItem); m_pAccountsContainer-> m_scopeDataItem.mask | = SDI_PARENT; m_pAccountsContainer-> m_scopeDataItem.relativeID = param; spConsoleNameSpace-> InsertItem (& m_pAccountsContainer -> m_scopedataItem); hr = s_ok;} Break; how to generate the result bar inheritance from CMYSNAPINDATA, generate a CEVENTENTRY class, representing an event content. Modify the mmcn_show message of the Notify method in the CEVENT class, which gives us an opportunity to add Result Item. How to modify the name of the child node? Each node is inherited from CMYSNAPINDATA, and its parent class csnapinitemimpl has a member variable ccombstr m_bstrdisplayName. MMC calls iComponentData or iComponent's getDisplayInfo to get display information, including node names, getDisplayInfo passes calls to getScopePaneInfo or getResultPaneInfo functions, getScopePaneInfo and getResultPaneInfo function defaults to M_BSTRDISPLAYNAME. Therefore, as long as the value of M_BSTRDISPLAYNAME is modified in the constructor. How to modify the icon of the child node? Implement the mmcn_add_images message of the Notify method in CMYSNAPINDATA, here we load all the icons. Such other Item classes do not need to implement this part of the code, just process the NOTISNAPINDATA's NOTIFY method to proceed.

For each Snapin, there is a IImageList interface for use, MMC uses this icon list to save all the icons you need to use, and each item only needs to indicate the index of its icon list: m_scopedataItem.nimage = 2 m_scopedataItem.nopenImage = 2; But the icon in the Scope column is not loaded by the MMCN_ADD_IMAGES message, but must first load all the necessary icons in the initialize method of the CMYSNAPIN. Also, the above method does not work on the icon of root nodes. The modification of the icon of the root node must be modified by modifying the CMYSNAPINABOUT GetStaticFolderImage method. You can also add an icon to the About dialog box. How to modify the default display of the child node in the result column? Scope Item only displaced a name in the Result PANE. For example, when selecting "MySnapin", the default behavior is displayed in the Result PANE. Below we let the Event Viewer display more detailed information. MMCN_SHOW modified message in the Notify method CEventsContainer: case MMCN_SHOW: {CComQIPtr spResultData (spConsole); spHeader-> InsertColumn (0, L "log", LVCFMT_LEFT, 100); spHeader-> InsertColumn (1, L "Description", LVCFMT_LEFT, 200); HR = S_OK;} Break; then overloads the GetResultPaneColinfo method in CEVENT, providing a display string of each column. Since getResultPaneColinfo is a non-virtual function, we have to reload the getResultPaneInfo function in CEVENT. Or modify the CMYSNAPANECOLINFO function of CMYSNAPANECOLINFO Functions, then you can override GetResultPaneInfo. How to add your own unique menu to a child node? Since CSNAPinItem IMPL is implemented in a template, the Item class inherited from CMYSNAPINDATA will need to over-dash the corresponding processing functions without maximizing the implementation of CSNAPinItemImpl. So, we let CService, such as more features, are inherited from CSNAPINITEMIMPL . Add a "Start" project to the Services project. 1. Add menu IDR_MYSNAPIN_MENU1 to menu resources, add menu item "Start", "Stop" to the first drop-down menu, if you need on other menu items, you can also add it to another drop-down menu; 2, use macro SNAPINMENUID (IDR_MYSNAPIN_MENU1) to tell the resource ID ATL menu; 3, using a macro BEGIN_SNAPINCOMMAND_MAP (CService, FALSE) SNAPINCOMMAND_ENTRY (IDM_START, OnStart) SNAPINCOMMAND_ENTRY (IDM_STOP, OnStop) END_SNAPINCOMMAND_MAP () receiving a command message.

4 and use HRESULT OnStart (Bool & BhandLled, CSnapinObjectRootBase * Pobj); HRESULT OnStop (Bool & BhandLled, CSNAPNObjectBase * Pobj); 5, if necessary, save the UpdateMenustate function to modify the menu status; how to add your own unique toolbar to a child node? Add the "Start" project to the Services project, "Stop" toolbar button. 1, add the toolbar to the resource, add button "Start", "Stop"; 2, use the macro begin_snapintoolbarid_map (cservice) SnapintoolBarid_Entry (idR_toolbar1) end_snapintoolbarid_map () to receive command messages. 3, if necessary, overload the UpdateToolbarbutton function modify the button status; Add a property page to the Account. 1. Overload the Notify method in the Caccount class. 2, in the processing method MMCN_SELECT Notify message: CComPtr spConsoleVerb; if (! SpConsole-> QueryConsoleVerb (& spConsoleVerb) = S_OK) {ATLTRACE ( "==> Query ConsoleVerb faild./n"); return S_FALSE;} else {SpConsoleverb-> setverbstate (MMC_VERB_PROPERTIES, ENABED, TRUE); HR = S_OK;} 3, overload the CreatePropertyPages, QueryPagesFor method, create the property page, you can use ATL WIZZARD to generate CMYSNAPInPage, you can customize a similar class. 4. If necessary, you can issue an attribute change in the onapply function of the property page to notify Caccout; 5, Caccout updates its own display accordingly. Precautions: lParam SCOPEDATAITEM / RESULTDATAITEM structure, special use of lParam ATL has special significance in ATL, IComponentImpl and IComponentDataImpl of GetDisplayInfo functions assume that lParam is CSnapInItem objects SCOPEDATAITEM / RESULTDATAITEM structure, therefore, the constructor of CMySnapinData there m_scopeDataItem.lParam = (LParam) This; and m_resultdataitem.lparam = (lparam) this; therefore, if our Item class does not inherit from CMYSNAPINDATA, then do these work.

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

New Post(0)