前言:為了介紹C#寫界面,C++寫算法的快捷交互開發(fā)方式,首先介紹c++,C#內(nèi)部的DLL,COM調(diào)用.
一, C# DLL
C#創(chuàng)建DLL非常的簡(jiǎn)單,只需要?jiǎng)?chuàng)建工程,選擇工程類型為class libaray即可。這里不介紹。
二,C# COM
C#創(chuàng)建COM,相對(duì)與一般的DLL,有幾個(gè)地方需要注意,看下面的實(shí)例:
Steps to create a Managed .NET C# COM Object:
- Open VS.NET2003->New Project->Visual C# Projects->Class Library.
- Project name: MyInterop.
- Create MyDoNetClass.cs file, and add the following lines of code:
using System.Runtime.InteropServices;
using System.Windows.Forms;
- Create an Interface
IMyDotNetInterface.
- Create a class
MyDoNetClass.
- 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.

- Now, create a GUID for the Interface, and add the following line for the interface:
[Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
- Now, create a GUID for the class, and add the following line for the class:
[Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
- 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.
- 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")]
- 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")]
- Add your assembly to the GAC using the following command:
gacutil /i MyInterop.dll
- 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
- Close the C# project.
到此COM創(chuàng)建完畢,可以被C#和Native的client調(diào)用。
三,C#內(nèi)部調(diào)用
C#對(duì)C#的COM的調(diào)用和一般的DLL的調(diào)用一樣的簡(jiǎn)單,只需要在工程中reference 所需要的COM或DLL。(Net真強(qiáng)!)
四,總結(jié)
一般使用C#開發(fā)DLL,即程序集,一般不開發(fā)COM,如果是要開發(fā)COM,一般了為了兼容以前的COM,則這個(gè)時(shí)候可以考慮把以前的COM轉(zhuǎn)化net的程序集來(lái)使用。一般C#的COM會(huì)被Native代碼來(lái)調(diào)用,具體的C++調(diào)用C#的COM見后面的章節(jié)。
參考:http://www.codeproject.com/csharp/ManagedCOM.asp (包含上面的實(shí)例代碼下載,C# 的Client的調(diào)用很簡(jiǎn)單,自己寫?。簙)
簡(jiǎn)單實(shí)例下載:http://www.shnenglu.com/Files/mzty/C#COM.rar