简体   繁体   English

如何在没有注册的情况下从Win32使用.Net程序集?

[英]How to use .Net assembly from Win32 without registration?

I'd like to dynamically load and use a .Net assembly created in C# from a Delphi Win32 application. 我想从Delphi Win32应用程序动态加载和使用在C#中创建的.Net程序集。 My classes and interfaces are marked as ComVisible, but I would like to avoid registering the assembly. 我的类和接口标记为ComVisible,但我想避免注册程序集。 Is this possible? 这可能吗?

PS I found here link text another good discussion on the topic, but it is more around hosting the CLR. PS我在这里找到链接文本关于该主题的另一个很好的讨论,但它更多地围绕托管CLR。 Which begs a question - why would you host CLR versus using ClrCreateManagedInstance? 这引出了一个问题 - 你为什么要主持CLR而不是使用ClrCreateManagedInstance?

Strangely enough, I couldn't find an answer on StackOverflow, and there is not much on the Net, especially for Delphi. 奇怪的是,我无法在StackOverflow上找到答案,网上也没有太多,特别是对于Delphi。 I found the solution from examples posted here . 我发现了这里发布的示例解决方案。 Here's what I got at the end: 这是我最后得到的:

function ClrCreateManagedInstance(pTypeName: PWideChar; const riid: TIID;
out ppObject): HRESULT; stdcall; external 'mscoree.dll';

procedure TMyDotNetInterop.InitDotNetAssemblyLibrary;
var
  MyIntf: IMyIntf;
hr: HRESULT;
NetClassName: WideString;
begin
//Partial assembly name works but full assembly name is preffered.
    NetClassName := 'MyCompany.MyDLLName.MyClassThatImplementsIMyIntf,
          MyCompany.MyDLLName';
    hr := ClrCreateManagedInstance(PWideChar(NetClassName), IMyIntf, MyIntf);
    //Check for error. Possible exception is EOleException with ErrorCode
    //FUSION_E_INVALID_NAME = $80131047 2148732999 : The given assembly name 
    //or codebase was invalid.
    //COR_E_TYPELOAD = $80131522 - "Could not find or load a specific type 
    //(class, enum, etc)"
    //E_NOINTERFACE = $80004002 - "Interface not supported".
    OleCheck(hr);
end;

BTW, depending on the situation, you might want to load mscoree.dll dynamically, because it might be not present on the system (XP with no .Net Framework) 顺便说一句,根据具体情况,你可能想要动态加载mscoree.dll,因为它可能不存在于系统上(没有.Net Framework的XP)

EDIT: Unfortunately, this was deprecated and stopped working with .Net4 as I just found out. 编辑:不幸的是,我刚刚发现,这已被弃用并停止使用.Net4。 This leaves only two options - CLR hosting and unmanaged export . 这只留下两个选项 - CLR托管和非托管导出 Also, debugging of .Net4 COM code is broken . 此外, .Net4 COM代码的调试被破坏

Maybe using RegFreeCOM and a COm-Callable Wrapper. 也许使用RegFreeCOM和COm-Callable Wrapper。

http://msdn.microsoft.com/en-us/magazine/cc188708.aspx http://msdn.microsoft.com/en-us/magazine/cc188708.aspx

不幸的是,在没有公开COM对象的情况下,这是不可能做到的(据我所知),因为.NET dll不是TRULY编译到Delphi(或其他任何东西)可以加载的库,因为这一切都是由JIT编译器在运行时完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM