用C#写了一个DLL类库,如果包含了委托,该怎么处理?VC中又该如何向其注册函数?先谢谢了。
简单的例子using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace TestOfDLL
{ [Guid("2A74D22E-37D3-4b5d-8CFC-5125EDFEF1DB"),ComVisibleAttribute(true)]
public delegate void AddEventHandler(); [Guid("83C8E00E-A801-4fe9-B500-29BEDAF3F142"), ComVisibleAttribute(true)]
public interface ComClass1Interface
{
int Add(int a, int b);
int Dec(int a, int b);
event AddEventHandler some;
} [Guid("D9091A89-AEB5-4217-8C0F-9EEA61FABAE5"), ClassInterface(ClassInterfaceType.AutoDispatch), ComVisibleAttribute(true)]
public class Class1:ComClass1Interface
{
public event AddEventHandler some;
#region ComClass1Interface 成员 int ComClass1Interface.Add(int a, int b)
{
return a + b;
} int ComClass1Interface.Dec(int a, int b)
{
return a - b;
} event AddEventHandler ComClass1Interface.some
{
add
{
throw new NotImplementedException();//如何写?
}
remove
{
throw new NotImplementedException();//如何写?
}
} #endregion
}
}
简单的例子using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace TestOfDLL
{ [Guid("2A74D22E-37D3-4b5d-8CFC-5125EDFEF1DB"),ComVisibleAttribute(true)]
public delegate void AddEventHandler(); [Guid("83C8E00E-A801-4fe9-B500-29BEDAF3F142"), ComVisibleAttribute(true)]
public interface ComClass1Interface
{
int Add(int a, int b);
int Dec(int a, int b);
event AddEventHandler some;
} [Guid("D9091A89-AEB5-4217-8C0F-9EEA61FABAE5"), ClassInterface(ClassInterfaceType.AutoDispatch), ComVisibleAttribute(true)]
public class Class1:ComClass1Interface
{
public event AddEventHandler some;
#region ComClass1Interface 成员 int ComClass1Interface.Add(int a, int b)
{
return a + b;
} int ComClass1Interface.Dec(int a, int b)
{
return a - b;
} event AddEventHandler ComClass1Interface.some
{
add
{
throw new NotImplementedException();//如何写?
}
remove
{
throw new NotImplementedException();//如何写?
}
} #endregion
}
}
假设要进行二次开发的程序为 A.exe,要增加的功能使用增加DLL的方法来完成,设为Cdll.DLL,其中含有A、B、C等方法。 其中的方法A要用到C#类库Csharp.DLL中的方法A。要完成这个任务,参考网络上的各种方法与代码,总结其步骤如下:先用VS2008 C#类库项目写成含有A方法的DLL,并设置项目属性以进行COM的封装,然后用VC6 等写C代码进行调用。一、生成C#类库 Csharp.DLL(1)在Visual Studio 2008 中创建C#的类库项目 CSharp(2)实现的类假设为:using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;//必须引用此空间
namespace CSharp{ [Guid("4BF8F1CE-F33D-48bd-A7C8-C4E8EE7376B1")] public interface iClass1 { string A(); }
[Guid ("39BE150B-7684-4a16-9574-0F6C15BDE407")] [ClassInterface(ClassInterfaceType.None)] public class Class1 : iClass1 { public string A() { return "测试字符串A"; } }}/*通过菜单的 “工具/创建 GUID”。注意要选择Define Guid{….}格式* 例如:// {4BF8F1CE-F33D-48bd-A7C8-C4E8EE7376B1}DEFINE_GUID(<<name>>, 0x4bf8f1ce, 0xf33d, 0x48bd, 0xa7, 0xc8, 0xc4, 0xe8, 0xee, 0x73, 0x76, 0xb1);
// {39BE150B-7684-4a16-9574-0F6C15BDE407}DEFINE_GUID(<<name>>, 0x39be150b, 0x7684, 0x4a16, 0x95, 0x74, 0xf, 0x6c, 0x15, 0xbd, 0xe4, 0x7);*/如果要增加其他方法,可如法炮制。(3)COM生成设置(管理员权限)配置:选择项目“属性”,选择“生成”,选择“为COM 互操作注册”打开AssemblyInfo.cs文件,设置 [assembly: ComVisible(true)]这样就可以生成CSharp.tlb文件(4)生成解决方案生成Csharp.DLL和Csharp.tlb即可。二、生成C或C++的 Cdll.DLL下面这个是测试程序,用来测试所写的C# COM组件是否正常。在VC6.0中测试#include "stdafx.h"#import "CSharp.tlb"//1、导入类型库tlbusing namespace CSharp;//下面COM初始化中可以不用写命名空间void main() { CoInitialize(NULL);// 2、初始化COM以及产生智能指针 iClass1Ptr p(__uuidof(Class1));//命名规则:接口Ptr p((__uuidof(类)))//3、调用COM中的方法 _bstr_t pt=p->A();//这个方法返回 string类型.可以根据IDE的提示设置类型 printf(pt); CoUninitialize(); //4、释放环境}如果测试正确,那么使用 Visual Studio C++ 6.0 ,创建C++类库项目 Cdll。定义def 文件如:LIBRARY CdllEXPORTS A
定义Cdll.cpp文件如:#include<stdio.h>#import "CSharp.tlb"using namespace CSharp;void A(){ CoInitialize(NULL); iClass1Ptr p(__uuidof(Class1)); _bstr_t pt=p->A(); printf(pt); CoUninitialize();}当然,要把Csharp.DLL和Csharp.tlb放到工程文件夹中。这样最终得到 Cdll.dll和Cdll.lib 两个文件。
C#的COM组件是一个DLL形式的文件,那么能不能像其他DLL一样如系统 kernel32.dll 直接在C代码(或C++代码)中使用呢?也就是不通过COM的封装而是直接使用。这样做是行不通的。因为C#的类库必然是托管类库,虽然文件扩展名也是DLL,但它不是一种传统意义上的DLL,例如没有输出表,所以得经过COM封装才能给非托管代码使用。至于托管代码使用托管DLL 如C#调用C#写的类库那就没必要经过COM的封装了。
event AddEventHandler some;
dll导出有两个函数
add_some
remove_some
参数是TestOfDLL::_AddEventHandler *value我该怎么定义这样的类型?又该怎么传递?不过还是要谢谢您的回复。
HRESULT hr = S_OK;
CComPtr<TestOfDLL::ComClass1Interface> pNew;
hr = pNew->add_some((TestOfDLL::_AddEventHandler *)fun);//这里hr失败