I have C dll with this function:
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
);
CALLINGCONV is _stdcall. And I call it with C++:
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
HMODULE lib = LoadLibrary(_T("libname.dll"));
typedef int(__stdcall *FNPTR)(const char* pPin, unsigned long slot,
const char* pOut, const char* pFrom, const char* pTo,
const char* pSubject, const char* pHeaders, const char* pBody,
const char* pAttachments, unsigned int flags, int init);
FNPTR myfunc = (FNPTR)GetProcAddress(lib, "SMIMESignML");
if (!myfunc) {
printf("No function!\n");
} else {
int code = myfunc(NULL, 0, "", "", "", NULL, NULL, "", "", 0, 0);
cout << code << endl;
}
return 0;
}
The return value (code) is error code, it should be 2 with this parameters, Here it returns 65535 - it is unknown error.
When I run 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.
It returns 2, as expected. Why the same calls behave differently? What is the difference?
The OS is Win8x64, for compiling c++ code I use VS2013, the lib.dll is 32 bit.
The question related to this one: Inconsistent results calling DLL from JNA/C versus Pascal but this question doesn't include JNA and JAVA in the scope.
Unfortunately, I don't have sources of the dll library, only some header files.