C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

C# DLL COM


前言:为了介绍C#写界面,C++写算法的快捷交互开发方式,首先介绍c++,C#内部的DLL,COM调用.

一, C# DLL
C#创建DLL非常的简单,只需要创建工程,选择工程类型为class libaray即可。这里不介绍。

二,C# COM
C#创建COM,相对与一般的DLL,有几个地方需要注意,看下面的实例:

Steps to create a Managed .NET C# COM Object:

  1. Open VS.NET2003->New Project->Visual C# Projects->Class Library.
  2. Project name: MyInterop.
  3. Create MyDoNetClass.cs file, and add the following lines of code:
    using System.Runtime.InteropServices;
        using System.Windows.Forms;
  4. Create an Interface IMyDotNetInterface.
  5. Create a class MyDoNetClass.
  6. Add the following line for MyDotNetClass:
    [ClassInterface(ClassInterfaceType.None)]

Although a .NET class is not directly invokable from unmanaged code, Microsoft has provided the capability of wrapping a .NET interface in an unmanaged layer of code that exposes the methods and properties of the .NET class as if the class were a COM object. There are two requirements for making a .NET class visible to unmanaged code as a COM object:

Requirement 1:

You have to add GUIDs - Globally Unique Identifiers - into your code for the interface and the class separately, through a GUID tool.

  1. Now, create a GUID for the Interface, and add the following line for the interface:
    [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
  2. Now, create a GUID for the class, and add the following line for the class:
    [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
  3. Your code will look like:      
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyInterop
{
    [Guid(
"03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
    interface IMyDotNetInterface
    {
        void ShowCOMDialog();
    }
     
    [ClassInterface(ClassInterfaceType.None)]
    [Guid(
"0490E147-F2D2-4909-A4B8-3533D2F264D0")]
    class MyDotNetClass : IMyDotNetInterface
    {
        
// Need a public default constructor for COM Interop.
        
public MyDotNetClass()
        {}
        
public void ShowCOMDialog()
        {
            System.Windows.Forms.MessageBox.Show(“I am a
" + 
                  "  Managed DotNET C# COM Object Dialog”);
        }
    }
}

 

10,Compile the solution.

11,You will see inside the project directory->obj->debug directory, the file “MyInterop.dll” generated after compilation.

Requirement 2:

Registration of the COM Class and Interfaces

For a COM class to be accessible by the client at runtime, the COM infrastructure must know how to locate the code that implements the COM class. COM doesn't know about .NET classes, but .NET provides a general "surrogate" DLL - mscoree.dll -- which acts as the wrapper and intermediary between the COM client and the .NET class.

  1. Hard-code a specific version number in your AssemblyVersion attribute in the AssemblyInfo.cs file which is in your project.

    Example:

    [assembly: AssemblyVersion("1.0.0.0")]
  2. Create a strong-name key pair for your assembly and point to it via the AssemblyKeyFile attribute in the AssemblyInfo.cs file which is in your project. Example:
    sn -k TestKeyPair.snk
    [assembly: AssemblyKeyFile("TestKeyPair.snk")]
  3. Add your assembly to the GAC using the following command:
    gacutil /i MyInterop.dll
  4. Register your assembly for COM by using the REGASM command along with the "/tlb" option to generate a COM type library.
    REGASM MyInterop.dll /tlb:com.MyInterop.tlb
  5. Close the C# project.

    到此COM创建完毕,可以被C#和Native的client调用。

三,C#内部调用
C#对C#的COM的调用和一般的DLL的调用一样的简单,只需要在工程中reference 所需要的COM或DLL。(Net真强!)

四,总结
一般使用C#开发DLL,即程序集,一般不开发COM,如果是要开发COM,一般了为了兼容以前的COM,则这个时候可以考虑把以前的COM转化net的程序集来使用。一般C#的COM会被Native代码来调用,具体的C++调用C#的COM见后面的章节。

参考:http://www.codeproject.com/csharp/ManagedCOM.asp  (包含上面的实例代码下载,C# 的Client的调用很简单,自己写!:~)

简单实例下载:http://www.cppblog.com/Files/mzty/C#COM.rar

posted on 2007-05-29 11:02 梦在天涯 阅读(6311) 评论(3)  编辑 收藏 引用 所属分类: C#/.NET

评论

# re: C# DLL COM 2007-06-22 12:25 .net编程

非常的好  回复  更多评论   

# re: C# DLL COM 2007-10-25 13:14 king2003

C#是托管的吧.这个没有.NET类库能用吗?  回复  更多评论   

# re: C# DLL COM 2007-10-25 13:17 梦在天涯

不能!  回复  更多评论   


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1633320
  • 排名 - 5

最新评论

阅读排行榜