vložil Radek Červinka
18. listopadu 2014 23:16
Pouze archivace kódu z originálního článku Delphi and .NET Interop with JVCL pro budoucnost.
function TfrmMain.AddFunction(aXValue: Integer; aYValue:Integer): Integer;
var
Host: TJclClrHost;
Obj: OleVariant;
begin
try
Host := TJclClrHost.Create('v4.0.30319');
Host.Start();
Obj := Host.DefaultAppDomain
.CreateInstance('Example1ClassLibrary',
'Example1ClassLibrary.Example1')
.UnWrap();
Result := Obj.AddFunction(aXValue, aYValue);
Host.Stop();
Host.Free;
Except
on E : Exception do
begin
ShowMessage('Exception class name = '+E.ClassName + ' ' + 'Exception message = '+E.Message);
end;
end;
end;
namespace Example1ClassLibrary
{
[ComVisible(true)]
public class Example1
{
public int AddFunction(int aXValue, int aYValue)
{
return aXValue + aYValue;
}
}
}
Druhý příklad
function TfrmMain.AddFunction(aXValue: Integer; aYValue:Integer): Integer;
var
Fads: TJclClrAppDomainSetup;
Fad: TJclClrAppDomain;
Ov: OleVariant;
obj: _ObjectHandle;
begin
try
Fads := FClrhost.CreateDomainSetup;
Fads.ApplicationBase := '..\Example2\Debug\Win32\';
Fad := FClrHost.CreateAppDomain('myNET', Fads);
obj := (Fad as _AppDomain).CreateInstanceFrom('Example2ClassLibrary.dll',
'Example2ClassLibrary.Example2');
ov := obj.Unwrap;
result := ov.AddFunction(aXValue, aYValue);
except
on E : Exception do
begin
ShowMessage('Exception class name = '+E.ClassName +
' ' + 'Exception message = '+E.Message); end;
end;
end;