我做了个例子,请各位指教。
DLL代码如下:
library DllExample;
uses
SysUtils,
Classes,
Graph in 'Graph.pas';
exports
ChangeSqlString;
{$R *.RES}
begin
end.
----------------
和unit Graph;
interface
uses
Windows, Messages, Classes, Graphics, Dialogs;
function ChangeSqlString(str:string):string;export;
implementation
function ChangeSqlString;
begin
result:='it is changed.';
end;
end.
调用DLL例子程序如下:
program Project2;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
-------------------------
和unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
test1: TMenuItem;
procedure test1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.test1Click(Sender: TObject);
var
sqlstr : string;
str:string;
DllName:string;
Buffer:array[0..127] of char;
Lib:Thandle;
ChangeSqlString:function(str:string):string;
begin
sqlstr := 'select count(*) from dict_code where par';
DllName:='DllExample.dll';
StrPCopy(Buffer,DllName);
Lib:=LoadLibrary(Buffer);
if Lib<>0 then
try
ChangeSqlString:=GetProcAddress(Lib,'ChangeSqlString');
str:=ChangeSqlString(sqlstr);
ShowMessage(str);
finally
FreeLibrary(Lib);
ShowMessage('fdghdf');
end
else
ShowMessage('load dll error');
end;
end.