天哪, 我第一次写Dll 怎么那么难写?书上看的. 网上找的 各式各样的都有 就是没一样我能成功的 谁能给我个标准的Dll的编写 和Dll的调用的格式?要保证可

  • 主题发起人 主题发起人 xhyph
  • 开始时间 开始时间
X

xhyph

Unregistered / Unconfirmed
GUEST, unregistred user!
天哪, 我第一次写Dll 怎么那么难写?书上看的. 网上找的 各式各样的都有 就是没一样我能成功的 谁能给我个标准的Dll的编写 和Dll的调用的格式?要保证可以用的(25分)<br />我照书上抄的,照网上抄的都出错
谁能给我个标准的Dll的编写 和Dll的调用的格式?要保证可以用的
我是菜鸟

 
//库部分
library off1;

uses
SysUtils,
Classes;

function GetRMBChar(Value : Char; Level : Integer) : String;
var
S : String;
begin
case Value of
'0': S := '零';
'1': S := '壹';
'2': S := '贰';
'3': S := '叁';
'4': S := '肆';
'5': S := '伍';
'6': S := '陆';
'7': S := '柒';
'8': S := '捌';
'9': S := '玖';
end;
case Level of
0: S := S + '分';
1: S := S + '角';
2: S := S + '元';
3: S := S + '拾';
4: S := S + '佰';
5: S := S + '仟';
6: S := S + '萬';
7: S := S + '拾';
8: S := S + '佰';
9: S := S + '仟';
end;
Result := S;
end;

function ChangeToRMB(Value : String) : String;
var
I : Integer;
S : String;
begin
S := '';
for I := Length(Value) downto 1 do
S := GetRMBChar(Value, Length(Value) - I) + S;
Result := S;
end;

//获得人民币串
function GetRMBStr(Value:currency):pchar;stdcall;
var
S1, s2 : String;
begin
S1 := FormatCurr('#.00', Value);
Delete(S1, Pos('.', S1), 1);
S2 := ChangeToRMB(S1);
S2 := S2 + '整'; //(RMB:' + S1 + ')
Result := pchar(S2);
end;

{$R *.res}
exports
GetRMBStr; //获得大写人民币串

begin
end.


//程序部分
unit main;

interface

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

type
TtestForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function GetRMBStr(Value:currency):pchar;stdcall;external 'off1.dll' name 'GetRMBStr';

var
testForm: TtestForm;

implementation

{$R *.dfm}

procedure TtestForm.Button1Click(Sender: TObject);
var
cdb:currency;
begin
cdb:=strtocurr(trim(edit1.Text));
label1.Caption:=string(GetRMBStr(cdb));
end;

end.
 
library ib_ftudf;

{ 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,
Classes;

{$R *.RES}


function LEN(s:ShortString):Integer;stdcall;
begin
Result:=Length(s);
END;

exports
LEN;

end.//DLL函数文件.

 
调用:
procedure len(s:shortstring);far;external 'ib_ftudf';
 
dll内容:(求阶乘)
uses
SysUtils,
Classes;

{$R *.RES}

function nn(n:integer):integer;stdcall;
var i:integer;
begin
result:=1;
for i:=1 to n do result:=result*i;
end;
exports
nn index 1 name 'nx';
begin
end.
主程序调用:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
type
tintfunction=function(n:integer):integer;stdcall;
procedure TForm1.Button1Click(Sender: TObject);
var
hinst:thandle;
fpointer:tfarproc;
myfunct:tintfunction;
begin
hinst:=loadlibrary('2.dll');//生成dll的名称
if hinst&gt;0 then
try
fpointer:=getprocaddress(hinst,'nx');
if fpointer&lt;&gt;nil then
begin
myfunct:=tintfunction(fpointer);
myfunct(strtoint(edit1.text));
edit2.text:=inttostr(myfunct(strtoint(edit1.text)));
showmessage(edit1.text+'的阶乘是:'+#13+edit2.text);
end
else
showmessage('dll not found');
finally
freelibrary(hinst);
end
else
showmessage('library not found');
end;
end.//以上程序只需加入需要的edit,button即可
 
真奇怪,,,为什么我第一次做的时候就是不对
我照抄你们的就对
我第一次其实也和你们一样的啊
 
后退
顶部