程序如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type TMB = function(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
MB: TMB;
inst: LongWord;
public end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
inst := LoadLibrary('user32.dll');
if inst <> 0 then MB := GetProcAddress(inst, 'MessageBoxA');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t,b: PChar;
begin
t := '标题';
b := '内容';
MB(0, b, t, 0);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeLibrary(inst); {记得释放}
end;end.编译没有错误,也可以生成出exe文件。 我运行生成的exe文件后出现了异常:project project1.exe raised exception class eaccessvioaltion with message 'access violation at address 00000000. read of address 00000000'.process stopped.use step or run to continue.是什么原因呢? 我的是番茄花园的操作系统。 在源程序里帮我改下,谢谢了!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type TMB = function(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
MB: TMB;
inst: LongWord;
public end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
inst := LoadLibrary('user32.dll');
if inst <> 0 then MB := GetProcAddress(inst, 'MessageBoxA');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t,b: PChar;
begin
t := '标题';
b := '内容';
MB(0, b, t, 0);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeLibrary(inst); {记得释放}
end;end.编译没有错误,也可以生成出exe文件。 我运行生成的exe文件后出现了异常:project project1.exe raised exception class eaccessvioaltion with message 'access violation at address 00000000. read of address 00000000'.process stopped.use step or run to continue.是什么原因呢? 我的是番茄花园的操作系统。 在源程序里帮我改下,谢谢了!
if inst <> 0 then
inst 断点跟下,看是否取到值了,
参数类型换成PAnsiChar试试
写法上唯一不规范的地方是如下,但是虽然不规范,依旧不会有错误procedure TForm1.Button1Click(Sender: TObject);
var
t,b: PChar;
begin
t := '标题';
b := '内容';
MB(0, b, t, 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
inst := LoadLibrary('user32.dll');
if inst <> 0 then //MB := GetProcAddress(inst, 'MessageBoxA');
@MB := GetProcAddress(inst, 'MessageBoxA');
end还有就是 查看你Dll 的参数方式 有可能不是 stdcall
对照参数类型
@MB := GetProcAddress(inst, 'MessageBoxA');
t,b: string;
begin
t := '标题';
b := '内容';
MB(0, PChar(b), PChar(t), 0);