读书人

问个新手有关问题 delphi 编写的dll 怎

发布时间: 2012-02-19 19:43:39 作者: rapoo

问个新手问题 delphi 编写的dll 如何进行调用呢?
编写好了DLL不知道如何调用,请高手指点下~~ 哈哈 太菜了我

Delphi(Pascal) code
library MjWl;{ Important note about DLL memory management: ShareMem must be the  first unit in your library's USES clause AND your project's (select  Project-View Source) USES clause if your DLL exports any procedures or  functions that pass strings as parameters or function results. This  applies to all strings passed to and from your DLL--even those that  are nested in records and classes. ShareMem is the interface unit to  the BORLNDMM.DLL shared memory manager, which must be deployed along  with your DLL. To avoid using BORLNDMM.DLL, pass string information  using PChar or ShortString parameters. }uses  SysUtils,  adodb,  db,  math,  Dialogs,  Mask,  Classes;const  cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';//四舍五入function RoundToEx(const AValue: Double; const ADigit: TRoundToRange; AFlag: Boolean): Double;var  LFactor: Double;begin  LFactor := IntPower(10, ADigit);  if AFlag then    Result := Round(AValue / LFactor) * LFactor //需要四舍五入  else    Result := Trunc(AValue / LFactor) * LFactor; //不需要四舍五入!!!end;//36进制转换function IntPower(Base, Exponent: Integer): Integer; {  返回 Base 的 Exponent 次方  }var  I: Integer;begin  Result := 1;  for I := 1 to Exponent do    Result := Result * Base;end; { IntPower }//36进制转换function DigitToInt(mDigit: string; mScale: Byte): Integer;{  返回进制表示转换成整数 ;mScale 指定多少进制  }var  I: Byte;  L: Integer;begin  Result := 0;  mDigit := UpperCase(mDigit);  L := Length(mDigit);  for I := 1 to L do    Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *      IntPower(mScale, I - 1);end; { DigitToInt }//执行sql语句返回记录集 TDataSourcefunction exSql_result_Stat(sqlstring: string; conn: tadoquery): TDataSource;var  qry: tadoquery;  ds: TDataSource;begin  ds := TDataSource.Create(nil);  qry := tadoquery.Create(nil);  qry.Connection := conn.Connection;  qry.SQL.Clear;  qry.SQL.Text := 'sqlstring';  qry.Open;  ds.DataSet := qry;  result := ds;  qry.Close;  ds.Free;end;//字符串比大小较函数//s1 标杆起//s2 标杆止//s3 比较对象//返回值 0 相等 1 比较对象大于S2标杆范围 2 比较对象小于S1标杆起 3比较对象在S1、s2标杆之间  4 比较对象不在标杆区间中function ComParString(s1, s2, s3: string): integer;var  i, s: integer;  Okin, Okin2: boolean;begin  Okin := FALSE;  if S3 = S2 then  begin    Result := 0;    exit;  end;  if s1 = s3 then  begin    Result := 0;    exit;  end;  if length(s1) > length(s3) then    Result := 2;  if length(s2) < length(s3) then    Result := 1;  if length(s1) = length(s3) then  begin    for I := 1 to length(s1) do    begin      if StrToIntDef(IntToStr(DigitToInt(s1[i], 36)), 0) < StrToIntDef(IntToStr(DigitToInt(s3[i], 36)), 0) then      begin        OkIn := true;        BREAK;      end;      if StrToIntDef(IntToStr(DigitToInt(s1[i], 36)), 0) > StrToIntDef(IntToStr(DigitToInt(s3[i], 36)), 0) then      begin        OkIn := FALSE;        BREAK;      end;    end;    for I := 1 to length(s2) do    begin      if StrToIntDef(IntToStr(DigitToInt(s2[I], 36)), 0) > StrToIntDef(IntToStr(DigitToInt(s3[i], 36)), 0) then      begin        OkIn2 := true;        BREAK;      end;      if StrToIntDef(IntToStr(DigitToInt(s2[I], 36)), 0) < StrToIntDef(IntToStr(DigitToInt(s3[i], 36)), 0) then      begin        OkIn2 := FALSE;        BREAK;      end;    end;    if (okin = true) and (OKIN2 = TRUE) then    begin      Result := 3;    end    else    begin      Result := 4;    end;  end;end;//返回连接状态TADOConnection;function DBconnect(connString: string): TADOConnection;var  conn: TADOConnection;begin  conn:=TADOConnection.Create(nil);  conn.ConnectionString:=connString;  conn.Connected:=true;  if conn.Connected=false then  begin    showmessage('服务器连接失败');  end  else  begin    result:=conn;  end;  conn.Free;end;{$R *.res}exports  DBconnect,ComParString;beginend.


不知道能不能直接点出来?

[解决办法]
function DBconnect就是连接数据库,先得调用这个。
[解决办法]
在DLL中连接数据库
记得要添加

Delphi(Pascal) code
uses ActiveX;initialization  CoInitialize(nil);//初始化COMfinalization  CoUnInitialize;
[解决办法]
不知道能不能直接点出来?

什么叫直接点出来?不是很明白你想问啥。如果只是调用dll的话,有静态调用和动态调用。给你举个静态调用的例子,你的dll输出了两个函数,在主界面的implementation上面写好函数原型:

function DBconnect(connString: string): TADOConnection; external 'dll文件名.dll';

然后在你需要的地方直接用这个函数就行了~动态调用也不麻烦,你google下delphi 调用dll就行了嘛~
[解决办法]
调用方式有两种:
1)动态调用 在使用时再调用
2)静态调用 EXE文件一运行就调用,直到EXE关闭之前再释放掉DLL
[解决办法]
静态调用
procedure OCRBarCodes; external 'C:\WINDOWS\system32\AspriseVB.dll';

动态调用
type
//动态DLL的函数 正则表达式取数据
pRegExGetStr = function (const WebStr, RegExStr: PChar; var ResultStr:PChar): Boolean; stdcall;

然后才能
//------- 调用DLL 用正则取网页数据
function TForm_Main.RegExGetData(Str, RegEx: string): string;
var
hDLL: THandle;
GetStr: pRegExGetStr;
pWeb: PChar;
begin
Result := '';
try
hDLL := Loadlibrary(PChar('regex.dll'));
if hDLL < 32 then Exit;
try
pWeb := StrAlloc(1024*1024);
GetStr := GetProcAddress(hDLL, 'RegExGetStrData');
if @GetStr = nil then Exit;
if not GetStr(PChar(Str), PChar(RegEx), pWeb) then Exit;
if pWeb = '' then
Result := ''
else
Result := pWeb;
finally
FreeLibrary(hDLL);
end;
StrDispose(pWeb);
except
Result := '';
end;
end;

[解决办法]
探讨
调用方式有两种:
1)动态调用 在使用时再调用
2)静态调用 EXE文件一运行就调用,直到EXE关闭之前再释放掉DLL

[解决办法]
Delphi(Pascal) code
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;type  TForm1 = class(TForm)    btnLoad: TButton;    procedure btnLoadClick(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;  TComParString= function(s1, s2, s3: string): integer; stdcall;var  Form1: TForm1; implementation{$R *.dfm}procedure TForm1.btnLoadClick(Sender: TObject);var  ProcAddr: FarProc;  DllHandle: THandle;  comParString: TComParString;begin  //  i := Self.MDIChildCount ;  DllHandle := LoadLibrary('AAA.dll');  ProcAddr := GetProcAddress(DllHandle, ComParString);  if ProcAddr <> nil then  begin    comParString:= ProcAddr;    comParString('A', 'B' , 'C') ;  end;end;
[解决办法]
在你的dll工程中 ,run -->Parameters的Host Application中选中上面的那个exe ,就可以调试啦。
[解决办法]
加载dll有两种方式:隐式加载、显示加载
隐式加载 既用external 关键字,既在程序启动时dll已加载
显示加载 调用api函数
10楼正解

读书人网 >.NET

热点推荐