看看我的程序!!!
Uses WinSock;Function GetLocalIPAddress : String;
Var
   WSAData  : TWSAData;
   HostName : Array[ 1 .. 32 ] Of Char;
   HostInfo : PHostEnt;
   I        : Integer;
Begin
     Result := '';
     If( WSAStartUp( 2, WSAData ) = 0  ) Then
     Begin
          If( GetHostName( @HostName[ 1 ], 32 ) = 0 ) Then
          Begin
               HostInfo := GetHostByName( @HostName[ 1 ] );
               If( Hostinfo <> Nil ) Then
               Begin
                    For I := 1 To 4 Do
                    Begin
                         Result := Result + IntToStr( Ord( HostInfo.h_addr^[ I - 1 ] ) );
                         If( I < 4 ) Then Result := Result + '.';
                    End;
               End;
          End;
     End;
     WSACleanup;
End;

解决方案 »

  1.   

    看看我的程序!!!
    Uses WinSock;Function GetLocalIPAddress : String;
    Var
       WSAData  : TWSAData;
       HostName : Array[ 1 .. 32 ] Of Char;
       HostInfo : PHostEnt;
       I        : Integer;
    Begin
         Result := '';
         If( WSAStartUp( 2, WSAData ) = 0  ) Then
         Begin
              If( GetHostName( @HostName[ 1 ], 32 ) = 0 ) Then
              Begin
                   HostInfo := GetHostByName( @HostName[ 1 ] );
                   If( Hostinfo <> Nil ) Then
                   Begin
                        For I := 1 To 4 Do
                        Begin
                             Result := Result + IntToStr( Ord( HostInfo.h_addr^[ I - 1 ] ) );
                             If( I < 4 ) Then Result := Result + '.';
                        End;
                   End;
              End;
         End;
         WSACleanup;
    End;
      

  2.   

    看看我的程序!!!
    Uses WinSock;Function GetLocalIPAddress : String;
    Var
       WSAData  : TWSAData;
       HostName : Array[ 1 .. 32 ] Of Char;
       HostInfo : PHostEnt;
       I        : Integer;
    Begin
         Result := '';
         If( WSAStartUp( 2, WSAData ) = 0  ) Then
         Begin
              If( GetHostName( @HostName[ 1 ], 32 ) = 0 ) Then
              Begin
                   HostInfo := GetHostByName( @HostName[ 1 ] );
                   If( Hostinfo <> Nil ) Then
                   Begin
                        For I := 1 To 4 Do
                        Begin
                             Result := Result + IntToStr( Ord( HostInfo.h_addr^[ I - 1 ] ) );
                             If( I < 4 ) Then Result := Result + '.';
                        End;
                   End;
              End;
         End;
         WSACleanup;
    End;
      

  3.   

    楼主,我copy你的代码,很好使啊,没有任何问题。
    不过我use Winsock;
    msdn里说WSAStartup的第一个参数是:
    wVersionRequested 
    [in] Highest version of Windows Sockets support that the caller can use. The high-order byte specifies the minor version (revision) number; the low-order byte specifies the major version number.
      

  4.   

    wsasstartup用来初始化winsock;在windows下是必须的过程
    如果初始化成功,返回0;否则返回的是 ERROR CODESThe Windows Sockets WSAStartup function initiates use of the Windows Sockets DLL by a process.int WSAStartup (
        WORD wVersionRequested, 
        LPWSADATA lpWSAData 
       ); 
    Parameters
    wVersionRequested
    [in] The highest version of Windows Sockets support that the caller can use. The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number.
    lpWSAData
    [out] A pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation. 
    Res
    This function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and to retrieve details of the specific Windows Sockets implementation. The application or DLL may only issue further Windows Sockets functions after a successful WSAStartup invocation.
    In order to support future Windows Sockets implementations and applications which may have functionality differences from current version of Windows Sockets, a negotiation takes place in WSAStartup. The caller of WSAStartup and the Windows Sockets DLL indicate to each other the highest version that they can support, and each confirms that the other's highest version is acceptable. Upon entry to WSAStartup, the Windows Sockets DLL examines the version requested by the application. If this version is equal to or higher than the lowest version supported by the DLL, the call succeeds and the DLL returns in wHighVersion the highest version it supports and in wVersion the minimum of its high version and wVersionRequested. The Windows Sockets DLL then assumes that the application will use wVersion. If the wVersion field of the WSADATA structure is unacceptable to the caller, it should call WSACleanup and either search for another Windows Sockets DLL or fail to initialize.
    This negotiation allows both a Windows Sockets DLL and a Windows Sockets application to support a range of Windows Sockets versions. An application can successfully utilize a Windows Sockets DLL if there is any overlap in the version ranges. The following chart gives examples of how WSAStartup works in conjunction with different application and Windows Sockets DLL versions:
    App versions DLL Versions wVersion
    Requested wVersion wHigh
    Version End Result 
    1.1 1.1 1.1 1.1 1.1 use 1.1
    1.0 1.1 1.0 1.1 1.0 1.0 use 1.0 
    1.0 1.0 1.1 1.0 1.0 1.1 use 1.0
    1.1 1.0 1.1 1.1 1.1 1.1 use 1.1
    1.1 1.0 1.1 1.0 1.0 Application fails
    1.0 1.1 1.0 --- --- WSAVERNOT
    SUPPORTED
    1.0 1.1 1.0 1.1 1.1 1.1 1.1 use 1.1
    1.1 2.0 1.1 2.0 1.1 1.1 use 1.1
    2.0 2.0 2.0 2.0 2.0 use 2.0
    The following code fragment demonstrates how an application which supports only version 2 of Windows Sockets makes a WSAStartup call:
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;  
    wVersionRequested = MAKEWORD( 2, 0 ); 
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we couldn't find a usable */
        /* WinSock DLL.                                  */
        return; 
    /* Confirm that the WinSock DLL supports 2.0.*/
    /* Note that if the DLL supports versions greater    */
    /* than 2.0 in addition to 2.0, it will still return */
    /* 2.0 in wVersion since that is the version we      */
    /* requested.                                        */ 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 0 ) {
        /* Tell the user that we couldn't find a usable */
        /* WinSock DLL.                                  */
        WSACleanup( );
        return; 
    }