有一个汉字转拼音的函数,在正常的文件中调用,没有问题,可是我把他封装在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 "这是怎么回事啊?!解决问题,一定给分!

解决方案 »

  1.   

    你在dll中是怎么写的?最好把源代码写出来。
    还有,dll中最好不要使用delphi的数据类型,例如String类型,string应该用pchar来代替
      

  2.   

    把你 hz2py的方法的代码贴出来看看,也许问题在那个方法里面了。
      

  3.   

    在DLL中如用了string的变量要在DLL工程文件的uses里加上ShareMem!library mke;{ Important note about DLL memory management: ShareMem must be the
      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. }
      

  4.   

    crapex(编程浪子)说得对,应该就是string惹的祸
      

  5.   

    代码太多!~我uses了ShareMem也不管用啊!!!我用pchar代替string以后,好多系统函数都编译过不去了![Error] Unit1.pas(16): Incompatible types: 'String' and 'PAnsiChar'怎么弄?!另外,代码实在太长了,没法贴上来!
      

  6.   

    我给改成这样了:Function padl (Int: Integer) : pchar;
    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);
    .......................
      

  7.   

    什么函数编译不过去了,你给的第一个函数
    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;
      

  8.   

    while Length(ss) < 3 do
        ss :='0' + ss;这句话也不对劲啊!!!
      

  9.   

    很多系统函数不能用是因为pchar和string类型不同的原因,在dll中用pchar就要多注意
      

  10.   

    uses
    中加上Sharemem,并放在第一个
    在调用工程中也要加,并放在第一个
    是工程,不是unit,就可以用String类型
      

  11.   

    最好这样封装你的函数:Function hz2py(pHzstr,pPystr: pchar):integer;
    这样简单而且比较安全.(如果学过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输出的函数完全不必作任何修改.