Search code examples
javadelphidlljna

DLL (Delphi) with ADO (connection and query) not work in Java (using jna)


I have a dll (Delphi) that contains a ADOConnection and ADOQuery, but when running the called DLL in java (using JNA) appear some error information to the console (below):

A fatal error has been detected by the Java Runtime Environment:

Internal Error (0xeedfade), pid=4400, tid=3840

JRE version: 6.0_25-b06 Java VM: Java HotSpot(TM) Client VM (20.0-b11 mixed mode, sharing windows-x86 ) Problematic frame: C [KERNELBASE.dll+0x812f]

An error report file with more information is saved as: C:\Users\Mmn1\Documents\NetBeansProjects\FLMOPDL\hs_err_pid4400.log

If you would like to submit a bug report, please visit:
http://java.sun.com/webapps/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.

What bug is this? (I realized that it is only happens if I have a form or as a component in the case ADOConnection ADOQuery and if I remove these components and perform a simple function, it works normally).

Edit:

A similar exemple bellow:

library TESTLIB;
{$DEFINE TESTLIB}

uses
  System.SysUtils,
  System.Classes,
  TestInt in 'TestInt.pas',
  Vcl.Dialogs,
  sharemem,
  Data.DB, Data.Win.ADODB;

{$R *.res}

function MyReturn(x: Integer; Test: PTest): Boolean; stdcall;
var
  ado: TADOQuery;
begin
    Result := True;
    //ado := TADOQuery.Create(nil); <- With this i got a error!
end;

exports MyReturn;

begin
end.

interface in Java

public interface TestInt extends StdCallLibrary {
    TestInt INSTANCE = (TestInt)Native.loadLibrary("C:/test/Win32/Debug/TESTLIB", TestInt.class);

    class Test extends Structure {
        public String vResult;

        public Test() { }
        public Test(int x, Pointer p) {
            super(p);
            read();
        }
        protected List getFieldOrder() { return Arrays.asList(new String[] { "vResult" }); }
    }

    Boolean MyReturn(int x, Test test);
}

conclusion: when I use a component, this error raise. Thanks for help.


Solution

  • Re-edited:

    I got the solution. The problem was "how to" DLL had been made. Below the solution and I hope it helps more people:

    TESTLIB (Delphi)

    library TESTLIB;
    {$DEFINE TESTLIB}
    
    uses
      System.SysUtils,
      System.Classes,
      TestInt in 'TestInt.pas', //here's my unit (interface)
      Vcl.Dialogs,
      ADODB,
      Vcl.Forms;
    
      {$R *.res}
    
    
    function MyReturn(Test: PTest): Boolean; stdcall;
    var
      ado: TADOQuery;
      con: TADOConnection;
    begin
        con := TADOConnection.Create(Application);
        ado := TADOQuery.Create(Application);
    
        con.ConnectionString := 'Provider=SQLOLEDB.1;Password=aaa;Persist Security Info=True;User ID=aa;Initial Catalog=aa;Data Source=127.0.0.1';
        con.LoginPrompt := False;
        con.Open();
    
        if con.Connected = True then
            ShowMessage('connected ok')
        else
            ShowMessage('not connected');
    
        ado.Active := False;
        ado.Connection := con;
        ado.SQL.Clear;
        ado.SQL.Add('select lalala from table where codlala = 1');
        try
            ado.Open();
            Test^.vResult := PAnsiChar(AnsiString(ado.FieldByName('fantasia').AsString));
            Result := True;
        except
            on E: Exception do
            begin
              ShowMessage('error to open the query' + #13 + 'Error: ' + E.Message);
              Result := False;
            end;
        end;
    end;
    
    exports MyReturn;
    
    begin
    end.
    

    Interface Ole32 (java)

    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.platform.win32.WinNT.HRESULT;
    import com.sun.jna.win32.StdCallLibrary;
    
    public interface Ole32 extends StdCallLibrary {
    
        Ole32 INSTANCE = (Ole32) Native.loadLibrary("Ole32", Ole32.class);
    
        public HRESULT CoInitialize(Pointer p);
    
        public HRESULT CoUninitialize();
    
    }
    

    Special thanks to kobik and technomage about CoInitialize

    links that helped me:

    TADOConnection failing in application initialization section of Delphi ISAPI App:

    Add CoInitializing constants and improve documentation

    Using COM from Java via JNA