library MyDLL;
uses
SysUtils,Registry,Regstr,
Classes;
Function ChkBoolean():Boolean;stdcall;
Begin
Result:=True;
End;procedure WriteReg(UserName:pchar);stdcall;
var
Reg:TRegistry;
begin
Reg:=TRegistry.Create;
errFind:=False;
try
Reg.RootKey:=HKEY_LOCAL_MACHINE;
If Reg.OpenKey('SOFTWARE\MyDLL\'+UserName,True) then
Reg.WriteString('Name',UserName);
finally
Reg.CloseKey;
Reg.Free;
end;
end;
end.
----------------------------------------上面是DLL
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
type
TMyProcedure=procedure(UseNam:pchar);stdcall;
function ChkBoolean():Boolean; external MyDLL.DLL';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
HInst:Thandle;
FPointer:TFarProc;
MyProc:TMyProcedure;
Begin
If ChkBoolean Then
//直接返回函数值,为真,继续运行……
Begin
HInst:=LoadLibrary('MyDLL.DLL');
if HInst>0 then
try
FPointer:=GetProcAddress(Hinst,Pchar('WriteReg'));
//GetProcAddress API函数用于找到DLL中的函数名,并返回一个指针
if FPointer<>nil then
begin
MyProc:=TMyProcedure(FPointer);
//将该函数指针转换成相应的数据类型并调用它
Try
MyProc(Edit1.Text);
except on E:Exception do
ShowMessage(E.Message);
end;
end
else
ShowMessage('DLL procedure not found!');
finally
FreeLibrary(HInst);
end
else
ShowMessage('Library not found!');
End;
end;
----------------------------------上面是窗體調用DLL
運行后有好幾處出錯.但是我只知道一處怎么改.1>是在result:true--------少了一個=
其它請幫忙.
uses
SysUtils,Registry,Regstr,
Classes;
Function ChkBoolean():Boolean;stdcall;
Begin
Result:=True;
End;procedure WriteReg(UserName:pchar);stdcall;
var
Reg:TRegistry;
begin
Reg:=TRegistry.Create;
errFind:=False;
try
Reg.RootKey:=HKEY_LOCAL_MACHINE;
If Reg.OpenKey('SOFTWARE\MyDLL\'+UserName,True) then
Reg.WriteString('Name',UserName);
finally
Reg.CloseKey;
Reg.Free;
end;
end;
end.
----------------------------------------上面是DLL
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
type
TMyProcedure=procedure(UseNam:pchar);stdcall;
function ChkBoolean():Boolean; external MyDLL.DLL';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
HInst:Thandle;
FPointer:TFarProc;
MyProc:TMyProcedure;
Begin
If ChkBoolean Then
//直接返回函数值,为真,继续运行……
Begin
HInst:=LoadLibrary('MyDLL.DLL');
if HInst>0 then
try
FPointer:=GetProcAddress(Hinst,Pchar('WriteReg'));
//GetProcAddress API函数用于找到DLL中的函数名,并返回一个指针
if FPointer<>nil then
begin
MyProc:=TMyProcedure(FPointer);
//将该函数指针转换成相应的数据类型并调用它
Try
MyProc(Edit1.Text);
except on E:Exception do
ShowMessage(E.Message);
end;
end
else
ShowMessage('DLL procedure not found!');
finally
FreeLibrary(HInst);
end
else
ShowMessage('Library not found!');
End;
end;
----------------------------------上面是窗體調用DLL
運行后有好幾處出錯.但是我只知道一處怎么改.1>是在result:true--------少了一個=
其它請幫忙.
exports中
ChkBoolean,
writereg
exports
ChkBoolean,
WriteReg;程序部分:
//将该函数指针转换成相应的数据类型并调用它
Try
MyProc(Edit1.Text);
改成如下:
Try
MyProc(PChar(Edit1.Text));