Beginner tutorial: ATLCOM simple project

zhaozj2021-02-16  57

Author: C Lung.

Original link:

http://www.codeproject.com/atl/com_atl.asp

Click here to download this article supporting source code

Introduction

The purpose of this tutorial is to tell you how to create a COM server using ATL and use the Visual C and Visual Basic programs to call this server. I don't want to explore the details of COM, and I don't want you to be in the IDL. This tutorial is only designed for VC 's novice programmer, telling them how simple is to create a COM object and let them produce more interest to ATL.

Step 1: Start ATL COM WIZARD

The first thing you need to do is to start Visual C and create a new project, select "ATL COM WIZARD", the project is "Simple_atl". After setting the path of the project, click the OK button. You will see, give you some options on the screen. The first option is "Server Type". We will create a server DLL, so make sure the server's type is selected as "Dynamic Link Library". We don't need to care about the other three check boxes below, so we can ignore them. Press the Finish button so that the wizard will generate the appropriate files. After that, a "New Project Information" window will appear, you can create any files created from above, and press "OK" to accept all.

Step 2: Create a new ATL object

Please confirm that you can see Workspace View in the VC IDE. If you can't, click the View menu and select "Workspace". In this view you will see three tabs, click the "ClassView" column, you should see "Simple_atl Classes". Right click here to click the mouse button and select "New ATL Object" in the pop-up menu, you will see the following window:

The default selection item "Simple Object" is what we want, please click the Next button, you will come to the "ATL Object Wizard Properties" window. Enter "first_atl" in the "Short Name" text box. Note that this will automatically fill in other text boxes. Then, click the "Attributes" tab at the top, here you need to do some options. The first threading model (Threading model), we select the default unit (Apartment) model. For interfaces (Interface), we choose Dual. Finally, because our procedure is independent of aggregation, we select "NO" radio button. You don't have to care about the three check boxes, you can click OK, you will create a new ATL simple object for us.

Step 3: Add a method

If you are now in the workspace, click the ClassView tab, then you will notice the wizard to add a stroke. The first thing we want to add is a method, and you can immediately mouse keys on "IFirst_atl" and select "Add Method".

Once you click "Add Method", you will see the "Add Method to Interface" window. You will see at the return value type (Return Type), this method returns HRESULT by default, in most cases you don't need to change it. The next text box allows us to enter the name of the method, we can enter "AddNumBers". The last text box is to let us enter the parameters, because we want to do two numbers added and get a return result, so we need three parameters, and the last parameter is a pointer. Now, we don't have to look at the 300-page tutorial about IDL, you can enter directly in the parameter text box: [in] long Num1, [in] long Num2, [out] long * ReturnVal

Simply, we declare the parameters of two long types, which are incoming ([IN]), and there is a final return value ([out]). (You will be somewhat surprises if you see something like this, but if you read a book about COM, you will feel more kind.) You can click the OK button now. Then, click the "ClassView" tab and expand all " " flags so that the tree view is fully expanded. You will see our "addNumbers" method and the parameters we give to the top of the interface (IFirst_atl). Double-click the mouse button on this method and insert the following code:

STDMETHODIMP CFIRST_ATL :: AddNumBers (long Num1, long Num2, long * returnval) {// Todo: add your importation code here * returnval = Num1 Num2; Return S_OK;}

Step 4: Compiling DLL

No matter whether you believe or, you already have a COM server written with ATL! Of course, we also need to compile it. Please press the F7 key so that VC can be compiled. After the compiler works a short, register your new DLL in the registry, so that other programs can be used. Let's test it.

Step 5: Test the COM server in Visual Basic

So let's test this COM server first with VB. (If you don't have VB, you can skip this section, test directly in VC .) Start VB, and select "Standard EXDARD EXE" to establish an engineering and place a command button on the dialog. Now, we need to add a reference to the COM server, click the "Project" menu and select "Reference (References)", find "Simple ATL 1.0 Type Library" and select it.

After clicking OK (OK) button, you can double-click the previously placed command button, and VB will switch to the code window of this button. Add the following code:

Private Sub Command1_Click () Dim objTestATL As SIMPLE_ATLLib.First_ATL Set objTestATL = New First_ATL Dim lngReturnValue As Long objTestATL.AddNumbers 5, 7, lngReturnValue MsgBox "The value of 5 7 is:" & lngReturnValue Set objTestATL = NothingEnd Sub If you are a VB The programmer, then these codes are very intuitive: we declare an object and call the "addNumBers" from the COM server, then display the results. Now press F5 to run this VB project, click the Command button, you will see the result of the expected: Isn't it difficult? Then let's come again, this time uses VC .

Step 6: Test the COM server in Visual C

If your Simple_atl project is still open, then turn it off and create a new project. Select "Win32 Console Application", name "Test_atl", click the OK button in the next window to accept all the defaults, and finally click the finish button. Now, you should have an empty project. Then, press CTRL N to join a new file, select "C Source File" and name "Test_atl.cpp", click OK Accept. You should now have a blank file that we need to add some code to test the COM server. code show as below:

/ / You need to specify the path to the Simple_ATL project to reference this header file #include "../simple_atl/simple_atl.h"#include // copy the following content from the Simple_ATL project directory SIMPLE_ATL_I.C file Come over // Note: You can also directly contain simple_atl_i.c files, I just want to clearly indicate where these constants come from and their appearance const same IID_iFirst_atl = {0xC8F6E230, 0X2672, 0X11D3, {0xA8, 0XA8, 0x00 , 0x10, 0x5a, 0xA9, 0x43, 0xDF}}; const clsid clsid_first_atl = {0x970599E0, 0x2673, 0x11d3, {0xA8, 0XA8, 0x00, 0X10, 0X5A, 0XA9, 0X43, 0X5A, 0XA9, 0X43, 0XDF}}; void main (void) {/ / Declare a HRESULT variable and a syrup hRESULT HR; IFirst_atl * ifirstatl = null; // now initializes COM HR = Coinitialize (0); // Use succeeded shadow to see if we can get the interface of the interface IF (successited) (HR)) {hr = cocreateInstance (CLSID_First_ATL, NULL, CLSCTX_INPROC_SERVER, IID_ID_IFIRST_ATL, (Void **) & IFirstatl); // If successful, then call AddNumBers method / / otherwise display a proper information IF (SUCCEEDED) )) {Long returnvalue; IFirstatl-> AddNumBers (5, 7, & returnval); cout << "The Answer for 5 7 IS: << ReturnValue << Endl; IFirstatl-> Release ();} else {cout << "CoCreateInstance failed." << end1;}} // Uninstall COM CounInitialize ();} Step 7: Compile and run the program

Now you can press the F5 key to compile the program, then press CTRL F5 to run. You should be able to see a DOS window and give the results you expect.

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

New Post(0)