Create a COM component with ATL, create a model (engineering) MyProj. Second, add a component Mycom to the model (project). Third, to the component increase method (functions) myf1, myf2, myf3, myf4.
I. Creating a model (Project) MyProj In the VC 6.0 work platform, click the New menu item under the menu file, select the Projects card in the New dialog box, select the ATL COM AppWizard in the list box, select the ATL COM AppWizard (Event Template Components navigation).
Enter a project name such as MyProj in the Project Name Editing box and select the appropriate location, press the confirmation button to enter the next dialog: ATL COM Appwizard - Step 1 of 1, select Dynamic Link Library [DLL] in Server Type, In-process server, this is the fastest component. Select the Support MFC selection. After pressing the finish and ok buttons, the framework of a component has been established.
Second, adding the component mycom in VC menu in Insert, the ATL Object Wizard dialog box appears. Select Objects in the category on the left, select the Simple Object item in the Objects on the right. Press the Next button.
In the SHORT NAME editing box in the eight editing box in the ATL Object Wizard property dialog box, enter a short name such as Mycom, and the content of the other seven edit boxes will be generated automatically. Then press the confirmation button to exit.
Third, to the component increase method (function) Myf1, myf2, myf3, myf4 find the interface iMycom item in the ClassView card on the left side of the VC work platform, press the right button, select Add method in the shortcut menu, Add method ... To interface dialog box, enter the function name, parameter, and return value type you want to add in the dialog box. Then, press the confirmation button to exit.
First increase the function myf1: function name: myf1 parameter: no
Insert code in mycom.cpp file: stdmethodimp cmycom :: myf1 () {afx_manage_state (AfxgetStaticModuleState ())
// Todo: Add Your Implementation Code Here AfxMessageBox ("Welcome to My Components"); Return S_OK;}
Use the same method to add function myf2: function name: Myf2 parameter is: [in] BSTR STR, [OUT, RETVAL] INT * VAL
Insert code: stdmethodimp cmycom :: myf2 (bstr str, int * val) {AFX_MANAGE_STATE ())
// Todo: Add Your Implementation Code Here CSTRING SSTR (STR); int N = sstr.getlength (); * VAL = n; Return S_OK;
Use the same method to add functions to the component Myf3: function name: Myf3 parameters are: [in] BSTR STR, [OUT, RETVAL] BSTR * RETSTR
Insert Code: STDMETHODIMP CMyCom :: MyF3 (BSTR str, BSTR * retstr) {AFX_MANAGE_STATE (AfxGetStaticModuleState ()) // TODO: Add your implementation code here CString sStr (str); CString sRetstr = "components receive your message: < " SSTR "> / n here. "; ccombstr temp (sretstr); * RetStr = Temp; Return S_OK;
Use the same method to add function myf4: function name: Myf4 parameter: [in] int x, [out, return] int * VAL
Insert code: stdmethodimp cmycom :: myf4 (int x, int * val) {AFX_MANAGE_STATE (AFXGETSTAICModuLestate ())
// Todo: add your importation code here * val = x 1; return s_ok;}
Cut the project, generate the component DLL. If the above three steps are completed on the Win2K or WinXP computer, the component's DLL file is generated in the debug subdirectory, and automatic registration is completed. But on the Win98 computer, registration is to run as follows command to complete: Regsrv32 c: /myproj/debug/myproj.dll
Create a COM Component Customer 1, create a dialog-based client. 2. Import the server type library into the customer work platform. 3, initialize the COM library. 4. Get the GLSID of the server. 5. Create an instance of a COM component server component. 6, use COM objects. 7. Terminate the COM library.
1. Create a customer program with MFC AppWizard (exe) to create a dialog-based application Myexe. Place four buttons in the dialog, named myf1, myf2, myf3, and myf4. Generate the four response functions of the click button with ClassWizard ONMYF1 (), ONMYF2 (), ONMYF3 (), and ONMYF4 ().
2. Import the server type library into the customer 2-1 Add code in the stdafx.h file:
#import "../myproj/myproj.tlb"
The final stdafx.h file is as follows: // stdafx.h: include file for standard system incrude files, // or project specificic include files That Are used frequently, but // area change infrequently //
#if! defined (AFX_STDAFX_H__2B646017_28AD_4CDE_9792_CB8F9A5C6B39__INCLUDED _) # Define AFX_STDAFX_H__2B646017_28AD_4CDE_9792_CB8F9A5C6B39__included_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
#define vc_extralean // Exclude Rarely-usest Stuff from Windows Headers
#include
// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.
#ENDIF /! Defined (AFX_STDAFX_H__2B646017_28AD_4CDE_9792_CB8F9A5C6B39__INCLUDED_)
Compile STDAFX.CPP, which generates the type library header file (.tlh) and type library implementation file (.tli) of the component in the client's Debug directory.
2-2 Use the namespace above the source file using the component, and the last source file is as follows.
// myexedlg.cpp: Implementation file //
#include "stdafx.h" #include "myexe.h" #include "myexedlg.h"
#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
USING NAMESPACE MyProjlib;
The following code is slightly
3, initialize the COM library HRESULT HR = Coinitialize (NULL);
4. Get the CLSIDCLSID CLSID (HR = CLSIDFromProgid (Olestr ("MyProj.mycom"), & clsid;
IF (Failed (HR))
{
AfxMessageBox ("com failed");
Return;
}
5. Create a COM server component instance, get component interface pointer Imycom * ptr = null; hr = cocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __UIDOF (iMycom), (lpvoid *) & PTR);
6-1, use COM objects in Onmyf1 () MYF1 ()
Ptr-> myf1 (); ptr-> release (); // release instance
6-2, using COM objects in OnMYF2 () MYF2 () char STR [32];
Sprintf (STR, "LEN =% D", PTR-> Myf2 ("AbcDefg"));
AfxMessageBox (STR);
Ptr-> release (); // release example
6-3, using COM objects in OnMYF3 () AFXMESSAGEBOX (Ptr-> Myf3 ("Abcdefg"));
Ptr-> Release (); // Release Example 6-4, using COM objects in OnMYF4 () MYF4 ()
INT x = 8; char STR [32];
Sprintf (STR, "X =% D, X 1 =% D", X, PTR-> myf4 (x));
AfxMessageBox (STR);
Ptr-> Release (); // Release Example 7, Terminate COM library couninitialize (); (end, to be continued)
Author Blog:
http://blog.9cbs.net/callzjy/