最近要把一个人家用C#编的程序中的 部分功能抽取成模块,然后放到ASP中用。对这些东西毫不了解,于是网上收罗了下。
第一步:C#应用程序转化成模块,也就是library或者程序集,这里就要兴建一个Class Library项目。
第二步:由于C#的Class Library不能被ASP直接使用似乎,要转成COM然后注册才能使用。于是参照http://www.cppblog.com/mzty/archive/2007/05/29/25043.html
一, C# DLL
C#创建DLL非常的简单,只需要创建工程,选择工程类型为class libaray即可。这里不介绍。
二,C# COM
C#创建COM,相对与一般的DLL,有几个地方需要注意,看下面的实例:
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:
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
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创建完毕,可以被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见后面的章节。
在C++中调用该COM的方法(from http://www.codeproject.com/csharp/ManagedCOM.asp)
Steps to create an Unmanaged C++ application to call a .NET Managed C# COM
- Open VS.NET2003->New Project->Visual C++ Projects->Win32->Win32 Console Project.
- Name: DotNet_COM_Call.
- Include the following line in your DoNet_COM_Call.cpp file:
#import “<Full Path>/com.MyInterop.tlb" named_guids raw_interfaces_only
- Compile the solution.
- It will generate a “com.myinterop.tlh” file into your project->debug directory.
- You can open this file and see the contents. This is basically the proxy code of the C# COM code.
- Now, you can write the code to call the .NET Managed COM.
- Please add the following lines of code before calling the COM exported functions:
CoInitialize(NULL); //Initialize all COM Components // <namespace>::<InterfaceName> MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr; // CreateInstance parameters // e.g. CreateInstance (<namespace::CLSID_<ClassName>) HRESULT hRes = pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass); if (hRes == S_OK) { BSTR str; pDotNetCOMPtr->ShowCOMDialog (); //call .NET COM exported function ShowDialog () } CoUninitialize (); //DeInitialize all COM Components
- Run this console application.
- Expected result: a managed code (C# ) dialog should appear with the string “I am a Managed DotNET C# COM Object Dialog”.