一个关于动态连接库的问题

chenfeng3000 2002-07-29 07:31:44
我现在想领会DELPHI的DLL编程技术,经过看书自学,我初步知道DELPHI有如下
三种调用方式,一写好DLL程序后,由主程序静态调用DLL
二写好DLL程序后,由Loadlibrary() GetProcaddress() Freelibrary
这三个API完成,我的代码如下:
unit pro1;

interface

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

type

TInstr = Function(Source:PChar;Check:Char):integer; //为了强制函数类型转换的类型说明
TForm1 = class(TForm)
Edit1: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);

private
{ Private declarations }
public

{ Public declarations }
end;
var
Form1: TForm1;
// function InStr1(S:PChar;Check:Char):integer; far; external'D:\DLLself\DLLSelf1.dll';
implementation

{$R *.dfm}

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
order:integer;
txt:PChar;
PFunc:TFarProc; //
Moudule:THandle; //句柄
begin
Moudule:=loadlibrary('D:\DLLSelf\DLLSelf1.dll'); //动态连接库路径\文件正确阿!
showmessage(inttostr(Moudule)); //程序执行后 Moudule=13500416
if Moudule>32 then
begin
PFunc:=nil; //先赋空值
PFunc:=GetProcAddress(Moudule,'InStr1'); // InStr1 为动态连接库中的函数名
if PFunc=nil then //若找到函数的入口地址成功那么 PFun必<>nil
begin
showmessage('sss'); //可悲的是此行程序运行了,而且终止了后面真正调用程序的代码了
exit;
end;
txt:=StrAlloc(80); //这往后的代码由于入口地址不对,我永远也执行不了!!!!!
txt:=StrPCopy(txt,Edit1.Text);
order:=TInstr(PFunc)(txt,Key);
if order=-1 then
showmessage('successful')
else
showmessage('unsuccessful');
end;
Freelibrary(Moudule);

end;

end.
/******以上是我的调用动态连接库的程序************/

/**********下面是我编写的动态连接库程序**********/
library DLLSelf1;

{ 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
ShareMem,
SysUtils,
Classes;
{$R *.res}

function InStr1(SourceStr:PChar;Ch:Char):integer;export;
var
Len,i:integer;
begin
Len:=strlen(SourceStr);
for i:=0 to Len-1 do
if SourceStr[i]=ch then
begin
Result:=i;
Exit;
end;

Result:=-1;

end;
exports
InStr1 Index 1 name 'MyInStr' resident;
begin
end. //这几行代码想来各位大虾能够知道我的用意:就是想作一个普通的动态库函数被其他程序所用

可是就是不能正常运行. 程序出错为: external exception C30000D 还有,您不介意的话利用我的动态连接
库作一个静态连接调用,和动态连接调用让我大饱眼福,不胜感激阿!!!!!!!谢谢!!!!!!!
...全文
24 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhenhao 2002-07-29
你的程序是正确的?
回复
lanying 2002-07-29
好长呀
回复
发动态
发帖子
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
社区公告
暂无公告