//---------------dll:library Project1;uses
SysUtils,
Classes;{返回字符ch在字符SourceStr中的位置}
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 Index 1 name 'MyinStr' resident;begin
end.//编绎生成DLL时提示:
[Warning] Project1.dpr(23): Symbol 'INDEX' is specific to a platform
[Warning] Project1.dpr(23): Symbol 'RESIDENT' is deprecated
-----------------------------------------------------------------------
但最终也生成了Project1.dll
用VB调用调试OK
Option Explicit
Private Declare Function Instr% Lib "example.dll" (ByVal SourceStr$, ByVal ch$)
Private Sub Command1_Click()
Text1.Text = Str(InStr("abc", "c"))
End Sub
-------------------------------------------------------
用下面的Delphi调用不行,为什么呀?
//-------------------------Delphi调用代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function InStr(SourceStr:PChar;ch:Char):integer;export;external 'project1.dll'
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
i:=instr('abc','c');
edit1.text:=inttostr(i);
end;end.
SysUtils,
Classes;{返回字符ch在字符SourceStr中的位置}
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 Index 1 name 'MyinStr' resident;begin
end.//编绎生成DLL时提示:
[Warning] Project1.dpr(23): Symbol 'INDEX' is specific to a platform
[Warning] Project1.dpr(23): Symbol 'RESIDENT' is deprecated
-----------------------------------------------------------------------
但最终也生成了Project1.dll
用VB调用调试OK
Option Explicit
Private Declare Function Instr% Lib "example.dll" (ByVal SourceStr$, ByVal ch$)
Private Sub Command1_Click()
Text1.Text = Str(InStr("abc", "c"))
End Sub
-------------------------------------------------------
用下面的Delphi调用不行,为什么呀?
//-------------------------Delphi调用代码:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function InStr(SourceStr:PChar;ch:Char):integer;export;external 'project1.dll'
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
i:=instr('abc','c');
edit1.text:=inttostr(i);
end;end.
Option Explicit
Private Declare Function Instr% Lib "Project1.dll" (ByVal SourceStr$, ByVal ch$)
Private Sub Command1_Click()
Text1.Text = Str(InStr("abc", "c"))
End Sub
才对
function MyinStr(SourceStr:PChar;ch:Char):integer;external 'project1.dll'
或者这样也可以
function InStr(SourceStr:PChar;ch:Char):integer;external 'project1.dll' name 'MyinStr'
执行Ok但发现一个奇怪的问题:
VB调用
Private Declare Function Instr% Lib "example.dll" (ByVal SourceStr$, ByVal ch$)
Private Sub Command1_Click()
Text1.Text = Str(InStr("abc", "c"))
End Sub
文本框的值是3而Delphi调用unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function InStr(SourceStr:PChar;ch:Char):integer;external 'project1.dll' name 'MyinStr'
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
i:=instr('abc','c');
edit1.text:=inttostr(i);
end;end.文本框的值是2
为什么一个是3,一个是2呢?
InStr在VB中是系统函数