关于delphi中动态调用dll时出现的问题

来源:百度知道 编辑:UC知道 时间:2024/06/10 16:56:26
在delphi编程时,需要用到动态调用dll文件,但是却老是出错提示,错误提示是access violation at address 00404240 in module 'project1.exe' .read of address 00E00BCC.
意思是非法访问某个内存地址。为了测试这个错误,我在整个程序里就是用了这么一个功能,就是调用dll文件,但是还是要出错。我把原代码贴出来,希望高手们帮我看看,究竟怎么回事。首先排除dll文件名及函数名以及dll文件本身的问题,因为不管什么dll文件都出现一样的问题。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tenc=function(Source,Key:string):string;stdcall;
var
Form1: TForm1;
hd_csqm:THandle;
crt_sqm:Tenc;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
hd_csqm:=LoadLibrary('evcrypt.dll');
try
if hd_csqm<>

hd_csqm:=LoadLibrary('evcrypt.dll');
try
if hd_csqm<>0 then
begin
crt_sqm:=GetProcAddress(hd_csqm,'EncryptString');
showmessage(crt_sqm('123456','123456787909'));
end
else
showmessage('找不到函数');
finally
FreeLibrary(hd_csqm);
end;

这样子试试

应该是参数的问题,你可以试试调一个没有参数的函数,或者参数采用默认值形式的函数;
如果是参数的问题的话,可能要把String 改成pchar

还有一个问题就是 你的DLL里面 uses 第一个是不是 ShareMem

这个绝对是函数声明的问题。
好好看看DLL的函数的声明。