函数向前引用引起的问题:将LocalIP Function放在FormCreate前面就可以了。

解决方案 »

  1.   

    1.在窗體Form1的類聲明中加入function LocalIP : string;再在過程體中加入TForm1.

    2.將function LocalIP : string;整個過程體放到
    procedure TForm1.FormCreate(Sender: TObject);與begin之間
    3.或如上面那位先生所說
      

  2.   

    改为如下:
    unit Unit2;interfaceuses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Winsock, StdCtrls, ExtCtrls, Menus ,clipbrd;
    type
      TForm1 = class(TForm)
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;function LocalIP : string;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
    begin
    Label1.Caption:='IP: '+LocalIP;
    //上面这句每次编译,结果总是如下的出错
    //[Error] Unit2.pas(27): Undeclared identifier: 'LocalIP'end;end.//IP函数
    function LocalIP : string;
    type
        TaPInAddr = array [0..10] of PInAddr;
        PaPInAddr = ^TaPInAddr;
    var
        phe  : PHostEnt;
        pptr : PaPInAddr;
        Buffer : array [0..63] of char;
        I    : Integer;
        GInitData      : TWSADATA;begin
        WSAStartup($101, GInitData);
        Result := '';
        GetHostName(Buffer, SizeOf(Buffer));
        phe :=GetHostByName(buffer);
        if phe = nil then Exit;
        pptr := PaPInAddr(Phe^.h_addr_list);
        I := 0;
        while pptr^[I] <> nil do begin
          result:=StrPas(inet_ntoa(pptr^[I]^));
          Inc(I);
        end;
        WSACleanup;
    end;