如何使用.net汇编从Win32没有注册? [英] How to use .Net assembly from Win32 without registration?
问题描述
我想动态加载并使用Delphi Win32应用程序在C#中创建的.Net程序集。我的类和接口被标记为ComVisible,但是我想避免注册程序集。
这是可能吗?
I'd like to dynamically load and use a .Net assembly created in C# from a Delphi Win32 application. My classes and interfaces are marked as ComVisible, but I would like to avoid registering the assembly. Is this possible?
我在这里找到了链接文本另一个很好的讨论话题,但它更多地是托管CLR。哪个提出一个问题 - 为什么要使用ClrCreateManagedInstance来承载CLR?
P.S. I found here link text another good discussion on the topic, but it is more around hosting the CLR. Which begs a question - why would you host CLR versus using ClrCreateManagedInstance?
推荐答案
奇怪的是,我找不到答案StackOverflow,并没有太多的网络,特别是对于Delphi。我发现实例的解决方案发布了 here 。
这是我在最后得到的:
Strangely enough, I couldn't find an answer on StackOverflow, and there is not much on the Net, especially for 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根据情况,您可能需要动态加载mscoree.dll,因为它可能是不存在于系统上(没有.Net框架的XP)
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)
编辑:不幸的是,这是已被弃用的,并停止使用.Net4,我刚刚发现。这只剩下两个选项 - CLR托管和非托管导出。另外, .Net4 COM代码的调试被破坏。
Unfortunately, this was deprecated and stopped working with .Net4 as I just found out. This leaves only two options - CLR hosting and unmanaged export. Also, debugging of .Net4 COM code is broken.
这篇关于如何使用.net汇编从Win32没有注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!