How to call com DLL (zz) on the MFC Client)

Source: Internet
Author: User
How to call com DLL on the MFC Client

Some may think this is a very pediatric problem. However, it took me some time and effort to figure it out. OK. Let's briefly talk about how to do this.

There are many ways to generate a com DLL. For example, VB, VC ++, and Delphi. Since I only omitted VB and Vc, I just used them as an example.

(1) DLL generated by VC

If the DLL is generated by VC, you can call the DLL on the MFC client through The TLB (Type Library) of the DLL.

Assume that the TLB file of the DLL is testatl. TLB. The content of the IDL file of the DLL is as follows:

Import "oaidl. IDL ";

Import "ocidl. IDL ";

[

Object,

UUID (0a2e8c16-7043-4fe9-8af1-545e88edbe14 ),

Dual,

Helpstring ("itestatlx interface "),

Pointer_default (unique)

]

Interface itestatlx: idispatch

{



[ID (1), helpstring ("method addlong")] hresult addlong ([in] Long X, [out, retval] Long * y );

};

[

UUID (AD75C4D3-958D-4A31-9BB7-512A8B4CCB60 ),

Version (1.0 ),

Helpstring ("testatl 1.0 Type Library ")

]

Library testatllib

{

Importlib ("stdole32.tlb ");

Importlib ("stdole2.tlb ");

[

UUID (94be9ef2-d9f9-469b-bff4-5aba71dadc38 ),

Helpstring ("testatlx class ")

]

Coclass testatlx

{

[Default] interface itestatlx;

};

};

You only need to add the following code to the MFC client.

1)

# Import ".. \ testatl. TLB" // .. \ testatl is the relative path of the Client

Using namespace testatllib;

Slow down. Where can this code be added? They can only be located below # endif and under annotations. Otherwise, hey...

// {Afx_insert_location }}

// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.

# Endif //! Defined (afx_xxxdlg_h1_cdd4b4c8_59e6_4c9c_bdf8_2820ff4960741_encoded _)

2)

Next, you can declare the classes in the DLL and call the methods in the DLL.

Itestatlxptr test (_ uuidof (testatlx ));

Long y = test-> addlong (8 );

What is itestatlxptr? Simple: the pointer of coclass interface in your DLL. If your interface is itestatlx, It is itestatlxptr. If it is inormalcom, then it is inormalcomptr. testatlx. You don't have to say you know it, coclass! Now, you can use itestatlxptr test (_ uuidof (testatlx) to obtain a test, through which you can call the function in the DLL.

And don't forget to call coinitialize () and couninitialize () before and after this code (). I won't talk about their role. What is unclear ?! · # ¥ % ............ -?

(2) DLL generated by VB

In fact, the com DLL generated by VB is basically similar to the com DLL generated by VC (not all implement the COM interface ). If there is a difference, the com DLL generated by VB contains the Type Library. In this way, the MFC code is slightly different. (I just extracted the VB part from codeproject. You can read it from http: // www.codeproject.com/dll/vbactivexwithvc.asp.Amit dey)

1)

# Import "prjdll. dll"

Using namespace prjdll;

Of course, it must also be placed in that strange place.

2)

Hresult;

CLSID;

Coinitialize (null); // initialize com Library

Hresult = clsidfromprogid (olestr ("prjdll. clsdll"), & CLSID); // retrieve CLSID of component



_ Clsdll * t;

Hresult = cocreateinstance (CLSID, null, clsctx_inproc_server ,__ uuidof (_ clsdll), (lpvoid *) & T );

If (failed (hresult ))

{

Afxmessagebox ("creation failed ");

Return;

}

T-> fncalldll (); // call Method

T-> release (); // call Method

Couninitialize (); // unintialize the com Library

(3) Others

You can also call the com DLL generated by VC through clsidfromprogid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.