Author: Li Xiaofei
Download the sample project in this article
I. Chatting
First of all, I am sorry to my friends who have not had time to reply to your questions here. This is a very busy time. I am so happy! Haha! Well, let's move on to the subject. Today we are going to talk about COM, which is a little deeper. I don't know how to use C ++ test or visual assistant (you can download it from the VC Knowledge Base tool column) none of the software, they all have a very striking feature, that is, they are embedded into the VC development environment. I am obsessed with this feature for only one reason: I want to create a VC engineering parser that can be embedded in the VC development environment (VC/Delphi engineering parser has been included in the online magazine 19th of the VC knowledge base ), in this way, the user can directly perform various analyses and statistics on the current or all projects in the VC development environment. Is it easy to implement it? Simple: Can next and copy be done easily? No, it is backed by the extensive and profound support of COM. Whether difficult or not, let's try it first.
II,
3. Implementation steps:
<3.1> Create<Devstudio add-in Wizard>Type project, enter the project name "codeanalyser ".
<3.2> On the second screen, you are required to enter the plug-in name and description. Users are required to select
Whether to generate a toolbar and whether to automatically add VC Event Response Code.
<3.3> click "finish" to end the wizard and enter the code editing window.
Here we want to say that the project references the icommands interface and sends the ccommands class from this interface. This class completes message response for all user-defined function interfaces, VC application message responses, and VC debugging actions. Before we add a member function for the ccommands class, we must first add the corresponding function interface declaration for the icommands interface. In this project, I added two function interfaces for the icommands interface. Their names are:GetcurdircommandmethodAndQuitcommandmethodThe statement is as follows:Codeanalyer. odlFile)
Interface icommands: idispatch
{
// Methods
[ID (1)] // function index number in vtable
Hresult getcurdircommandmethod (); // obtain the current working directory of the VC.
[ID (2)] // function index number in vtable
Hresult quitcommandmethod (); // exit the VC editor.
};
To add an interface function to the interface icommands, we must declare and implement the icommands interface function in the class ccommands. The internal code of the function is no different from that of the common engineering code.
// Implement (ccommands class Internal interface function declaration)
Public:
Stdmethod (getcurdircommandmethod) (this );
Stdmethod (quitcommandmethod) (this );
// Function Code (ccommands class Internal interface function implementation)
// Obtain the working directory of the Current VC development environment [You can also make it the functional code you want to implement]
Stdmethodimp ccommands: getcurdircommandmethod ()
{
Afx_manage_state (afxgetstaticmodulestate ());
Verify_ OK (m_papplication-> enablemodeless (variant_false ));
BSTR bstrcurdir;
M_papplication-> get_currentdirectory (& bstrcurdir );
Cstring STR (bstrcurdir );
: MessageBox (null, STR, "VC working directory", mb_ OK | mb_iconinformation );
Verify_ OK (m_papplication-> enablemodeless (variant_true ));
Return s_ OK;
}
// Exit the VC development environment
Stdmethodimp ccommands: quitcommandmethod ()
{
Afx_manage_state (afxgetstaticmodulestate ());
Verify_ OK (m_papplication-> enablemodeless (variant_false ));
If (: MessageBox (null,
"Do you want to exit the VC ++ Editor (y/n )? ",
"Inquire information ...",
Mb_yesno | mb_iconquestion) = idyes)
M_papplication-> quit ();
Verify_ OK (m_papplication-> enablemodeless (variant_true ));
Return s_ OK;
}
& Lt; 3.4 & gt;Create a toolbar and connect to the toolbar button event
All the behind-the-scenes work is ready, and only a toolbar interface is ready. Open the class cdsaddin, which contains three member functions, whereOnconnectionAndOndisconnectionThe significance of member functions is very important. Their meanings are as follows:
<1>Onconnection:The initialization task of the plug-in is completed here. Such as the start of the COM Service, the creation of the toolbar/menu bar, and the addition and modification of the toolbar buttons/menu items.
<2>Ondisconnection:The uninstall of the plug-in is completed here. For example, unmount the COM Service, destroy and release the toolbar/menu bar, and so on.
After understanding their respective purposes, we can add code to the corresponding message events. Obviously, the toolbar should be initialized inOnconnectionEvent completed.
InOnconnectionIn the event, the system first obtains the VC application interface, and then calls an interface function: addcommand to add commands and command shadow functions for the plug-in. Then, use another interface function addcommandbarbutton to add a toolbar button to the toolbar. Each toolbar button is connected with a command flag, so that buttons and commands (messages) can be implemented). Below isCode for adding a command and a toolbar button (if you want to add multiple toolbar buttons, just repeat this step):
Lpctstr szcommand = _ T ("getcurdircommand ");
Variant_bool Bret;
Cstring strcmdstring;
Strcmdstring. loadstring (ids_cmd_string );
Strcmdstring = szcommand + strcmdstring;
Ccombstr bsz1_string (str#string );
Ccombstr bszmethod (_ T ("getcurdircommandmethod "));
Ccombstr bszcmdname (szcommand); // corresponds to the Add toolbar button below
Verify_ OK (papplication-> addcommand (bsz1_string, bszmethod, 0, dwcookie, & BRET ));
// Description of the addcommand parameter:
// Bsz#string: command string.
// Bszmethod: The icommands interface function name.
// The third parameter represents the bitmap offset.
// The decibels of the fourth and fifth parameters are system parameters and return values (refer to the iapplication introduction of msdn)
If (Bret = variant_false)
{
* Onconnection = variant_false;
Return s_ OK;
}
// Add a toolbar button
If (bfirsttime = variant_true)
{
Verify_ OK (papplication-> addcommandbarbutton (dsglyph, bszcmdname, m_dwcookie ));
}
& Lt; 3.5 & gt;Compile, connect, and introduce plug-ins in VC
The above is all our code work. Please build the following quickly. After compilation, there will be a DLL file under the DEBUG directory of your project. Open the VC editor and right-click any tool bar of VC. The menu shown in is displayed. Select the Customize sub-menu to open the toolbar customization window, as shown in:
Select "add-ins and macro files" on the last page of the window. The window shown is displayed.
Click the "Browse..." button to open the DLL file in the DEBUG directory of your project, so that you can see the toolbar you created. In the same way, you can open the above menu again. This time you can see that there is an additional toolbar, And the name is messy. How can you change the toolbar name? The method is simple: Open the "toolbars" option page in the window above, find your toolbar in the toolbar list box, and enter your desired name in the "toolbar name" edit box. Open the menu above to see if the name has changed. Haha!
OK. Let's talk about this topic today. It's still the old rule. If you have any questions, please mail it to me and wish you a pleasant learning. Bye!