java - JNA vs. DLL (Delphi) -
i'm new(bie) here but, have "big problem"
i have dll (in delphi) , want access java. it's easy if use simple return or procedure in "body" (.dpr) of dll. need use interface because want use same code in desktop application , web application (using java). code bellow:
testlib.dll
library testlib; {$define testlib} uses system.sysutils, system.classes, testint in 'testint.pas'; {$r *.res} function myreturn(test: ptest): pchar; stdcall; begin result := 'im here!'; test^.vresult := 'test 123'; end; exports myreturn; begin end. interface testint.pas
unit testint; interface type ptest = ^ttest; ttest = record vresult: pchar; end; {$ifndef testelib} function myreturn(test: ptest): pchar; stdcall; {$endif} implementation {$ifndef testelib} function myreturn; external 'testlib.dll' name 'myreturn'; {$endif} end. and how access using simple application in delphi:
unit formmain; interface uses winapi.windows, winapi.messages, system.sysutils, system.variants, system.classes, vcl.graphics, vcl.controls, vcl.forms, vcl.dialogs, vcl.stdctrls; type tform1 = class(tform) button1: tbutton; procedure button1click(sender: tobject); private { private declarations } public { public declarations } end; var form1: tform1; implementation {$r *.dfm} uses testint; //the interface procedure tform1.button1click(sender: tobject); var test: ttest; //type declared in interface testint.pas begin showmessage(myreturn(@test)); //returns im here! showmessage(test.vresult); //test 123 end; end. i want access using java (jni, jna, etc... exemple welcome!)
thanks lot guys!
in general delphi dlls (using stdcall calling convention) accessed in same way access win32 apis. should able figure out appropriate type mappings examining sizes of delphi types , replacing them similarly-sized java types.
pchar maps java string, "interface" provided this:
public interface testint extends stdcalllibrary { testint instance = (testint)native.loadlibrary("testint", testint.class); class test extends structure { public string vresult; public test() { } public test(pointer p) { super(p); read(); } protected list getfieldorder() { return arrays.aslist(new string[] { "vresult" }); } } string myreturn(test test); } bear in mind david's warning returning strings; if delphi allocates memory, must later free memory, it's easier have caller (java) pass in allocated memory , have delphi write provided buffer.
Comments
Post a Comment