这是自己写的一个DelphiDll代码如下:
library MyDll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Windows,
Unit1 in 'Unit1.pas' {Form1};
type
TStateCallback=procedure(iState:Integer);cdecl;
{$R *.res}
function add(x,y :Integer) :Integer ;stdcall;
begin
Result :=x+y;
Form1.biaoshi;
end;
function RegisterStateCallback(FCallback:TStateCallback) :Integer ;stdcall;
begin
Form1.CallBack(FCallback);
end;
procedure MyDLLProc(dwReason: DWORD);
begin
case dwReason of
DLL_Process_Attach: //the process into
begin
// OpenLogFile;
Form1 :=TForm1.Create(nil);
end;
DLL_Process_Detach://the process out
begin
if Form1<>nil then
begin
FreeAndNil(Form1);
end;
// AddLog('Hardware control dynamic libraries have withdrawn');
end;
DLL_Thread_Attach: //the thread into
begin
//
end;
DLL_Thread_Detach:
begin
//
end;
end;
end;
exports
add,
RegisterStateCallback;
begin
DLLProc := @MyDLLProc;
MyDLLProc(DLL_Process_Attach);
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TQuery=procedure(Bt :Integer);cdecl;
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
function biaoshi :Integer;
procedure CallBack(acallback :TQuery);
end;
var
Form1: TForm1;
aquery :TQuery;
implementation
{$R *.dfm}
function TForm1.biaoshi :Integer;
var
str :Integer;
begin
str :=555555;
if Assigned(aquery) then
begin
aquery(str);
end;
end;
procedure TForm1.CallBack(acallback :TQuery);
begin
aquery :=acallback;
end;
end.
C++调用delphi Dll的测试代码如下:
#include <stdio.h>
#include <windows.h>
typedef int (_stdcall *TStateCallback)(int iState) ;
typedef int(_stdcall *lpAddFun)(int, int); //宏定义函数指针类型
typedef int(_stdcall *IpRegisterStateCallbackFun)(TStateCallback);
void __stdcall test (int bt );
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
IpRegisterStateCallbackFun RegisterStateCallbackFun;
hDll = LoadLibrary("MyDll.dll");
if (hDll != NULL)
{
RegisterStateCallbackFun = (IpRegisterStateCallbackFun)GetProcAddress(hDll, "RegisterStateCallback");
if (RegisterStateCallbackFun != NULL)
{
int result = RegisterStateCallbackFun(test);
printf("%d\n", result);
}
addFun = (lpAddFun)GetProcAddress(hDll, "add");
if (addFun != NULL)
{
int result = addFun(3, 3);
printf("%d\n", result);
}
void _stdcall test( int bt);
{
int bt;
printf("%d\n", bt);
}
FreeLibrary(hDll);
}
return 0;
}
2 个解决方案
#1
type
TStateCallback=procedure(iState:Integer);cdecl;改成stdcall
TStateCallback=procedure(iState:Integer);cdecl;改成stdcall
#2
C++代码中int result = RegisterStateCallbackFun(test);也会报错不知道这样写是不是有问题谢谢
#1
type
TStateCallback=procedure(iState:Integer);cdecl;改成stdcall
TStateCallback=procedure(iState:Integer);cdecl;改成stdcall
#2
C++代码中int result = RegisterStateCallbackFun(test);也会报错不知道这样写是不是有问题谢谢