c creates vc callable com component method shares

  • 2020-05-26 10:02:54
  • OfStack

Development tool: VS2008

VS2008 command prompt (oh, you should be able to find this somewhere)

Attachment: this article applies to any VS series tool.

Here are a few things to keep in mind when creating COM using C# :

1: the class to be exported must be public;

2: all attributes and methods must be public;

3: the properties and methods to be exported must be in the interface mode; If it is not declared in the interface, even if the method (property) is public, it cannot be properly exported to COM. But they can be used by other.NET programs;

4: all events must also be interfaced;

Now let's get down to business:

1. Create a new Visual C# project.

Select type "class library"; I'll just call it MyCom.

2. Write the export interface.

I'm just going to give you an example of an addition operation just for your convenience. As follows:


[Guid("154BD6A6-5AB8-4d7d-A343-0A68AB79470B")]
    public interface MyCom_Interface
    {
        [DispId(1)]
        int Add(int a, int b);

Guid is a global unique 1 identifier, and you can use the command prompt VS2008 to enter: guidgen will come out of its window. Select the last Registry Format in a few check boxes, click New Guid, and then COPY.

[DispId(1)] is the identifier of the function. If there are multiple functions, [DispId(2)], [DispId(3)]...

3. Create an event interface.


[Guid("D11FEA37-AC57-4d39-9522-E49C4F9826BB"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface MyCom_Events
{
}

Guid is the same thing as 2, not to mention

The InterfaceType table finds the mode to be disclosed to COM, and it is selected here to be disclosed to COM in the mode of dispatch.

4. Create concrete classes:


[Guid("2E3C7BAD-1051-4622-9C4C-215182C6BF58"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(MyCom_Events))]
public class Class1 : MyCom_Interface
{
public int Add(int a, int b)
{
return a + b;
}
}

At this point, the code is complete. Simple enough. Don't worry, there are often overlooked steps to follow, but put the overall code below (note the reference to InteropServices) :


using System;
using System.Runtime.InteropServices;
using System.Text;
 
namespace MyCom
{
[Guid("154BD6A6-5AB8-4d7d-A343-0A68AB79470B")]
public interface MyCom_Interface
{
[DispId(1)]
int Add(int a, int b);
}
 
[Guid("D11FEA37-AC57-4d39-9522-E49C4F9826BB"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface MyCom_Events
{
 
}
 
[Guid("2E3C7BAD-1051-4622-9C4C-215182C6BF58"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(MyCom_Events))]
public class Class1 : MyCom_Interface
{
public int Add(int a, int b)
{
return a + b;
}
}
}


5. Everyone knows that COM requires registration.

Register to add the key file.SNK. The first step is to generate the SNK file. Enter the VS2008 command prompt. To use the command: sn, k MyCom.snk enter. One (MyCom.snk) file is generated under E:\vs2008\vc. You can look for it in the file directory where your command prompt is located. Then put it COPY into your project root directory.

6. Open AssemblyInfo.cs.

Add [assembly:AssemblyKeyFile(" MyCom.snk ")]

7. (1) project attributes - > Applications - > Assembly information - > Select make assembly COM visible. (2) project attributes - > Generate - > Select register for COM interop.

8. To generate. If you have an MyCom.tlb under Debug, you have succeeded (and you must have MyCom.dll as well) in asking for this tlb file in order to test it in VC6.0.

9. Build an MFC dialog program in VC (of course, Console is the same as the Console program, just for convenience). Once you're done, copy the MyCom.tlb you just generated to your root directory.

10. Select #import "MyCom.tlb" in the code and write the core test code: (I added it in one Button1 button), as follows:


void CTestDlg::OnButton1() 
{
CoInitialize(NULL);// Pay attention to initialization 
MyCom::MyCom_InterfacePtr p(__uuidof(MyCom::Class1));// Creating smart Pointers 
MyCom::MyCom_Interface *s = p;
int a = 3;
int b = 6;
int c = s->Add(a,b);
CString str;
str.Format("%d",c);
MessageBox(str);
}

When you pop up a 9, you're done.


Related articles: