注意一下调用约定:stdcall;
function InStr1(SourceStr:PChar;Ch:Char):integer;export;
改为:
function InStr1(SourceStr:PChar;Ch:Char):integer;export;stdcall;TInstr = Function(Source:PChar;Check:Char):integer;
改为:
TInstr = Function(Source:PChar;Check:Char):integer;stdcall;
function InStr1(SourceStr:PChar;Ch:Char):integer;export;
改为:
function InStr1(SourceStr:PChar;Ch:Char):integer;export;stdcall;TInstr = Function(Source:PChar;Check:Char):integer;
改为:
TInstr = Function(Source:PChar;Check:Char):integer;stdcall;
我的MAIL常开
[email protected]
改为:
GetProcAddress(Moudule,‘MyInStr’); // MyInStr为动态连接库中的函数名
procedure TForm1.Button1Click(Sender: TObject);
type TCallMeDll = function(a,b: Integer): string;
var
CallMeDll: TCallMeDll;
FuncPtr: TFarProc;
hDll: THandle; result:
string;
begin
hDll:=LoadLibrary('Mytestdll.dll');
FuncPtr:=GetProcAddress(hDLL,'CallMe');
@CallMeDll:=FuncPtr;
if @CallMeDll <> nil then
result:=CallMeDll(4,5);
FuncPtr:=nil;
FreeLibrary(hDll);
end; 注意,首先要把DLL装载到内存。然后获得指向函数的指针,并将其指派给CallMeDLL。检查ProcAddress是否为Nil
第一、 你的DLL 做得有点问题
应该如下:
library DLLSelf1;
uses
ShareMem,SysUtils,Classes;{$R *.res}function InStr1(SourceStr:PChar;Ch:Char):integer;stdcall;
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;
begin
end.
第二. 静态调用相对简单一些
在 语句
implementation
{$R *.dfm} 之后加上 声明
function InStr1(SourceStr:PChar;Ch:Char):integer;stdcall
external 'dllself1.dll';
调用如下:
procedure TForm1.Button1Click(Sender: TObject);
var
n:integer;
p1:pchar;
c1:char;
begin
c1:='8';
try
getmem(p1,10*sizeof(char));
strpcopy(p1,'12345');
n:=InStr1(p1,c1);
if n=-1 then
ShowMessage('不匹配!')
else
ShowMessage('匹配!');
finally
freemem(p1);
end;
end;第三. 动态调用比较麻烦,但是效率高
下面我的这些代码在一台电脑上可以正常运行,但在另外一台电脑上总出错,我也搞不清.
type
TP1=Function(SourceStr:PChar;Ch:Char):integer;stdcall;
procedure TForm1.Button2Click(Sender: TObject);
var
n:integer;
p1:pchar;
c1:char;
mylib:THandle;
p:TP1;
begin mylib:=loadlibrary('dllself1.dll');
try //try1 begin if mylib<>0 then begin
@P:=GetProcAddress(mylib,'InStr1');
IF @P<>nil then
begin
try //try2 begin
getmem(p1,20*sizeof(char));
strpcopy(p1,'abcdefg');
c1:='g';
n:=p(p1,c1);
finally
freemem(p1);
end; //try2 end
end;
if n=-1 then
ShowMessage('不匹配!')
else
ShowMessage('匹配!');
end;
finally
freelibrary(mylib);
end; //try1 end
end;