如何使用.NET程序集的Win32从未经登记? [英] How to use .Net assembly from Win32 without registration?

查看:199
本文介绍了如何使用.NET程序集的Win32从未经登记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态加载并使用C#创建.NET程序集从德尔福Win32应用程序。我的类和接口都被标记为标记有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?

P.S。我发现这里链接文本的另一个好讨论的话题,但它更承​​载周围的CLR。这引出一个问题 - 为什么你会举办CLR与使用ClrCreateManagedInstance

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?

推荐答案

奇怪的是,我无法找到一个答案上计算器,并且没有太多的网络,特别是为Delphi。我发现从实例解决方案发布这里
下面是我在结束了:

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动态加载,因为它可能是不存在系统(XP没有.Net框架)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆