有一个汉字转拼音的函数,在正常的文件中调用,没有问题,可是我把他封装在DLL里,就出现问题(编译没错,执行就有问题),Function hz2py(hzstr: string):string; external 'hz2py.dll';
procedure TForm1.Button4Click(Sender: TObject);
begin
edit2.Text := hz2py(edit1.Text);
end;错误:Project Project1.exe raised exception class EInvalidPointer with message "Invalid pointer operation "这是怎么回事啊?!解决问题,一定给分!
procedure TForm1.Button4Click(Sender: TObject);
begin
edit2.Text := hz2py(edit1.Text);
end;错误:Project Project1.exe raised exception class EInvalidPointer with message "Invalid pointer operation "这是怎么回事啊?!解决问题,一定给分!
还有,dll中最好不要使用delphi的数据类型,例如String类型,string应该用pchar来代替
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. }
Var
ss : pchar;
Begin
ss := Inttostr (Int);
While LENGTH (ss) < 3 Do ss := '0' + ss;
result := ss
End;
Function hz2py (hzstr: pchar) : pchar;
Var
PYSTR,S,S1,PYSTR1: pchar;
n,j,i,STRLEN : integer;
Begin
S := DupeString (' ', 1000);
S1 := DupeString (' ', 809);
.......................
Function padl (Int: Integer) : pchar;
Var
ss : pchar;
Begin
ss := Inttostr (Int);
While LENGTH (ss) < 3 Do ss := '0' + ss;
result := ss
End;
改成这样
function padl(int :integer):PChar;
var
ss :PChar;
begin
ss :=PChar(IntToStr(int));
while Length(ss) < 3 do
ss :='0' + ss;
result :=ss;
end;
ss :='0' + ss;这句话也不对劲啊!!!
中加上Sharemem,并放在第一个
在调用工程中也要加,并放在第一个
是工程,不是unit,就可以用String类型
这样简单而且比较安全.(如果学过C++或者熟悉指针操作,你就会明白其中的道理),然后你的函数实现改成:
Function hz2py(pHzstr,pPystr: pchar):integer;
Var
hzstr,pystr: string
PYSTR,S,S1,PYSTR1: string;
n,j,i,STRLEN : integer;
Begin
hzstr := string(pHzstr);
//下面的部分,把result用pPystr替代,注意把string转成PChar
S := DupeString (' ', 1000);
S1 := DupeString (' ', 809);
.......................其他不在DLL输出的函数完全不必作任何修改.