在Delphi中使用/不使用JCL托管CLR - 例子 [英] Hosting CLR in Delphi with/without JCL - example

查看:214
本文介绍了在Delphi中使用/不使用JCL托管CLR - 例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在这里发贴一个如何在Delphi中托管CLR的例子?我已经阅读了类似的问题,但我不能使用JCL作为我想在Delphi 5中托管它。谢谢。






编辑:关于在Fox Pro中托管CLR的href =http://www.west-wind.com/Weblog/posts/104449.aspx =nofollow noreferrer>文章看起来很有前途,但我不知道如何从Delphi访问clrhost.dll。






编辑2:我放弃了Delphi 5需求。现在我正在用Delphi 7尝试JCL。但是我再也找不到任何的例子。这是我到现在为止:



我的C#程序集:

 命名空间DelphiNET 
{
public class NETAdder
{
public int Add3(int left)
{
return left + 3;
}
}
}

我已将其编译成 DelphiNET.dll



现在我要使用Delphi中的这个程序集:

 使用JclDotNet,mscorlib_TLB; 

procedure TForm1.Button1Click(Sender:TObject);
var
clr:TJclClrHost;
ads:TJclClrAppDomainSetup;
ad:TJclClrAppDomain;
ass:TJclClrAssembly;
obj:_ObjectHandle;
ov:OleVariant;
begin
clr:= TJclClrHost.Create();
clr.Start;
ads:= clr.CreateDomainSetup;
ads.ApplicationBase:='C:\Delhi.NET';
ads.ConfigurationFile:='C:\Delhi.NET\my.config';
ad:= clr.CreateAppDomain('myNET',ads);
obj:=(ad as _AppDomain).CreateInstanceFrom('DelphiNET.dll','DelphiNET.NETAdder');
ov:= obj.Unwrap;
Button1.Caption:='done'+ string(ov.Add3(5));
结束

结束于错误: EOleError:Variant不引用自动化对象



我没有和Delphi一起工作很久,所以我被困在这里...






解决方案: COM可见性中没有默认情况下出现问题。这是正确的.NET程序集:

 命名空间DelphiNET 
{
[ComVisible(true)]
public class NETAdder
{
public int Add3(int left)
{
return left + 3;
}
}
}






重要提示:



当使用来自Delphi的.NET时,调用 Set8087CW($ 133F); 在程序的开头(即在 Application.Initialize; 之前)。默认情况下,Delphi启用了浮点异常(请参阅),并且CLR不会不喜欢他们当我启用它时,我的程序奇怪地冻结了。

解决方案

该类必须是可见的。如果您有整个程序集的ComVisible(false)可能不是这种情况。



.Net类将默认为IDispatch兼容,因此您的示例应该正常工作,如果课堂真的是可以看到的。



但是将它最小化到最低限度。将您的exe放在与.Net程序集相同的文件夹中,并跳过配置文件和应用程序库。



在某些事情混淆之前,异常在这里发生,对吗? / p>

  ov:= obj.Unwrap; 


Can somebody please post here an example how to host CLR in Delphi? I have read similar question here but I cannot use JCL as I want to host it in Delphi 5. Thank you.


EDIT: This article about hosting CLR in Fox Pro looks promising but I don't know how to access clrhost.dll from Delphi.


Edit 2: I give up on Delphi 5 requirement. Now I'm trying JCL with Delphi 7. But again I am unable to find any example. Here is what I have till now:

My C# assembly:

namespace DelphiNET
{
    public class NETAdder
    {
        public int Add3(int left)
        {
            return left + 3;
        }
    }
}

I have compiled it to DelphiNET.dll.

Now I want to use this assembly from Delphi:

uses JclDotNet, mscorlib_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
  clr: TJclClrHost;
  ads: TJclClrAppDomainSetup;
  ad: TJclClrAppDomain;
  ass: TJclClrAssembly;
  obj: _ObjectHandle;
  ov: OleVariant;
begin
  clr := TJclClrHost.Create();
  clr.Start;
  ads := clr.CreateDomainSetup;
  ads.ApplicationBase := 'C:\Delhi.NET';
  ads.ConfigurationFile := 'C:\Delhi.NET\my.config';
  ad := clr.CreateAppDomain('myNET', ads);
  obj := (ad as _AppDomain).CreateInstanceFrom('DelphiNET.dll', 'DelphiNET.NETAdder');
  ov := obj.Unwrap;
  Button1.Caption := 'done ' + string(ov.Add3(5));
end;

This ends with error: EOleError: Variant does not reference an automation object

I have not worked with Delphi for a long time so I am stuck here...


Solution: There was problem in COM visibility which is not by default. This is the correct .NET assembly:

namespace DelphiNET
{
    [ComVisible(true)]
    public class NETAdder
    {
        public int Add3(int left)
        {
            return left + 3;
        }
    }
}


Important note:

When working with .NET from Delphi, it is important calling Set8087CW($133F); at the beginning of your program (i.e. before Application.Initialize;). Delphi has enabled floating point exceptions by default (see this) and the CLR doesn’t like them. When I had them enabled, my program weirdly freezed.

解决方案

The class has to be comvisible. Which might not be the case if you have ComVisible(false) for the whole assembly.

.Net classes will be IDispatch compatible by default, so your sample should work just fine, if the class really is comvisible..

But strip it down to the bare minimum first. Put your exe in the same folder as your .Net assembly and skip the config file and application base.

Before something gets mixed up, the exception happesn here, right?

 ov := obj.Unwrap;

这篇关于在Delphi中使用/不使用JCL托管CLR - 例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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