Introduction to COM Component Development interface and sample tutorial

Source: Internet
Author: User
Tags visual studio

Recently a collaborative 973 project that needs to call someone else's interface. The other side gave a DLL file and function interface description, and then I looked through the traditional generated dll,loadlibrary to find function pointers, through function pointers to call, failed. Later found that the DLL is not a general DLL but a COM interface, need to register AH and so on.

The purpose of COM is to cross the programming language, for example, they use VB, C # and so on, and then your project is written in C + +, for some reason, you need to directly reuse the other people's implementation, others in security or not necessary for you to implement, only need to encapsulate a COM interface for you to call. The COM interface is different from the normal function generation DLL, and the function pointer is called by the LoadLibrary loading DLL through the function name.

The following is a simple example to illustrate the application of COM interfaces. The development environment is in visual Studio.

The following code implements an "important" interface function in C #, which is addition.

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;

Namespace Comtest
{
public interface Programinterface
{
int add (int a, int b);
}
Default is Classinterfacetype.autodispatch, do not show annotations also OK
[ClassInterface (Classinterfacetype.autodual)]
can refer to http://www.cnblogs.com/JessieDong/archive/2009/7/21.html
public class Program:programinterface
{
Public program ()
{ }

public int Add (int a, int b)
{
return a + B;
}
}
}

When you are done, right-click the project properties, select Application, click Assembly Information, and then check "make assembly COM visible." (English version below)

Then select "Generate" tab and check "register for COM Interop." (English version below)

Then build, the build may fail, prompt permissions are insufficient, and then run as administrator to regenerate.

The following message sees the DLL successfully generated.

1> comtest-> D:cppcomtestcomtestbinreleasecomtest.dll

The generated DLL can be invoked by another person. The caller needs to register with RegAsm to generate the TLB file.

The call can be directly import this TLB file, directly write an empty main function,

#import "D:cppcomtestreleasecomtest.tlb" raw_interfaces_only

It is then generated so that the build can generate a COMTEST.TLH file that Visual Studio can intelligently prompt.

The invocation can be this way:

Programinterfaceptr PTR (__uuidof (program));

Programinterface is the name of the interface just declared in C #, and then add the suffix PTR declaration is a smart pointer to the interface (TLH file has the corresponding macro), program for the specific implementation of the corresponding interface classes. The corresponding implementation method is then invoked through the PTR pointer. Note Remember to pass CoInitialize (NULL) before declaring PTR, and initialize some COM environments.

The caller complete code is as follows:

#include <windows.h>
#include <iostream>
using namespace Std;

#import "D:cppcomtestreleasecomtest.tlb" raw_interfaces_only

using namespace Comtest;
int main ()
{
cout << "Hello World" << Endl;

HRESULT hr = CoInitialize (NULL);
if (HR!=S_OK)
{
printf ("hr failed/n");
Exit (-1);
}
Programinterfaceptr PTR (__uuidof (program));
Long a = 1;
Long B = 2;
Long C;
Ptr->add (A, B, &c);
cout << c << Endl;

}
int main1 ()//this also works
{
HRESULT hr = CoInitializeEx (null,coinit_multithreaded);
if (HR!=S_OK)
{
printf ("hr failed/n");
Exit (-1);
}
Comtest::P rograminterfaceptr ptr;
Long a = 1;
Long B = 2;
Long C;
hr = ptr. CreateInstance (__uuidof (program), NULL, Clsctx_all);
if (hr = = S_OK)
{
Ptr->add (A, B, &c);
cout << "result =" << C;
}
Else
{
cout << "fail" << Endl;
}
}

Run and you can see the following results

Reference:

[1] Excel calls the C # generated DLL https://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80% 99s-guide-to-calling-a-net-library-from-excel/

[2] Com Component Introduction http://www.cppblog.com/3522021224/archive/2007/06/22/26803.html

[3] Wiki:http://en.wikipedia.org/wiki/component_object_model

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.