Inconsistent results calling DLL from JNA/C versus

2019-09-01 15:38发布

问题:

I have a c++ dll library with header files provided, without implementation. And I implement JNA call for this library functions. And I have the problem with only 1 function (other, even similar works fine). This is declaration from .h file:

int CALLINGCONV SMIMESignML(
  const char* pin,
  unsigned long slot,
  const char* szOutputFilePath,
  const char* szFrom,
  const char* szTo,
  const char* szSubject,
  const char* szOtherHeaders,
  const char* szBody,
  const char* szAttachments,
  unsigned long dwFlags,
  int bInitialize
);

Java code:

public interface Dll extends StdCallLibrary {
  public String JNA_LIBRARY_NAME = "libname.dll";
  int SMIMESignML(String pPin, int slot, String pOut, String pFrom, String pTo,
    String pSubject, String pHeaders, String pBody, String pAttachments, int flags,
    int init);
}

public class Test {
  private static final Dll dll = (Dll) Native.loadLibrary(Dll.JNA_LIBRARY_NAME, Dll.class, W32APIOptions.ASCII_OPTIONS);

  public static void main(String[] args) {
    String pOut = "";
    String pFrom = "";
    String pTo = "";
    String pBody = "";
    String pAttachments = "";

    int code = dll.SMIMESignML(null, 0, pOut, pFrom, pTo, null, null, pBody, pAttachments, 0, 0);
    System.out.println(code);
  }
}

The function should return different int error codes, but it always return code 0xFFFF.

I can check it with the same code in Pascal:

unit dll;
interface
const
  JNA_LIBRARY_NAME = 'libname.dll';

function SMIMESignML(pPin: PChar; slot: integer; pOut: PChar; pFrom: PChar; pTo: PChar;
  pSubject: PChar; pHeaders: PChar; pBody: PChar; pAttachments: PChar; flags: integer;
  init: integer): integer; stdcall; external JNA_LIBRARY_NAME;
implementation
end.

program Hello;
uses dll;
var 
  code: integer;
begin
  code := SMIMESignML(nil, 0, '', '', '', nil, nil, '' , '', 0, 0);
  writeln(code);
end.

Pascal code returns 2, Java code return 65535. Moreover, Pascal std calls work right, changing arguments we get different error codes (0=OK and others), but Java with the same arguments is not working, it always returns 0xFFFF. How can I debug it to understand the problem?

P.S. Moreover, in the same library I has this function and it works from JNA without any problems:

int CALLINGCONV PKCS7SignML(
  const char *pin,
  unsigned long slot,
  const char* szInputFileName,
  const char* szOutputFileName,
  int bInitialize);

The OS is Win8 x64, JavaOracle7x86, library is x32. "unsigned long" should not be the problem, as it should be 4 bytes on windows.

What am I doing wrong, if the same STD call returns different results in this two examples? And How can I debug it?

标签: java c jna stdcall