因为可以TTcpClient的LocalHostAddr和LocalHostName来获得的本地的机器名和IP,我想把这写在一个DLL内。如下unit NetFun;interfaceuses
  Sockets;
  
function GetLocalPCName : string; stdcall;
function GetLocalPCIP : string; stdcall;implementationfunction GetLocalPCName : string; stdcall;
var tmpV : TTcpClient;
begin
  tmpV := nil;
  try
    tmpV := TTcpClient.Create(Application);
    result := tmpV.LocalHostName;
  finally
    tmpV.Free;
  end;
end;function GetLocalPCIP : string; stdcall;
var tmpV : TTcpClient;
begin
  tmpV := nil;
  try
    tmpV := TTcpClient.Create(Application);
    result := tmpV.LocalHostAddr;
  finally
    tmpV.Free;
  end;
end;end.
我在外部的EXE调用这个函数的时候,可以有正确的返回值,但马上报“无效的指针操作”。我不知道这是为什么,请各位高手指教。一定给分。

解决方案 »

  1.   

    但奇怪的是,如果我在里面加了个SHOWMESSAGE(‘TEST’)之后,就再报错。不知是怎么回事,请高手指点。
      

  2.   

    写错了,是加了SHOWMESSAGE(‘TEST’)之后,就不再报错。就是下面的代码function GetLocalPCIP : string; stdcall;
    var tmpV : TTcpClient;
    begin
      tmpV := nil;
      try
        tmpV := TTcpClient.Create(Application);
        showmessage('test');
        result := tmpV.LocalHostAddr;
      finally
        tmpV.Free;
      end;
    end;
      

  3.   

    将create(Application) 改为create(nil);
    如果还有问题,将函数返回值改为pchar。
    不使用bpl时,string不能在参数中传递,能否作为返回值,我就不太记得了。
    试一试。
      

  4.   

    老兄你可真.....呵呵,我发现是STRING传参的问题刚准备过来把这栏给处理掉~~