Lu Siwei - Pan Ai People :: ATL Type (4)

zhaozj2021-02-11  202

ATL introduction (four)

爱伟

(Received the previous period)

---- 4. Compiling the Connection, the case of COM service

---- Compiled with the ATL process includes several steps:

Use the IDL document of the MIDL to compile the process, shape the interface definition of the header and the code for the Marshalling. The .CPP document is shaped into a target file. Connect the target document, shape the application module. Question COM service program.

---- It is similar to that of the process of engineering and translation, which is similar to the MFC process in Visual C , and only focus on the COM service program.

---- In ATL, the COM service program is the final stage of the engineer compiler, and is completed by ATL assistance. In the COM programming program of the hand worker, the registration of the service program is more annoying. In ATL, the system is unfinished by reading a registration script that is formed in the construction process process. The Register Script, a briefed RGS) is an ATL's text-based registration auxiliary file. The lower surface is an example of the registration script file.

---- HKCR - Forms the registration item of the COM object in the registry, is the overcoming of HKEY_CLASS_ROOT

{

Authtest.ActiveXObject.1 = S'ActiveXObject Class'

{

CLSID = S '{97A5CB10-AF82-11D2-B9BC-00104B04B867}'

} - PROGID of the object

Authtest.ActiveXObject = s 'ActiveXObject Class'

{

CLSID = S '{97A5CB10-AF82-11D2-B9BC-00104B04B867}'

} - Passing with the version of PROGID

Noremove CLSID - Paragraphs CLSID Registration

{

FORCEREMOVE {97A5CB10-AF82-11D2-B9BC-00104B04B867}

= S 'ActiveXObject Class'

{

PROGID = s 'authtest.activeXObject.1'

VersionIndependentProgid = s 'authtest.activeXObject'

InprocServer32 = S '% module%'

- Service gear type, show DLL service

{

Val threadingmodel = s 'Both'

- Thread model, this is Both type

}

}

}

}

---- RGS file contains the contents of the registration COM service program, and us often do not have to modify this RGS file, and we can also modify the RGS file to customize the registration process of the module.

Fourth, an example of the ATL should be used --- on the top of the original process of creating a COM service program. In the introduction, we have actually produced a basic framework of a COM service program, just not filling in the actual content. At the lower part, we have issued a piece of simple COM service program, and write a customer code to test, so that the big family has a full overall solution for the process of opening the COM service using ATL.

---- The function of the service order we have to open is very simple. It is only an interface. This interface name is ISIMPLEINTERFACE. There is only one members of the interface, called Welcome. The function of this function is just a string of "Hello World!".

---- In the steps of creating COM service schemes, I will exercise as follows.

Open Visual C into an open environment. Create a ATL process called SimpleTest. Insert a new object in this engine, the name of the object is SimpleInterface. Set the configuation attribute of the interface isimpleInterface to make it a double interface. Addition method Welcome in ISIMPLEINTERFACE. Open the frame of the Welcome method added to the ATL to see the following code segment.

STDMETHODIMP CACTIVEXOBJECT:: GET_TESTPROP (long * pval)

{

AFX_MANAGE_STATE (AFXGETSTATICModuleState ())

// Todo: Add Your Implementation Code Here

Return S_OK;

}

---- 7. Replace the comment part in the program frame frame to the lower code:

:: Messagebox (Null, _T ("Hello World!"), _ T ("Welcome"), MB_OK.

A message box will pop up when the Welcome method is called.

---- 8 . Compile the connecting process.

---- After the steps on the upper surface, we have a simple COM service program, and have been included in the pre-system.

---- I have to finish a simple COM customer program. A COM customer program is simple to use the program of the COM component object. The basic process of the customer program calls COM object is as follows.

Creating an example of COM object. This can be completed by calling the API function cocreateInstance of the Windows system. Pass the interface to the interface. Put the COM object instance.

---- Our customer program is a simple-signed simple application program written by MFC. The specific process is as follows:

Open Visual C into an open environment. Create a dialog-based MFC process called SimpleClient. Add a button in the dialog box, the name is TEST. Add the following code in the SimpleClient.cpp file. ---- (1) Add the code after .CPP document #include "simpleclientdlg.h":

#include "d: /simpletest/simpletest_i.h"

// According to the path required to repair the finished file

---- The added header is self-generated in the compiled COM service program process, which includes the definition of the interface, the definition of the interface IID and the definition of the CLSID of COM object. The package contains the item to enable the customer to use the COM service program.

---- (2) Add the following code in the message control function of the button TEST:

HRESULT HR;

ISIMPLEINTERFACE * PINTF = NULL;

HR = CoCreateInstance

(CLSID_SIMPLEINTERFACE, NULL, CLSCTX_SERVER,

IID_ISIMPLEINTERFACE, (void **) & PINTF);

En (ac))

{

PINTF-> Welcome ();

PINTF-> Release ();

}

---- The first-faced code first passes through the system API COCREATEINSTANCE to create a COM object, get the pointer of the interface, then call the interface member function Welcome, and release the COM object instance by the iUnknown :: release () function.

---- 5. Compile the program.

---- The most, we can test the customer's program. Enable the customer program, when you click the Test button, we can see a message box, which is the function of our COM service program. (Full text)

---- (author address: Research "of Beijing University Science and Technology Research Institute, 100871; Received: 1999.08)

---- Responsible Editor: 许 x_jufang@ccw.com.cn

-

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

New Post(0)