Forum > Windows
Using C# DLL in Lazarus
JohnRambo:
At first I admit that in C # I am green like chives. I want to prepare a C# DLL using the UnmanagedExports tool (https://www.nuget.org/packages/UnmanagedExports) in the C # DLL. Here's an example by author:https://stackoverflow.com/a/2055508/1919267 .
So far ok, managed to produce a DLL and use it in Delphi. But there is a problem: when I want to use the same file in Lazarus, it returns nothing. The code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var adder : IDotNetAdder;begin try CreateDotNetAdder(adder); Writeln('4 + 3 = ', adder.Add3(4)); returns me 0 (so I am thinking that DLL response is nil). Is there any solution to fix that? Should I need to fix my pascal code or fix that DLL?
avra:
Next time you cross post I will hold my comments... >:(
Please do not cross post in the future. ;D
tudi_x:
@avra - the deletion of posts does not seem to work. maybe he opened a new one due to code not inserted as recommended in the first place.
@rambo - i think you need to have a handler for the dll and define the external function, something like
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TGetDeviceInfo = function(AIndex: integer): PTDeviceInfoRecord; cdecl;
after you define the handler and the function you can use it.
JohnRambo:
Sorry for double topic but I can't delete topic in wrong category. I cant use COM, because I need to use old version of Lazarus (without ActiveX).
In my code I declare procedure in static way:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var Form1: TForm1; procedure createdotnetadder(out instance:IDotNetAdder); stdcall external 'DelphiNETstd.dll' name 'createdotnetadder';implementation{ TForm1 }When I try to do it dynammicaly nothing work, only more problems. I don't understand that Delphi do it perfectly, but Lazarus not. In attachments I put .dll file and my pas unit. I don't asking about do it for me but to show me the way
JohnRambo:
OK I made it. :D
Firstly we need to get some information: https://stackoverflow.com/a/9353800
"In C#/C++ you will need to define the Result as out Parameter, in order to maintain binary code compatibility of stdcall calling conventions" - thats the key. In my C# code I was use return. For Delphi its ok, but for Lazarus it is problematic. So I retyped my c# code from:
--- Code: C# [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class DotNetAdder : DelphiNET.IDotNetAdder { public void Add3(int left) { return= left + 3; } }
to
--- Code: C# [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class DotNetAdder : DelphiNET.IDotNetAdder { public void Add3(int left,out int result) { result= left + 3; } }And change my pascal code from:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//interface: type IDotNetAdder = interface ['{ACEEED92-1A35-43fd-8FD8-9BA0F2D7AC31}'] function Add3(left : integer):integer; stdcall; end; //execution:var adder : IDotNetAdder;begin try CreateDotNetAdder(adder); Writeln('4 + 3 = ', adder.Add3(4));
to
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- //interface: type IDotNetAdder = interface ['{ACEEED92-1A35-43fd-8FD8-9BA0F2D7AC31}'] function Add3(left : integer;out result:integer); stdcall; end; //execution: CreateDotNetAdder(adder); i:=4; finally adder.Add3(i,x); showmessage('4 + 3 = '+ (inttostr(x)));
Navigation
[0] Message Index
[#] Next page