Create a COM component whole process in C #

Source: Internet
Author: User

Creating COM components in C # seems like a simple feature, but because there are fewer articles, there are still a lot of things that you don't understand that need to be learned. This article explains in detail how to create COM components in C #, and can be called with VC6.0.

This article explains in detail how to create COM components in C #, and can be called with VC6.0. With the code passed by the complete test. The overall look of this feature is simple. But I was in the first time in C # to do com, the pain of sorrow. Because very few people write this kind of article. And even if there is code is messy. It's all misguided. Later in C # help read a foreigner wrote the article (there is a complete SQL Server instance) before the completion of this feature. Share them.

Development tools: VS2008

VS2008 command Prompt (oh, this you should be able to find where it is)

Attached: This article applies to any VS series tool.

When creating COM components in C #, be sure to keep the following points in mind:

1: The class to be exported must be public;

2: All properties, methods must also be public;

3: The property to be exported, the method must be interface, and if it is not declared in the interface, it cannot be exported to COM normally, even if the method (property) is public. But they can be anything else. NET program;

4: All events must also be in the interface mode;

Now let's get to the point:

First, a new Visual C # project (used to call it, now called the solution), select the Type "Class library"; I'll call mycom.

Second, write the export interface. For everyone to understand the convenience, I only one addition operation example. As follows:

    1. [Guid ("154bd6a6-5ab8-4d7d-a343-0a68ab79470b")]
    2. Public Interface Mycom_interface
    3. {
    4. [DispID (1)]
    5. int ADD (int A, int b);
    6. }

The GUID is a globally unique identity and can be entered using the VS2008 command prompt: Guidgen will come out of its window. Select the last registry Format in a few checkboxes, click on the new GUID, then copy on the line (the following is the case with a Guid string, all the same operation)

[DispID (1)] is the identity of the function. If there are multiple functions corresponding to the function before adding [DispID (2)], [DispID (3)] ...

Third, create the event interface.

    1. [Guid ("D11FEA37-AC57-4D39-9522-E49C4F9826BB"),
    2. InterfaceType (Cominterfacetype.interfaceisidispatch)]
    3. Public Interface mycom_events
    4. {
    5. }

GUID same as two, not much to say

The InterfaceType table asks for a way to expose to COM, where it is chosen to be exposed to COM in a dispatch manner.

IV. Create specific classes:

    1. [guid ( "2e3c7bad-1051-4622-9c4c-215182c6bf58"),
    2. classinterface (classinterfacetype.none),
    3. ComSourceInterfaces ( typeof (mycom_events))]
    4. public class Class1:mycom_ Interface
    5. {
    6. < Strong>public int Add ( int A, int b)
    7. {
    8. return A + b;
    9. }

At this point, the code is complete. It's simple. Don't worry, there are often overlooked steps behind, first put the whole code below (note the reference interopservices):

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Mycom
  5. {
  6. [Guid ("154bd6a6-5ab8-4d7d-a343-0a68ab79470b")]
  7. Public Interface mycom_interface
  8. {
  9. [DispID (1)]
  10. int ADD (int A, int b);
  11. }
  12. [Guid ("D11FEA37-AC57-4D39-9522-E49C4F9826BB"),
  13. InterfaceType (Cominterfacetype.interfaceisidispatch)]
  14. Public Interface mycom_events
  15. {
  16. }
  17. [Guid ("2e3c7bad-1051-4622-9c4c-215182c6bf58"),
  18. ClassInterface (ClassInterfaceType.None),
  19. ComSourceInterfaces (typeof(mycom_events))]
  20. Public class Class1:mycom_interface
  21. {
  22. Public int Add (int A, int b)
  23. {
  24. return A + b;
  25. }
  26. }
  27. }

We all know that COM needs to be registered. To encrypt the key file when registering. SNK. This is the generation of SNK files. Enter the VS2008 command prompt. With command: Sn–k mycom.snk enter. My E:\VS2008\VC generates a file (MYCOM.SNK) below the. (You can find it under your own command prompt in the file directory). Then copy it to your project root directory.

Six, open the AssemblyInfo.cs. Add in [Assembly:assemblykeyfile ("Mycom.snk")]

Vii. (1) Project properties, application-and assembly information, select Make assembly COM visible. (2) Project properties, build, check Register for COM Interop.

Eight, generate. If there is a mycom.tlb under Debug, then you have succeeded (and certainly MyCom.dll) to have this TLB file for testing in VC6.0.

Nine, in the VC built an MFC dialog box program (of course, the console program is the same, I want to look at the convenience). When you're finished, copy the mycom.tlb you just created into your root directory.

Ten, select the code to add #import "Mycom.tlb", and then write the core test code: (I was added in a Button1 button), as follows:

    1. void Ctestdlg::onbutton1 ()
    2. {
    3. CoInitialize (NULL); //Note initialization
    4. Mycom::mycom_interfaceptr p (__uuidof (MYCOM::CLASS1)); //Create smart pointer
    5. Mycom::mycom_interface *s = p;
    6. int a = 3;
    7. int B = 6;
    8. int c = S->add (A, b);
    9. CString str;
    10. Str. Format ("%d", c);
    11. MessageBox (str);
    12. }

When a 9 pops up, you have successfully created COM components in C #.

Go from MFT article: http://www.cnblogs.com/panlijiao/archive/2012/10/14/2773881.html

Create a COM component whole process in C #

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.