.NET核心:从64位C#致电第三方32位DLL的事件处理
我将迁移一个.NET框架4.*应用程序到.NET 6。 主应用程序被编译为64位,我需要使用与X86第三方驱动程序交互的Windows System Sermogate(DLLHOST)中托管的32位(.NET 6)DLL。
我编写了一个带有32位服务器的示例,该示例由Windows System Sermogate托管的类库项目中的C#类实现。 我需要处理主要应用程序和dllhost之间的事件,但是在.NET 6中,我在事件处理程序注册期间有以下例外:
不能元帅'参数#1':.NET Core不支持编组 .NET框架提供的_delegate接口的委托 com类型库。要将代表作为界面,将其作为接口 idispatch指针。
如果我用.NET 4编译两个,则可以正常工作。**。
如何在.NET 6实现中定义和处理事件?
班级代码:
namespace OutOfProcCOM;
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)] //F586D6F4-AF37-441E-80A6-3D33D977882D
public interface IServer
{
double Test();
event OnMyEventDelegate OnMyEvent;
}
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)] //F586D6F4-AF37-441E-80A6-3D33D977882D
public delegate void OnMyEventDelegate(string text);
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)]
public interface IMyEvents
{
[DispId(1)]
void OnMyEvent(string text);
}
[ComVisible(true)]
[ComSourceInterfaces(typeof(IMyEvents))]
[Guid(Contract.Constants.ServerClass)] //"AF080472-F173-4D9D-8BE7-435776617347"
public sealed class DllServer : IServer
{
public event OnMyEventDelegate OnMyEvent;
public double Test()
{
OnMyEvent?.Invoke("Hello");
return 10;
}
}
这是我注册的方式:
regsvr32.exe “ dllserver.comhost.dll”
注册表条目
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Wow6432Node\AppID\{AF080472-F173-4D9D-8BE7-435776617347}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{AF080472-F173-4D9D-8BE7-435776617347}]
"AppID"="{AF080472-F173-4D9D-8BE7-435776617347}"
,这是客户:
var type = Type.GetTypeFromProgID("OutOfProcCOM.DllServer");
var obj = (IServer)Activator.CreateInstance(type);
obj.OnMyEvent += (OnMyEventDelegate)((t) =>
{
Console.WriteLine(t);
});
var x = obj.ComputePi();
谢谢!
I'm going to migrate a .NET Framework 4.* application to .NET 6.
Main application is compiled as 64bit and I need to use a 32bit (.NET 6) dll hosted as out-of-process in Windows system surrogate (dllhost) that interact with x86 third party driver.
I wrote a sample with a 32-bit server, implemented as a C# class in a class library project, hosted by Windows system surrogate.
I need to handle events between main application and dllhost but in .NET 6 I have the following exception during event handler registration:
Cannot marshal 'parameter #1': .NET Core does not support marshalling
delegates to the _Delegate interface provided by the .NET Framework
COM Type Library. To marshal a delegate as an interface, marshal it as
an IDispatch pointer.
It works fine if I compile both with .NET 4.*.
How can I define and handle events in .NET 6 implementation?
Class code:
namespace OutOfProcCOM;
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)] //F586D6F4-AF37-441E-80A6-3D33D977882D
public interface IServer
{
double Test();
event OnMyEventDelegate OnMyEvent;
}
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)] //F586D6F4-AF37-441E-80A6-3D33D977882D
public delegate void OnMyEventDelegate(string text);
[ComVisible(true)]
[Guid(Contract.Constants.ServerInterface)]
public interface IMyEvents
{
[DispId(1)]
void OnMyEvent(string text);
}
[ComVisible(true)]
[ComSourceInterfaces(typeof(IMyEvents))]
[Guid(Contract.Constants.ServerClass)] //"AF080472-F173-4D9D-8BE7-435776617347"
public sealed class DllServer : IServer
{
public event OnMyEventDelegate OnMyEvent;
public double Test()
{
OnMyEvent?.Invoke("Hello");
return 10;
}
}
This is how I register it:
regsvr32.exe
"DllServer.comhost.dll"
Registry entry
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Wow6432Node\AppID\{AF080472-F173-4D9D-8BE7-435776617347}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{AF080472-F173-4D9D-8BE7-435776617347}]
"AppID"="{AF080472-F173-4D9D-8BE7-435776617347}"
And here is the client:
var type = Type.GetTypeFromProgID("OutOfProcCOM.DllServer");
var obj = (IServer)Activator.CreateInstance(type);
obj.OnMyEvent += (OnMyEventDelegate)((t) =>
{
Console.WriteLine(t);
});
var x = obj.ComputePi();
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论