library Example;//动态链接库程序uses
SysUtils,
Classes;{返回字符在串中的位置}
function InStr(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
InStr;
beginend.
..............................................//执行程序单元unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
i:=InStr('abcdefg','d');//调用动态链接库函数,在这出错!
Edit1.Text := InttoStr(i);
end;
end.
...............................................................................//引用动态链接库单元unit Unit2;interface
function InStr(SourceStr:PChar;Ch:Char):Integer;stdcall;external 'Project1.dll';implementationend.
....................................................//主程序工程文件
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
end.
出现错误是:Project Project1.exe raised exception class EAccessVillation with message'Access violation at address 00376E9F in module 'Project1.dll'.Read of address 00000000' Process stopped.
SysUtils,
Classes;{返回字符在串中的位置}
function InStr(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
InStr;
beginend.
..............................................//执行程序单元unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
i:=InStr('abcdefg','d');//调用动态链接库函数,在这出错!
Edit1.Text := InttoStr(i);
end;
end.
...............................................................................//引用动态链接库单元unit Unit2;interface
function InStr(SourceStr:PChar;Ch:Char):Integer;stdcall;external 'Project1.dll';implementationend.
....................................................//主程序工程文件
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
end.
出现错误是:Project Project1.exe raised exception class EAccessVillation with message'Access violation at address 00376E9F in module 'Project1.dll'.Read of address 00000000' Process stopped.
var
i:integer;
begin
i:=InStr(Pchar('abcdefg'),'d');//你这样改试一下
Edit1.Text := InttoStr(i);
end;
end.
function InStr(SourceStr:PChar;Ch:Char):Integer;export;stdcall;//动态链接库函数unit2里面定义:
function InStr(SourceStr:PChar;Ch:Char):Integer;stdcall;external 'Example.dll';
谢谢二位。。
如果你不说明(stdcall)调用规范,默认为寄存器规范