大概的一个过程,从生成到调用
一. Dll的生成:
1 把工程文件中的Programe 改为library
2 加输出函数:如:
library myDll;uses
SysUtils,
Classes,
Base64 in 'Base64.pas';{$R *.RES}
exports
enCode,Decode;//此两函数已经在单元文件中定义,不需要带调用时的参数二.Dll的调用:var
Form1: TForm1;
function Encode(s:PChar;d:PChar;var dn:integer;flg:boolean):boolean; stdcall;external 'myDll.dll' ;
function Decode(s:PChar;d:PChar;
var dn:integer;flg:boolean):boolean; stdcall;external 'myDll.dll';
implementation然后就可以调用普通过程一样调用了
一. Dll的生成:
1 把工程文件中的Programe 改为library
2 加输出函数:如:
library myDll;uses
SysUtils,
Classes,
Base64 in 'Base64.pas';{$R *.RES}
exports
enCode,Decode;//此两函数已经在单元文件中定义,不需要带调用时的参数二.Dll的调用:var
Form1: TForm1;
function Encode(s:PChar;d:PChar;var dn:integer;flg:boolean):boolean; stdcall;external 'myDll.dll' ;
function Decode(s:PChar;d:PChar;
var dn:integer;flg:boolean):boolean; stdcall;external 'myDll.dll';
implementation然后就可以调用普通过程一样调用了
解决方案 »
- 目标IP地址和目标PORT端口
- 问个有关于操作数据库速度的问题
- 关于TIdThread
- 找钟兆超同学
- 帮帮小弟把.很简单的问题!!一直没搞懂!!!
- http://www.new7wonders.com/ 刚刚去看了,是在选世界七大遗迹,中国长城和布达拉宫,快去投票阿
- 如何在Memo或其他控件中只选择鼠标点中的字符(只选一个!!,即鼠标点中的字符),象ultraedit一样。
- delphi 连接 oracle,刷新databriage时,界面会停顿一下,如何解
- 在程序中怎样才能读取DBGrid的某个单元格的内容
- 急!帮忙看一下这个程序,关于对数据库中关键字重复的问题。谢谢.(昨天帖了,没人回答)
- 高分求DELPHI+SQLSERVER源代码?(WHXXR@163。COM)
- TAdoStoredProc的参数的使用
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TGetDouble=function(i:string): string;stdcall;
var
Form1: TForm1;implementation{$R *.dfm}
//在delphi中动态调用DLL
procedure TForm1.Button1Click(Sender: TObject);
var
DLLHandle: THandle;
Func: TGetDouble;
D:string;
begin
Try
DLLHandle := LoadLibrary('zmjpass.dll');
if DLLHandle <> null then
begin
@Func := GetProcAddress(DLLHandle, 'pass');
//if Assigned(@Func) then
if @Func<>nil then
begin
D := Func('abc');
showmessage(D);
end;
end
else
begin
showmessage('无法回放,不能调用DLL库!,请仔细检查相关设置!');
end;
finally
FreeLibrary(DLLHandle);
end;
end;end.
输入参数是(userid:int;pass:string)
方法是inpass,输出参数是(istrue:int;outpass:string);是什么意思?
DLL引出的函数(引出的方法见Delphi Help中exports的帮助)最好不要用缺省调用方式,建议使用stdcall,cdecl也可以.
尽可能不使用变长字符串、动态数组等需要内存管理模块的变量作为参数和返回值。
不要把局部的字符串变量转换为Pchar返回,因为它将被释放。
调用方法(以stdcall方式为例):
1:DLL:
library TestDll
uses
SysUtils,
Classes;{$R *.RES}
function SafeStrCpy(Dest:PChar;Source:PChar;MaxLen:Integer):Integer;stdcall;
begin
if MaxLen >StrLen(Source) then
MaxLen := StrLen(Source);
Move(Source^,Dest^,MaxLen);0D
Result := MaxLen;
end;exports
SafeStrCpy;
begin
end.
2:静态调用
...
function SafeStrCpy(Dest:PChar;Source:PChar;MaxLen:Integer):Integer;stdcall;external 'TestDll.Dll';
...
var
Dest:String;
CopyLen:Integer;
begin
Dest := StringOfChar(' ',100);
CopyLen := SafeStrCpy(PChar(Dest),Edit1.Text,100);
Dest := Copy(Dest,1,CopyLen);
end;
3:动态调用:
...
type
TSafeStrCpy=function(Dest:PChar;Source:PChar;MaxLen:Integer):Integer;stdcall;
...
var
SafeStrCpy:TSafeStrCpy;
0A DllHandle:THandle;
Dest:String;
CopyLen:Integer;
begin
DllHandle := LoadLibrary(PChar('TestDll.Dll'));
if DllHandle = 0 then
raise Exception.Create('加载TestDll.DLL失败');
SafeStrCpy := TSafeStrCpy(GetProcAddress(DllHandle,PChar('SafeStrCpy')));
if not Assigned(SafeStrCpy) then
raise Exception.Create('在TestDll.dll中找不到入口SafeStrCpy');
Dest := StringOfChar(' ',100);
CopyLen := SafeStrCpy(PChar(Dest),PChar(Edit1.Text),100);
Dest := Copy(Dest,1,CopyLen);
end;
____ ____
\ p \ / g /
\ l \_/ n /
\ a o /
\ i s /
\ n /
\_/