很简单的问题,老是出错,不知道为什么5555555555555~~~~~~~~~``(10分)

  • 主题发起人 主题发起人 zhangweizw
  • 开始时间 开始时间
Z

zhangweizw

Unregistered / Unconfirmed
GUEST, unregistred user!
我编了一个很简单的DLL,求两数之和,静态调用的时候算出来的结果是对的,可是动态调用
的时候算出来的结果乱七八糟,为什么啊。动态调用要注意一些什么问题?
哪位大虾能帮我看看错在哪里?谢谢了啊

procedure TForm1.Button1Click(Sender: TObject);
type
tfunc=function(x,y:integer):integer;stdcall;
var
alib:thandle;
func:tfunc;
all:integer;
begin
alib:=loadlibrary('dll');
if alib>=32 then
begin
func:=getprocaddress(alib,'add');
all:=func(strtoint(edit1.text),strtoint(edit2.text));
label1.Caption :=inttostr(all);
freelibrary(alib);
end;
end;
 
unit Unit1;

interface

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

type
TPluginDescribe = Function:Boolean; stdcall;
TPluginDrawDescribe = procedure(DC:HDC;Rect:TRect); stdcall;
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
LibHandle: HMODULE;
DwarProc: TPluginDrawDescribe;
procedure LoadPlugins;
procedure LoadPlugin(sr: TSearchRec);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
LibHandle:=0;
LoadPlugins;
end;

procedure TForm1.LoadPlugins;
var
sr: TSearchRec;
path: string;
Found: Integer;
begin
path := ExtractFilePath(Application.Exename);
try
Found := FindFirst(path + '*.DLL', 0, sr);
while Found = 0 do
begin
LoadPlugin(sr);
Found := FindNext(sr);
end;
finally
FindClose(sr);
end;
end;

procedure TForm1.LoadPlugin(sr: TSearchRec);
var
iLibHandle: HMODULE;
pDescribeProc: TPluginDescribe;
pDwarProc: TPluginDrawDescribe;
begin
iLibHandle := LoadLibrary(Pchar(sr.Name));
if iLibHandle <> 0 then
begin
try
pDescribeProc := GetProcAddress(iLibHandle, 'loadpicture');
pDwarProc := GetProcAddress(iLibHandle, 'drawpicture');
if Assigned(pDescribeProc) and Assigned(pDwarProc) then
ListBox1.Items.Add(sr.Name);
finally
FreeLibrary(iLibHandle);
end;
end else
ShowMessage('loading Dll file error!');
end;

procedure TForm1.ListBox1DblClick(Sender: TObject);
var pDescribeProc: TPluginDescribe;
begin
if ListBox1.ItemIndex<>-1 then
begin
if LibHandle<>0 then
FreeLibrary(LibHandle);
DwarProc:=nil;
LibHandle := LoadLibrary(PChar(ListBox1.Items[ListBox1.ItemIndex]));
if LibHandle <> 0 then
begin
try
pDescribeProc := GetProcAddress(LibHandle, 'loadpicture');
if pDescribeProc then
begin
DwarProc := GetProcAddress(LibHandle, 'drawpicture');
Invalidate;
end;
Except
FreeLibrary(LibHandle);
LibHandle:=0;
end;
end
else
ShowMessage('loading Dll file error!');
end;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
try
if (LibHandle<>0) and Assigned(DwarProc) then
DwarProc(Self.Canvas.Handle,Self.ClientRect);
Except
if LibHandle<>0 then
begin
FreeLibrary(LibHandle);
LibHandle:=0;
end;
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if LibHandle<>0 then
begin
FreeLibrary(LibHandle);
LibHandle:=0;
end;
end;

end.


这一个关于简单插件的例子,完整的代码和执行程序请到
www.aidelphi.com
下载
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
979
import
I
I
回复
0
查看
721
import
I
后退
顶部