关于调用dll返回时出错,求助!(100分)

W

wsj1208

Unregistered / Unconfirmed
GUEST, unregistred user!
下面这个动态链接库程序调用返回时出错,请各位大侠出手相助,谢谢!
动态链接库程序如下:
library bhcomm;
uses
SysUtils,
Classes,
Windows;
{$R *.RES}
function GeneCode:integer;far;stdcall;export;
var
SerialNum : pdword;
a, b : dword;
Buffer : array [0..255] of char;
sdisk:string;
begin
//获得C盘序列号:
if GetVolumeInformation('c:/', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then
sdisk := IntToStr(SerialNum^);
if sdisk='12345' then
result:=0
else
result:=1;
end;

exports
GeneCode;
end.

调用程序如下:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function GeneCode:integer;stdcall;external 'bhcomm.dll';
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
showmessage(inttostr(GeneCode));
end;
end.
 
在DLL中声明的函数后面是FAR,STDCALL
在程序中声明DLL中的函数时没有FAR,加上就可以了
 
多谢hfhuga的回答,已经加上far试过了,可还是不行,出错提示没有变化。
 
换种调用方式。。
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
hdl:thandle;
function GeneCode:integer;stdcall;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function GeneCode:integer;stdcall;external 'bhcomm.dll';
procedure TForm1.BitBtn1Click(Sender: TObject);
begin

hdl:=loadlibrary(pchar('bhcomm.dll'));
@GeneCode:=getprocaddress(hdl,pchar('bhcomm.dll'));
GeneCode(application.Handle);
showmessage(inttostr(GeneCode));
end;
end.
大概的路子,没有测试,你试试看吧!!
 
是你调用的API引起的.
GetVolumeInformation('c:/', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0)
在DLL中,有时调用API会出错,具体的说不清楚.
 
我觉得9861说的对,如果我不调用GetVolumeInformation函数时,就不会出错了。
可是我必须要调用这个函数,怎末办呢?各位快想想办法吧!
 
至少你犯了一个错误
GetVolumeInformation('c:/', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0)
SerialNum是一个PDWORD类型,也就是一个指针类型,但是你却未初始化
你不需要SERIALNUM那么就该
GetVolumeInformation('c:/', Buffer, SizeOf(Buffer), NIL, a, b, nil, 0)
如果你需要
那么你该
VAR A,B,C:DWORD;
...........
begin
SERIALNUM:=@C;
具体是否还有其他地方出错我不清楚.
造成这个API这么容易被引用错的原因在于
SERIALNUM是一个PDWORD类型,但是允许为NIL,所以
DELPHI在WINDOWS.PAS里的申明就没有使用VAR SERIALNUM:DWORD来替代.
倘若他使用VAR SERIALNUM:DWORD我想大家都不会出现这个错误了
VAR SERIALNUM:DWORD其实等同于SERIALNUM:pDWORD,只是后一种申明方式可以很方便
的使用NIL(也就是不返回SERIALNUM)
 
按照wenyue的指点,问题圆满解决,非常感谢各位的帮助,分配工资!
 

Similar threads

I
回复
0
查看
681
import
I
I
回复
0
查看
616
import
I
I
回复
0
查看
486
import
I
顶部