以下有一个TestLib.dll的.net 类库,其中有一类:TestClass
请按照以下步骤来操作:
1. 在客户端注册.net控件
在命令行执行regasm TestLib.dll /codebase。注:安装了.Net Framework的机器在Framework目录下就有regasm.exe的程序
2. 在平台上,调用这个控件
X3平台上,通过DispatchHelper来调用这个控件的方法和属性
其中:
InvokeMethod 调用方法
PropertyGet 获取参数的值
PropertySet 设置参数的值
例如:下面代码是调用一个方法的写法。
var
c: object;
d: System.DispatchHelper;
begin
c := ComObj.CreateOleObject('TestLib.TestClass');
d := System.DispatchHelper.Create(a);
retrunValue := d.InvokeMethod('methodName', [param1, param2 ... paramN]));
end;
注意:
1、.net 中的实现类的构造函数不能带参数。
2、只能传递简单类型,比如:int、long、double、string 等,传递数组参数时,需要声名为 object 类型,如下:
public class TestClass
{
public int arrayTest(object a)
{
int[] b = (int[])a;
return b[0] + b[1];
}
}
10. 第三步生成注册文件_reg.pas的时候,提示“Access violation at address 004BE4FC in module 'DelphiImporter.exe'. Read of address 00000000”
以下情况都会出现这个错误:
1) .pas文件编译错误:请首先在Delphi中编译涉及的所有文件是否可以编译通过。
2) 包含了CLX的单元文件:请确认单元文件中没有包含CLX的单元。
3) .pas文件中有编译指示near、far、export,去掉这些兼容性的编译指示。
4) .pas文件中,有函数使用了缺省参数,请去掉缺省参数,然后才可以导入进来。
用这个tlb生成的文件,在Delphi中就编译不通过
[Error] mscorlib_TLB.pas(5191): Type 'Byte' is not yet completely defined
[Error] mscorlib_TLB.pas(5206): Type 'Double' is not yet completely defined
[Error] mscorlib_TLB.pas(5232): Type 'Int64' is not yet completely defined
[Error] mscorlib_TLB.pas(5264): Type 'Single' is not yet completely defined
[Error] mscorlib_TLB.pas(5744): Illegal type in OLE automation section: 'Byte'
[Error] mscorlib_TLB.pas(5751): Illegal type in OLE automation section: 'Single'
[Error] mscorlib_TLB.pas(5752): Illegal type in OLE automation section: 'Double'
[Error] mscorlib_TLB.pas(10871): Illegal type in OLE automation section: 'Byte'
[Error] mscorlib_TLB.pas(10878): Illegal type in OLE automation section: 'Single'
[Error] mscorlib_TLB.pas(10879): Illegal type in OLE automation section: 'Double'
[Fatal Error] ControlSamples_TLB.pas(35): Could not compile used unit '..\..\Business_3.0\Import\2323\ControlSample\mscorlib_TLB.pas'