我用c写了一组函数,编译成动态连接库,然后我用delphi里把它们封装到一个类里面,
作为类方法使用,但创建类对象->调用方法这里总是出错。
把这组函数作为全局函数声明后调用就没问题,why?
比如在vc里
EXPORTS
hello @1
在delphi里
type THello=class(TObject)
public
function hello(): integer;stdcall;external 'hello.dll';name 'hello';
end;
这样使用:
var
a:THello;
begin
a:=Thello.create;
a.hello;//出错;
...
作为类方法使用,但创建类对象->调用方法这里总是出错。
把这组函数作为全局函数声明后调用就没问题,why?
比如在vc里
EXPORTS
hello @1
在delphi里
type THello=class(TObject)
public
function hello(): integer;stdcall;external 'hello.dll';name 'hello';
end;
这样使用:
var
a:THello;
begin
a:=Thello.create;
a.hello;//出错;
...
主要是调用方式,C中默认的调用是cdecl,而Pascal默认得是register,Win32系统的函数大多都是stdcall。
另外,我以前写VC程序的时候,发现VC会给Export的函数加一段乱七八糟的字符串,这个在声明的时候也要包括进去。
这句有什么不对吗?
function hello(): integer;stdcall;external 'hello.dll' name 'hello';
c:
extern "C" __declspec(dllexport)
int WINAPI Double (int n)
{
return n * 2;
}extern "C" __declspec(dllexport)
int WINAPI Triple (int n)
{
return n * 3;
}__declspec(dllexport)
int WINAPI Add (int a, int b)
{
return (a + b);
}
================delphi======
function Add (A, B: Integer): Integer;
stdcall; external 'CPPDLL.DLL' name '@Add$qqsii';//您说的就是这个吧?
function Double (N: Integer): Integer;
stdcall; external 'CPPDLL.DLL' name 'Double';
function Triple (N: Integer): Integer;
stdcall; external 'CPPDLL.DLL';//这里没有那些乱东西了
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.unit Unit1;{略}implementation{$R *.dfm}type
THello = class(Tobject)
public
function Hello(H: HWND; Text: PChar): Integer;
end;function Hello(H: HWND; Text: PChar): Integer; external 'Project2.dll' name 'Hello';function THello.Hello(H: HWND; Text: PChar): Integer;
begin
Result := Unit1.Hello(H, Text);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
with THello.Create do try
Hello(Handle, 'OK');
finally
Free;
end;
end;
{这里的Project1是一个EXE,Unit1与之对应
Project2是一个DLL,Unit2与之对应
其中Project1和Project2与一般的EXE和DLL没有特别不同之处。
}
end.library Project2;
uses
SysUtils,
Classes,
Unit2 in 'Unit2.pas';
{$R *.res}
exports
Hello;
begin
end.unit Unit2;interfaceuses
Windows;function Hello(H: HWND; Text: PChar): Integer;
implementation
function Hello(H: HWND; Text: PChar): Integer;
begin
Result := MessageBox(H, Text, 'Hello Title', MB_YESNO);
end;
end.