动态连接库函数能封装成一个类吗?(200分)

  • 主题发起人 主题发起人 张校风
  • 开始时间 开始时间

张校风

Unregistered / Unconfirmed
GUEST, unregistred user!
我用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;//出错;
...
 
这很难吗?
 
这样做未免太简单了吧。您可以试一试:

type
THello=class(TObject)
public
function Hello:Integer;
end;
...
function DLL_Hello:Integer;stdcall;external 'hello.dll';name 'hello';

function THello.Hello:Integer;
begin
Result:=DLL_Hello;
end;
 
我之所以想把dll函数封装在类里面就是不想让他作为全局函数出现,但楼上的写法.....
 
一、我修改了一下:
将全局函数放在实现部分。其作用域仅在该单元内。其封装性应该是可以接受的。
该单元就放这一个类。以前我就是这么做的。
unit hello_
interface
type
THello=class(TObject)
public
function Hello:Integer;
end;

implementation
...
function DLL_Hello:Integer;stdcall;external 'hello.dll';name 'hello';

function THello.Hello:Integer;
begin
Result:=DLL_Hello;
end;

二、您可以考虑使用 接口 进行封装。
type

IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size: Integer): Pointer; stdcall;
function Realloc(P: Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P: Pointer); stdcall;
function GetSize(P: Pointer): Integer; stdcall;
function DidAlloc(P: Pointer): Integer; stdcall;
procedure HeapMinimize; stdcall;

end;
 
后退
顶部