我有这个功能的C dll:
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是_stdcall。 我用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;
}
返回值(代码)是错误代码,使用此参数应为2,此处返回65535 - 这是未知错误。
当我在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.
按预期返回2。 为什么相同的调用表现不同?有什么区别?
操作系统是Win8x64,用于编译我使用VS2013的c ++代码,lib.dll是32位。
与此相关的问题: Inconsistent results calling DLL from JNA/C versus Pascal 但是这个问题不包括范围内的JNA和JAVA。
不幸的是,我没有dll库的来源,只有一些头文件。