300分(一个关于DLL的调用,是动态调入函数的),请高手赐教!HELP ME PLEASE (300分)

H

hpretty

Unregistered / Unconfirmed
GUEST, unregistred user!
function GetHDDSerioNum: string;
var
proc: function: PChar; stdcall;
hMod: THandle;
begin
Result := '';
hMod := LoadLibrary('syswin60.DLL');
if (hMod = 0) then Exit;
try
@proc := GetProcAddress(hMod, 'GetNumber');
if (@proc <> nil) then
begin
Result := proc;
end;
finally
FreeLibrary(hMod);
end;
end;
这个方法调用没问题,
function GetHexString(const s :string): string;
var
proc: function(const s :pchar): PChar; stdcall;
hMod: THandle;
begin
Result := '';
hMod := LoadLibrary('syswin60.DLL');
if (hMod = 0) then Exit;
try
@proc := GetProcAddress(hMod, 'StringToHex');
if (@proc <> nil) then
begin
Result := proc(pchar(s));
//Form1.caption := proc(s);
end;
finally
FreeLibrary(hMod);
end;
end;
这个就不行了。
下面是这两个函数的原码!
function StringToHex(const s: pchar): Pchar;
var
hex: string;
i: Integer;
begin
hex := '';
for i := 1 to Length(s) do
begin
hex := hex + IntToHex(Ord(s), 2);
end;
Result := pchar(Hex);
end;

function GetNumber: PChar;
 
procedure StringToHex(const s: pchar; out: Pchar);
var
hex: string;
i: Integer;
begin
hex := '';
for i := 1 to Length(s) do
begin
hex := hex + IntToHex(Ord(s), 2);
end;
StrCopy(pchar(Hex), out);
end;

function GetHexString(const s :string): string;
var
proc: procedure(const s :pchar; aout: PChar); stdcall;
hMod: THandle;
out: char[0..255] of char;
begin
Result := '';
hMod := LoadLibrary('syswin60.DLL');
if (hMod = 0) then Exit;
try
@proc := GetProcAddress(hMod, 'StringToHex');
if (@proc <> nil) then
begin
proc(pchar(s), @out);
Result := out;
//Form1.caption := proc(s);
end;
finally
FreeLibrary(hMod);
end;
end;

 
还是一样的报错呀!
 
什么错?
为何要用dll,我有delphi代码
 
访问地址非法!因为我要将整个法算法加密,给别人只能给DLL不能给源码!
 
是不是这行出错?
proc(pchar(s), @out);
 
问题在StringToHex函数上,一个函数里的局部变量的作用域只在该函数中,当函数返回后就
无效了。
你有两种解决方法:
1、将hex定义为全总变量,用它作为字符串缓存(不太推荐);
2、用一个参数作为字符串缓存,此参数所指向的字符串的内存必须由调用者分配。

procedure StringToHex(const s: pchar; Buf: Pchar);
var
hex: string;
i: Integer;
begin
hex := '';
for i := 1 to Length(s) do
begin
hex := hex + IntToHex(Ord(s), 2);
end;
StrCopy(Buf, PChar(hex));
end;
第二种方法Xerxes也提到了,但他范了个小错,用错了StrCopy的参数位置。
对了,还有proc(pchar(s), @out);一句,应为:proc(PChar(s), Buf),建议不要用
out作为参数名,它是一个关键字。
 
是delphi的SRTING同Pchar的问题吗?
 
PChar只是字符串指针,在没有分配内存之前是不被赋值的,但可以指向某一字符串的地址。
在前面的PChar(s),实际上是临时分配了一个PChar字符串指针,指向String s的数据地址。

上面我给的程序还有错,按照下面的来改:
procedure StringToHex(const s: pchar; Buf: Pchar);
var
hex: string;
i: Integer;
begin
hex := '';
for i := 0 to Length(s)-1 do
begin
hex := hex + ' ' + IntToHex(Ord(s), 2);
end;
StrCopy(Buf, PChar(hex));
end;

procedure TForm1.Button1Click(Sender: TObject);
var Buf: array [0..255] of Char;
begin
StringtoHex('12345',Buf);
ShowMessage(Buf);
end;
 
静态调用是没有问题的了,但动态调用却不行,
你再帮我试一下动态调用,好吗?谢谢!!!
 
我又花时间试了两种方法,但也遇到了一些自己想不通的问题,有时间还要再揣摩一下,下
面是调通了的:

//DLL程序:
library MyFunc;

uses
SysUtils;

var
gBuf: string;

{$R *.res}
function StringToHex(const s: PChar): PChar;
var i: Integer;
begin
gBuf := '';
for i:=0 to Length(s)-1 do
begin
gBuf := gBuf + ' ' + IntToHex(Ord(s), 2);
end;
Result := PChar(gBuf);
end;

exports
StringToHex;

begin
end.

//调用程序:
unit Unit1;

interface

uses
Windows, Forms, StdCtrls, Controls, Classes;

type
TGetHexStr = function(const s: PChar): PChar; stdcall;
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
gBuf: array [0..255] of char;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
proc: TGetHexStr;
FP: TFarProc;
hd: THandle;
s: string;
begin
hd := LoadLibrary('MyFunc.DLL');
if (hd = 0) then Exit;
try
FP := GetProcAddress(hd, 'StringToHex');
if (FP <> nil) then
begin
proc := TGetHexStr(FP);
s := proc(PChar(Edit1.Text));
MessageBox(Handle, PChar(s), '', MB_OK);
end;
finally
FreeLibrary(hd);
end;
end;

end.
 
多谢!!! 300分 表示表示!
 
顶部