Announcement

Collapse
No announcement yet.

IP Address Info

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Joe Byrne
    replied
    Al,

    PB has a download section for this very reason. Just zip everything together and email it to: [email protected]


    ------------------
    * Americans: Fight for Right. Join the push for the Fair Tax!

    [This message has been edited by Joe Byrne (edited December 19, 2006).]

    Leave a comment:


  • Al Orica
    replied
    Belated thanks for the code. It is MUCH appreciated.

    Is there a web site where I can upload PowerBASIC Code projects, other than sourceforge?

    Posting the code here is cool, but I'd like to post a complete project with icons, PBRs, etc. for others to use.

    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    Also thank to you Tonny,
    you are the one who where generous enough
    to share your code in the first place,
    this keep the weel spinning...

    Pierre

    Leave a comment:


  • Tonny Bjorn
    replied
    Thanks Pierre,

    When I slamed the code together I was on a strict deadline and
    never got around to update it with the corrections.

    Regards
    Tonny

    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    Mark,
    I added GetNetworkParams to the code above,
    this will give DNS info...

    Pierre

    Leave a comment:


  • Mark A Nelson
    replied
    Where should DNS be added?

    Leave a comment:


  • Pierre Bellisle
    replied
    If I may,
    there are many little things in Tonny's code,
    one is the absence of a valid buffer while calling GetAdaptersInfo,
    also the possibility to have a list of ip address returned is ignored...

    Here is a revision that should give more details and be more resistant to GPF's...
    GetNetworkParams api call was added for other network infos.

    Pierre

    Code:
    #COMPILE EXE '#Win 8.02#
    #DIM ALL
    #INCLUDE "WS2_32.INC"
    #INCLUDE "IPHLPAPI.INC"
     
    %Date_1970_01_01 = 116444736000000000
     
    '______________________________________________________________________________
     
    FUNCTION NetUserName AS STRING
     LOCAL UserName   AS ASCIIZ * %UNLEN + 1
     LOCAL BufferSize AS DWORD
     
     BufferSize = %UNLEN + 1
     WNetGetUser BYVAL %NULL, UserName, BufferSize
     FUNCTION = RTRIM$(UserName, $NUL)
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION ComputerName AS STRING
     LOCAL RetCode    AS LONG
     LOCAL Buffer     AS ASCIIZ * %MAX_COMPUTERNAME_LENGTH + 1
     LOCAL BufferSize AS DWORD
     
     BufferSize = %MAX_COMPUTERNAME_LENGTH + 1 
     GetComputerName Buffer, BufferSize
     FUNCTION = RTRIM$(Buffer, $NUL)
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION WindowsVersion AS STRING
     LOCAL OS AS OSVERSIONINFOEX
     
     OS.dwOSVersionInfoSize = SIZEOF(OS)
     IF GetVersionEx(OS) THEN
       SELECT CASE OS.dwPlatformId
         CASE %VER_PLATFORM_WIN32_WINDOWS    
           SELECT CASE OS.dwMinorVersion
             CASE  0 : FUNCTION = "95"
             CASE 10 : FUNCTION = "98"
             CASE 90 : FUNCTION = "Me"       
           END SELECT
         CASE %VER_PLATFORM_WIN32_NT    
           SELECT CASE OS.dwMajorVersion
             CASE 3 : FUNCTION = "NT 3.51"
             CASE 4 : FUNCTION = "NT 4.0"   '& $SPC & RTRIM$(OS.szCSDVersion, $NUL) & ", build" & STR$(OS.dwBuildNumber)
             CASE 5
               SELECT CASE OS.dwMinorVersion         
                 CASE 0 : FUNCTION = "2000" '& $SPC & RTRIM$(OS.szCSDVersion, $NUL) & ", build" & STR$(OS.dwBuildNumber)
                 CASE 1 : FUNCTION = "XP"   '& $SPC & RTRIM$(OS.szCSDVersion, $NUL) & ", build" & STR$(OS.dwBuildNumber)
                 CASE 2 : FUNCTION = "2003" '& $SPC & RTRIM$(OS.szCSDVersion, $NUL) & ", build" & STR$(OS.dwBuildNumber)
               END SELECT  
           END SELECT  
       END SELECT
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION ServicePackVersion AS STRING
     LOCAL OS AS OSVERSIONINFOEX
     
     OS.dwOSVersionInfoSize = SIZEOF(OS)
     GetVersionEx OS
     FUNCTION = RTRIM$(OS.szCSDVersion, $NUL)    
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION IsAdmin AS LONG
     LOCAL hAccessToken       AS DWORD
     LOCAL BufferSize         AS DWORD
     LOCAL Retval             AS LONG
     LOCAL Looper             AS LONG 
     LOCAL psidAdministrators AS LONG
     LOCAL os                 AS OSVERSIONINFOEX
     LOCAL pTokenGroups       AS TOKEN_GROUPS PTR
     LOCAL siaNtAuthority     AS SID_IDENTIFIER_AUTHORITY
     LOCAL Info               AS STRING
     
     os.dwOSVersionInfoSize = SIZEOF(os)
     GetVersionEx os
     
     IF os.dwPlatformId = %VER_PLATFORM_WIN32_NT THEN
       IF (OpenProcessToken(GetCurrentProcess, %TOKEN_QUERY, hAccessToken)) THEN
         GetTokenInformation hAccessToken, BYVAL %TOKENGROUPS, BYVAL 0&, BYVAL 0&, BufferSize
         Info = NUL$(BufferSize)
         Retval = GetTokenInformation(hAccessToken, BYVAL %TOKENGROUPS, BYVAL STRPTR(Info), LEN(Info), BufferSize)
         CloseHandle hAccessToken
         IF Retval THEN
           siaNtAuthority.Value(5) = 5 ' = SECURITY_NT_AUTHORITY
           IF (AllocateAndInitializeSid(siaNtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators)) THEN
             pTokenGroups = STRPTR(Info)
             FOR Looper = 0 TO @pTokenGroups.GroupCount - 1
               IF EqualSid(BYVAL psidAdministrators, BYVAL @pTokenGroups.Groups(Looper).pSid) THEN
                 FUNCTION = 1
                 EXIT FOR
               END IF
             NEXT
             FreeSid psidAdministrators
           END IF
         END IF
       END IF
     ELSE
       FUNCTION = 1 'Return true if Windows 95, 98 or Millenium
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION RegDateToInternationalDate(BYVAL SecondSince1970 AS DWORD)AS STRING
     'Seconds elapsed since 00:00:00, January 1, 1970
     LOCAL File_Time AS QUAD
     LOCAL Sys_Time  AS SystemTime
     
     File_Time = %Date_1970_01_01 + SecondSince1970 * 10000000 '10 millions  
     FileTimeToSystemTime BYVAL VARPTR(File_Time), Sys_Time
     
     FUNCTION = _
       FORMAT$(Sys_Time.wyear, "0000") & "-" & FORMAT$(Sys_Time.wMonth,  "00") & "-" & _
       FORMAT$(Sys_Time.wDay,    "00") & " " & FORMAT$(Sys_Time.wHour,   "00") & ":" & _
       FORMAT$(Sys_Time.wMinute, "00") & ":" & FORMAT$(Sys_Time.wSecond, "00") & " " & _
       "(" & FORMAT$(Sys_Time.wMilliSeconds, "000") & " milliseconds)" & _
       CHOOSE$(Sys_Time.wDayOfWeek + 1, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") 
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION AdaptersInfoGet()AS LONG
     LOCAL Looper            AS LONG
     LOCAL Buffer            AS STRING  
     LOCAL AdapterBuffer     AS STRING
     LOCAL AdapterBufferSize AS DWORD
     LOCAL AdapterPointer    AS IP_ADAPTER_INFO POINTER 
     LOCAL IpAddressPointer  AS IP_ADDR_STRING POINTER
     
     IF GetAdaptersInfo(BYVAL 0, AdapterBufferSize) = %ERROR_BUFFER_OVERFLOW THEN
       AdapterBuffer = NUL$(AdapterBufferSize)  
       AdapterPointer = STRPTR(AdapterBuffer)
       IF GetAdaptersInfo(BYVAL AdapterPointer, AdapterBufferSize) = %ERROR_SUCCESS THEN 
         DO 'For each adaptor
           Buffer = "Next adapter ptr:" & $TAB & HEX$(@AdapterPointer.pNext)               & $CRLF & _
                    "Combo index:"      & $TAB & "(Reserved)"                              & $CRLF & _
                    "Adapter name:"     & $TAB & RTRIM$(@AdapterPointer.AdapterName, $NUL) & $CRLF & _
                    "Description:"      & $TAB & RTRIM$(@AdapterPointer.Description, $NUL) & $CRLF & _
                    "MAC addr. length:" & $TAB & FORMAT$(@AdapterPointer.AddressLength)    & $CRLF & _
                    "MAC address:"      & $TAB
           FOR Looper = 0 TO @AdapterPointer.AddressLength - 1  'Build MAC address      
             Buffer = Buffer & HEX$(@AdapterPointer.Address(Looper), 2) & $SPC
           NEXT         
           Buffer = Buffer & $CRLF & _
                    "Adapter index:" & $TAB & HEX$(@AdapterPointer.Index) & $CRLF & _
                    "Adapter type:"  & $TAB 
           SELECT CASE @AdapterPointer.uType       
             CASE %MIB_IF_TYPE_OTHER     : Buffer = Buffer & "Other"
             CASE %MIB_IF_TYPE_ETHERNET  : Buffer = Buffer & "Ethernet"
             CASE %MIB_IF_TYPE_TOKENRING : Buffer = Buffer & "Tokenring"
             CASE %MIB_IF_TYPE_FDDI      : Buffer = Buffer & "FDDI"
             CASE %MIB_IF_TYPE_PPP       : Buffer = Buffer & "PPP"
             CASE %MIB_IF_TYPE_LOOPBACK  : Buffer = Buffer & "LoopBack" 
             CASE %MIB_IF_TYPE_SLIP      : Buffer = Buffer & "Slip"
           END SELECT       
           Buffer = Buffer & $CRLF & _              
                    "DHCP enabled:" & $TAB & IIF$(@AdapterPointer.DhcpEnabled, "Yes", "No")              & $CRLF & _  
                    "Current ip:"   & $TAB & "(Reserved " & HEX$(@AdapterPointer.CurrentIpAddress) & ")" & $CRLF & $CRLF
     
           IpAddressPointer = VARPTR(@AdapterPointer.IpAddressList.pNext) 
           Looper = 1
           DO 'List ip address, may be many
             Buffer = Buffer & $CRLF & _              
                      "IP"           & STR$(Looper) & ":" & $TAB & $TAB & RTRIM$(@IpAddressPointer.IpAddress.sString, $NUL) & $CRLF & _
                      "Mask"         & STR$(Looper) & ":" & $TAB & $TAB & RTRIM$(@IpAddressPointer.IpMask.sString, $NUL)    & $CRLF & _                
                      "NTE"          & STR$(Looper) & ":" & $TAB & $TAB & HEX$(@IpAddressPointer.Context)                   & $CRLF & _                                
                      "Next address" & STR$(Looper) & ":" & $TAB & HEX$(@IpAddressPointer.pNext)                            & $CRLF 
             IpAddressPointer = @IpAddressPointer.pNext
             INCR Looper
           LOOP WHILE IpAddressPointer       
     
           IpAddressPointer = VARPTR(@AdapterPointer.GatewayList.pNext) 
           Looper = 1
           DO 'List gateway info, may be many gateways  
             Buffer = Buffer & $CRLF & _              
                      "Gateway ip"   & STR$(Looper) & ":" & $TAB & RTRIM$(@IpAddressPointer.IpAddress.sString, $NUL) & $CRLF & _
                      "Gateway mask" & STR$(Looper) & ":" & $TAB & RTRIM$(@IpAddressPointer.IpMask.sString, $NUL)    & $CRLF & _
                      "Gateway NTE"  & STR$(Looper) & ":" & $TAB & HEX$(@IpAddressPointer.Context)                   & $CRLF & _
                      "Next address" & STR$(Looper) & ":" & $TAB & HEX$(@IpAddressPointer.pNext)                     & $CRLF
             IpAddressPointer = @IpAddressPointer.pNext
             INCR Looper
           LOOP WHILE IpAddressPointer 
     
           Buffer = Buffer & $CRLF & _              
                    "DHCP ip:"      & $TAB & $TAB & RTRIM$(@AdapterPointer.DhcpServer.IpAddress.sString, $NUL) & $CRLF & _
                    "DHCP mask:"    & $TAB & RTRIM$(@AdapterPointer.DhcpServer.IpMask.sString, $NUL)           & $CRLF & _
                    "DHCP NTE:"     & $TAB & HEX$(@AdapterPointer.DhcpServer.Context)                          & $CRLF & _
                    "Have WINS:"    & $TAB & IIF$(@AdapterPointer.HaveWins, "Yes", "No")                       & $CRLF & _
                    "Primary WINS:" & $TAB & RTRIM$(@AdapterPointer.PrimaryWinsServer.IpAddress.sString, $NUL) & $CRLF & $CRLF 
           IpAddressPointer = VARPTR(@AdapterPointer.SecondaryWinsServer.pNext) 
           Looper = 1
           DO 'List WINS server info, may be many servers
             Buffer = Buffer & $CRLF & _              
                      "Sec. WINS ip"   & STR$(Looper) & ":" & $TAB & RTRIM$(@IpAddressPointer.IpAddress.sString, $NUL) & $CRLF & _
                      "Sec. WINS mask" & STR$(Looper) & ":" & $TAB & RTRIM$(@IpAddressPointer.IpMask.sString, $NUL)    & $CRLF & _
                      "Sec. WINS NTE"  & STR$(Looper) & ":" & $TAB & HEX$(@IpAddressPointer.Context)                   & $CRLF & _
                      "Next address"   & STR$(Looper) & ":" & $TAB & HEX$(@IpAddressPointer.pNext)                     & $CRLF
             IpAddressPointer = @IpAddressPointer.pNext
             INCR Looper
           LOOP WHILE IpAddressPointer 
     
           Buffer = Buffer & $CRLF & _              
                    "Lease obtained:" & $TAB & RegDateToInternationalDate(@AdapterPointer.LeaseObtained) & $CRLF & _
                    "Lease expires:"  & $TAB & RegDateToInternationalDate(@AdapterPointer.LeaseExpires) 
     
           MessageBox %HWND_DESKTOP, ByCopy Buffer, ByCopy "GetAdaptersInfo", %MB_ICONINFORMATION OR %MB_OK
     
           AdapterPointer = @AdapterPointer.pNext
         LOOP WHILE AdapterPointer      
       END IF   
     
     END IF
     
    END FUNCTION
    '______________________________________________________________________________   
     
    FUNCTION NetworkParamsGet() AS LONG
     LOCAL pFixedInfo    AS FIXED_INFO POINTER
     LOCAL pIpAddr       AS IP_ADDR_STRING POINTER
     LOCAL InfoBufferLen AS DWORD
     LOCAL InfoBuffer    AS STRING
     LOCAL Buffer        AS STRING 
     LOCAL Retval        AS LONG 
     LOCAL Looper        AS LONG
     
     Retval = GetNetworkParams(BYVAL 0, InfoBufferLen)
     IF Retval = %ERROR_BUFFER_OVERFLOW THEN
       InfoBuffer = NUL$(InfoBufferLen) 'Buffer may be bigger than SIZEOF(FixedInfo)
       pFixedInfo = STRPTR(InfoBuffer)
       Retval = GetNetworkParams(BYVAL pFixedInfo, InfoBufferLen) 
     
       Buffer = "Host name: "   & $TAB & RTRIM$(@pFixedInfo.HostName, $NUL)   & $CRLF & _
                "Domain name: " & $TAB & RTRIM$(@pFixedInfo.DomainName, $NUL) & $CRLF 
     
       IF @pFixedInfo.CurrentDnsServer THEN
         Buffer = Buffer & "Current DNS: " & $TAB & @[email protected] & $CRLF 
       ELSE 'MS say that DNS should always come from .DnsServerList
         Buffer = Buffer & "Current DNS: " & $TAB & "(Reserved)" & $CRLF       
       END IF
     
       Looper = 1
       pIpAddr = VARPTR(@pFixedInfo.DnsServerList.pNext)
     
       DO WHILE pIpAddr
         Buffer = Buffer & "DNS number" & STR$(Looper) & ": " & $TAB & RTRIM$(@pIpAddr.IpAddress, $NUL) & $CRLF             
         pIpAddr = @pIpAddr.pNext
         INCR Looper
       LOOP  
     
       Buffer = Buffer & "Node type: " 
       SELECT CASE @pFixedInfo.NodeType
         CASE %BROADCAST_NODETYPE    : Buffer = Buffer & $TAB & "Broadcast"     '1
         CASE %PEER_TO_PEER_NODETYPE : Buffer = Buffer & $TAB & "Peer to peer"  '2
         CASE %MIXED_NODETYPE        : Buffer = Buffer & $TAB & "Mixed"         '4
         CASE %HYBRID_NODETYPE       : Buffer = Buffer & $TAB & "Hybrid"        '8
         CASE ELSE                   : Buffer = Buffer & $TAB & "Unknown (" & FORMAT$(@pFixedInfo.NodeType) & ")"
       END SELECT
       Buffer = Buffer & $CRLF
     
       Buffer = Buffer & _  
                "DHCP scope name: " & $TAB & RTRIM$(@pFixedInfo.ScopeId, $NUL)            & $CRLF & _      
                "DHCP node: "       & $TAB & IIF$(@pFixedInfo.EnableRouting, "Yes", "No") & $CRLF & _
                "Routing enabled: " & $TAB & IIF$(@pFixedInfo.EnableRouting, "Yes", "No") & $CRLF & _
                "Proxy enabled: "   & $TAB & IIF$(@pFixedInfo.EnableProxy,   "Yes", "No") & $CRLF & _   
                "DNS eEnabled: "    & $TAB & IIF$(@pFixedInfo.EnableDns,     "Yes", "No") 
     END IF
     
     MessageBox %HWND_DESKTOP, ByCopy Buffer, ByCopy "GetNetworkParams", %MB_ICONINFORMATION OR %MB_OK
     
    END FUNCTION
    '______________________________________________________________________________   
     
    FUNCTION PBMAIN()
     LOCAL Buffer AS STRING
     
     Buffer = "OS Version: "     & $TAB & "Windows " & WindowsVersion & $CRLF & _
              "OS Patch Level: " & $TAB & ServicePackVersion          & $CRLF & _
              "Computer Name: "  & $TAB & ComputerName                & $CRLF & _
              "User Name: "      & $TAB & NetUserName                 & $CRLF & _
              "Administrator: "  & $TAB & IIF$(IsAdmin, "Yes", "No")
     MessageBox %HWND_DESKTOP, ByCopy Buffer, ByCopy "Get info", %MB_ICONINFORMATION OR %MB_OK
     
     NetworkParamsGet
     
     AdaptersInfoGet
     
    END FUNCTION
    '______________________________________________________________________________
     
     'TYPE IP_Adapter_INFO
     '  pNext               AS IP_ADAPTER_INFO PTR
     '  ComboIndex          AS DWORD
     '  AdapterName         AS STRING * (%MAX_ADAPTER_NAME_LENGTH + 4)
     '  Description         AS STRING * (%MAX_ADAPTER_DESCRIPTION_LENGTH + 4)
     '  AddressLength       AS DWORD
     '  Address(0 TO %MAX_ADAPTER_ADDRESS_LENGTH - 1) AS BYTE '%MAX_ADAPTER_ADDRESS_LENGTH = 8
     '  Index               AS DWORD
     '  uType               AS DWORD
     '  DhcpEnabled         AS DWORD
     '  CurrentIpAddress    AS IP_ADDR_STRING PTR
     '  IpAddressList       AS IP_ADDR_STRING
     '  GatewayList         AS IP_ADDR_STRING
     '  DhcpServer          AS IP_ADDR_STRING
     '  HaveWins            AS LONG
     '  PrimaryWinsServer   AS IP_ADDR_STRING
     '  SecondaryWinsServer AS IP_ADDR_STRING
     '  LeaseObtained       AS LONG
     '  LeaseExpires        AS LONG
     'END TYPE
     
     'TYPE IP_ADDR_STRING
     '  pNext     AS IP_ADDR_STRING PTR
     '  IpAddress AS IP_ADDRESS_STRING
     '  IpMask    AS IP_MASK_STRING
     '  Context   AS DWORD
     'END TYPE
     
     'TYPE IP_ADDRESS_STRING
     '  sString AS STRING * 16
     'END TYPE
     
     'TYPE IP_MASK_STRING
     '  sString AS STRING * 16
     'END TYPE
     
     'TYPE FIXED_INFO
     ' HostName         AS STRING * (%MAX_HOSTNAME_LEN + 4)
     ' DomainName       AS STRING * (%MAX_DOMAIN_NAME_LEN + 4)
     ' CurrentDnsServer AS IP_ADDR_STRING PTR
     ' DnsServerList    AS IP_ADDR_STRING
     ' NodeType         AS DWORD
     ' ScopeId          AS STRING * (%MAX_SCOPE_ID_LEN + 4)
     ' EnableRouting    AS DWORD
     ' EnableProxy      AS DWORD
     ' EnableDns        AS DWORD
     'END TYPE
     
    '______________________________________________________________________________



    [This message has been edited by Pierre Bellisle (edited October 19, 2006).]

    Leave a comment:


  • John Schexnaydre
    replied
    And another:

    ' Tonny Bjorn
    ' Host Network Information
    Code:
    #DIM ALL
    #COMPILE EXE
    #INCLUDE "WS2_32.INC"
    #INCLUDE "IPHLPAPI.INC"
    
    %GET_HOST_ADAPTER_NAME      = 0
    %GET_HOST_IP_ADDRESS        = 1
    %GET_HOST_SUBNET_MASK       = 2
    %GET_HOST_DEFAULT_GATEWAY   = 3
    %GET_HOST_BROADCAST         = 4
    %GET_HOST_MAC_ADDRESS       = 5
    %GET_HOST_INTERFACE_COUNT   = 6
    
    ' ------------------------------
    FUNCTION NetUserName AS STRING
        LOCAL UserName AS ASCIIZ * 256
        LOCAL SIZE AS LONG
        SIZE = SIZEOF(UserName)
        WNetGetUser BYVAL %NULL, UserName, SIZE
        FUNCTION = TRIM$(UserName)
    END FUNCTION
    
    ' ------------------------------
    FUNCTION ComputerName AS STRING
        %MAX_COMPUTERNAME_LENGTH = 15
        DIM RetCode     AS LONG
        DIM lpBuffer    AS ASCIIZ * %MAX_COMPUTERNAME_LENGTH + 1
        DIM nSize       AS LONG
        nSize = %MAX_COMPUTERNAME_LENGTH + 1
        RetCode = GetComputerName(lpBuffer,nSize)
        FUNCTION = TRIM$(lpBuffer)
    END FUNCTION
    
    ' -----------------------------------------------
    FUNCTION WindowsVersion AS STRING
        LOCAL OS AS OSVERSIONINFO
        OS.dwOSVersionInfoSize = SIZEOF(OS)
        IF GetVersionEx(OS) THEN
           SELECT CASE OS.dwPlatformId
           CASE 1
                IF OS.dwMinorVersion = 0 THEN FUNCTION = "95"
                IF OS.dwMinorVersion = 10 THEN FUNCTION = "98"
           CASE 2
                IF OS.dwMajorVersion = 3 THEN FUNCTION = "NT 3.51"
                IF OS.dwMajorVersion = 4 THEN FUNCTION = "NT 4.0" '& "(" & FORMAT$(OS.dwBuildNumber) & "), " & TRIM$(OS.szCSDVersion)
                IF OS.dwMajorVersion = 5 AND OS.dwMinorVersion = 0 THEN FUNCTION = "2000" '& "(" & FORMAT$(OS.dwBuildNumber) & ")" ' & TRIM$(OS.szCSDVersion)
                IF OS.dwMajorVersion = 5 AND OS.dwMinorVersion = 1 THEN FUNCTION = "XP" '& "(" & FORMAT$(OS.dwBuildNumber) & ")" ' & TRIM$(OS.szCSDVersion)
                IF OS.dwMajorVersion = 5 AND OS.dwMinorVersion = 2 THEN FUNCTION = "2003" '& "(" & FORMAT$(OS.dwBuildNumber) & ")" ' & TRIM$(OS.szCSDVersion)
           CASE ELSE
                FUNCTION = "Unknown"
           END SELECT
        END IF
    END FUNCTION
    
    ' -----------------------------------------------
    FUNCTION ServicePackVersion AS STRING
      LOCAL OSVI AS OSVERSIONINFOEX
      LOCAL OSV AS OSVERSIONINFO
      OSVI.dwOSVersionInfoSize = SIZEOF(OSVI)
      IF ISFALSE GetVersionEx (BYVAL VARPTR (OSVI)) THEN
        OSVI.dwOSVersionInfoSize = SIZEOF (OSV)
        GetVersionEx BYVAL VARPTR(OSVI)
      END IF
      FUNCTION = OSVI.szCSDVersion
    END FUNCTION
    
    ' -----------------------------------------------
    FUNCTION IsAdmin AS BYTE 'LONG
        LOCAL os AS OSVERSIONINFO
        LOCAL hAccessToken AS LONG, i AS LONG
        LOCAL Info AS STRING
        LOCAL szInfo AS LONG
        LOCAL pTokenGroups AS TOKEN_GROUPS PTR
        LOCAL siaNtAuthority AS SID_IDENTIFIER_AUTHORITY
        LOCAL psidAdministrators AS LONG
    
        FUNCTION = 0
    
        os.dwOSVersionInfoSize = SIZEOF(os)
        GetVersionEx BYVAL VARPTR(os)
    
        IF ISFALSE(os.dwPlatformId = %VER_PLATFORM_WIN32_NT) THEN
            FUNCTION = -1
            EXIT FUNCTION
        END IF
    
        %TOKEN_QUERY = 8
        IF ISFALSE(OpenProcessToken(GetCurrentProcess, %TOKEN_QUERY, hAccessToken)) THEN
            FUNCTION = -1
            EXIT FUNCTION
        END IF
    
        GetTokenInformation hAccessToken, BYVAL %TOKENGROUPS, BYVAL 0&, BYVAL 0&, szInfo
        Info = SPACE$(szInfo)
        i = GetTokenInformation(hAccessToken, BYVAL %TOKENGROUPS, BYVAL STRPTR(Info), LEN(Info), szInfo)
        CloseHandle hAccessToken
    
        IF ISFALSE(i) THEN
           FUNCTION = -1
            EXIT FUNCTION
        END IF
    
        siaNtAuthority.Value(5) = 5 ' = SECURITY_NT_AUTHORITY
    
        IF ISFALSE(AllocateAndInitializeSid(siaNtAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators)) THEN
            FUNCTION = -1
            EXIT FUNCTION
        END IF
        pTokenGroups = STRPTR(Info)
        FOR i = 0 TO @pTokenGroups.GroupCount - 1
            IF EqualSid (BYVAL psidAdministrators, BYVAL @pTokenGroups.Groups(i).pSid) THEN
                FUNCTION = 1
                EXIT FOR
            END IF
        NEXT
    
        FreeSid BYVAL psidAdministrators
    
    END FUNCTION
    
    ' ------------------------------
    FUNCTION HostNetworkInfo(BYVAL HostInfo AS LONG) AS STRING
      LOCAL cbRequired          AS DWORD
      LOCAL Adapter             AS IP_ADAPTER_INFO
      LOCAL AdapterIP           AS IP_ADDR_STRING
      LOCAL sAdapterName        AS STRING
      LOCAL sIPAddr             AS STRING
      LOCAL sSubnetAddr         AS STRING
      LOCAL sGateway            AS STRING
      LOCAL ptr1                AS LONG
      LOCAL dwRemoteIP          AS DWORD
      LOCAL zRemoteIP           AS ASCIIZ * 16
      LOCAL dwMacAddrLen        AS DWORD
      LOCAL sMacByte            AS STRING
      LOCAL sRemoteMacAddress   AS STRING
      LOCAL lCounter            AS LONG
      DIM   MacArray(0 TO 1)    AS DWORD
      LOCAL WinsockData         AS WSADATA
      LOCAL sd                  AS DWORD
      LOCAL nBytesReturned      AS DWORD
      LOCAL i                   AS LONG
      LOCAL nNumInterfaces      AS LONG
      LOCAL sBuff               AS STRING * 760  ' allow max 10 interfaces
      LOCAL pszHostIP           AS ASCIIZ PTR
      LOCAL pszHostBroadcastIP  AS ASCIIZ PTR
      LOCAL sHostIP             AS STRING
      LOCAL x AS BYTE PTR
    
      %SIO_GET_INTERFACE_LIST = &H4004747F
      %INTERFACE_RECLEN       = 76
      %IP_Len                 = 4
    
      SELECT CASE HostInfo
    
        CASE 0 ' Adapter Name
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              FUNCTION = TRIM$(adapter.Description,$NUL)
            END IF
          END IF
    
        CASE 1 ' Host IP Address
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              FUNCTION = TRIM$(Adapter.Ipaddresslist.Ipaddress.sString,$NUL)
            END IF
          END IF
    
        CASE 2 ' Host Subnet Mask
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              FUNCTION = TRIM$(Adapter.Ipaddresslist.Ipmask.sString,$NUL)
            END IF
          END IF
    
        CASE 3 ' Host Default Gateway
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              FUNCTION = TRIM$(Adapter.Gatewaylist.Ipaddress.sString,$NUL)
            END IF
          END IF
    
        CASE 4 ' Broadcast Address
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              zRemoteIP = TRIM$(Adapter.Ipaddresslist.Ipaddress.sString,$NUL)
              dwRemoteIP = (inet_addr(zRemoteIP) OR &HFF000000)
              x = VARPTR(dwRemoteIP)
              FUNCTION = FORMAT$(@x) & "." & FORMAT$(@x[1]) & "." & FORMAT$(@x[2]) & "." & FORMAT$(@x[3])
            END IF
          END IF
    
        CASE 5 ' Host MAC Address
    
          GetAdaptersInfo(BYVAL 0, cbRequired)
          IF cbRequired > 0 THEN
            IF GetAdaptersInfo(Adapter, cbRequired) = %ERROR_SUCCESS THEN
              ptr1 = VARPTR(adapter.pNext)
              CopyMemory BYREF Adapter, BYVAL ptr1, LEN(Adapter)
              zRemoteIP = TRIM$(Adapter.Ipaddresslist.Ipaddress.sString,$NUL)
              dwRemoteIP = inet_addr(zRemoteIP)
              IF dwRemoteIP <> 0 THEN
                dwMacAddrLen = 6
                IF SendARP(dwRemoteIP, 0&, MacArray(0), dwMacAddrLen) = %NO_ERROR THEN
                  IF ISTRUE(MacArray(0) + MacArray(1)) AND dwMacAddrLen = 6 THEN
                    FOR lCounter = 0 TO 5
                      sMacByte = HEX$(PEEK(VARPTR(MacArray(0)) + lCOunter), 2)
                      sRemoteMacAddress = sRemoteMacAddress & sMacByte
                      IF lCounter < 5 THEN sRemoteMacAddress = sRemoteMacAddress & "-"
                    NEXT
                    FUNCTION = sRemoteMacAddress
                  END IF
                END IF
              END IF
            END IF
          END IF
    
        CASE 6 ' Interface Count
    
          IF WSAStartup( MAKDWD(2,2), WinsockData) <> 0 THEN EXIT FUNCTION
          sd = WSASocket(%AF_INET, %SOCK_DGRAM, 0, BYVAL 0, 0, 0)
          IF sd = %SOCKET_ERROR THEN EXIT FUNCTION
          WSAIoctl(BYVAL sd, %SIO_GET_INTERFACE_LIST, 0, 0, VARPTR(sBuff), LEN(sBuff), nBytesReturned, BYVAL 0, BYVAL 0 )
          CloseSocket sd
          WSACleanup
          FUNCTION = FORMAT$(INT( nBytesReturned / %INTERFACE_RECLEN ) - 1)
        CASE ELSE
    
          FUNCTION = ""
    
      END SELECT
    
    END FUNCTION
    
    
    FUNCTION PBMAIN()
      LOCAL OutStr AS STRING
    
      OutStr = OutStr & "OS Version: " & $TAB & "Windows " & WindowsVersion & $CRLF
      OutStr = OutStr & "OS Patch Level: " & $TAB & ServicePackVersion & $CRLF
      OutStr = OutStr & "Computer Name: " & $TAB & ComputerName & $CRLF
      OutStr = OutStr & "User Name: " & $TAB & NetUserName & $CRLF
      OutStr = OutStr & "Administrator: " & $TAB & IIF$(IsAdmin, "Yes", "No") & $CRLF
      OutStr = OutStr & "Active NIC Count: " & $TAB & HostNetworkInfo(%GET_HOST_INTERFACE_COUNT) & $CRLF
      OutStr = OutStr & "Adapter Name: " & $TAB & HostNetworkInfo(%GET_HOST_ADAPTER_NAME) & $CRLF
      OutStr = OutStr & "IP Address: " & $TAB & HostNetworkInfo(%GET_HOST_IP_ADDRESS) & $CRLF
      OutStr = OutStr & "Subnet Mask: " & $TAB & HostNetworkInfo(%GET_HOST_SUBNET_MASK) & $CRLF
      OutStr = OutStr & "Default Gateway: " & $TAB & HostNetworkInfo(%GET_HOST_DEFAULT_GATEWAY) & $CRLF
      OutStr = OutStr & "Broadcast Addr: " & $TAB & HostNetworkInfo(%GET_HOST_BROADCAST) & $CRLF
      OutStr = OutStr & "MAC Address: " & $TAB & HostNetworkInfo(%GET_HOST_MAC_ADDRESS) & $CRLF
    
      MSGBOX OutStr, %MB_OK, " Host Network Info"
    
    END FUNCTION
    ------------------

    Leave a comment:


  • John Schexnaydre
    replied
    Do a search in POFFS for iphlpapi.inc

    Here is one I found for the subnet mask:

    Code:
    #DIM ALL
    #COMPILE EXE
    #INCLUDE "iphlpapi.inc"
    
    FUNCTION PBMAIN() AS LONG
       LOCAL iCount   AS LONG
       LOCAL iSize    AS LONG
       LOCAL pIPADD   AS MIB_IPADDRTABLE PTR
       LOCAL sBuffer  AS STRING
       LOCAL pMask    AS BYTE PTR
       LOCAL pAddr    AS BYTE PTR
       LOCAL IpMask   AS DWORD
       LOCAL IpAddr   AS DWORD
       LOCAL sText    AS STRING
       'First, get the size result in Ret
       IF GetIpAddrTable (BYVAL 0, iSize, 0) = %ERROR_INSUFFICIENT_BUFFER THEN
          sBuffer = SPACE$(iSize)
          IF GetIpAddrTable (BYVAL STRPTR(sBuffer), iSize, 0) = %NO_ERROR THEN
             pIPADD = STRPTR(sBuffer)
             FOR iCount = 0 TO @pIPADD.dwNumEntries - 1
                IpAddr = @pIPADD.table(iCount).dwAddr
                IpMask = @pIPADD.table(iCount).dwMask
                pAddr = VARPTR(IpAddr)
                pMask = VARPTR(IpMask)
                MSGBOX "ADDRESS : " + USING$("#_.#_.#_.#", @pAddr[0], @pAddr[1], @pAddr[2], @pAddr[3]) + $CRLF _
                + "SUBNET  : " + USING$("#_.#_.#_.#", @pMask[0], @pMask[1], @pMask[2], @pMask[3]) ,,"TCP/IP Interface #" +STR$(iCount)
             NEXT
          ELSE
             MSGBOX "Error retrieving IP address table.",,"Error"
          END IF
       ELSE
          MSGBOX "Error getting the size needed to retrieve the IP address table.",,"Error"
       END IF
    END FUNCTION
    ------------------

    Leave a comment:


  • Al Orica
    started a topic IP Address Info

    IP Address Info

    I am using the following code to return the IP address:
    Code:
    FUNCTION IpAddress () AS STRING
      LOCAL pIP AS BYTE PTR
      HOST ADDR TO ip&
      pIP = VARPTR(ip&)
      FUNCTION = USING$("#_.#_.#_.#", @pIP, @pIP[1], @pIP[2], @pIP[3])
    END FUNCTION
    What I need is code to return the IP Mask, Gateway and DNS servers.

    I did a couple different searches and couldn't find anything. Any clues?

    ------------------
Working...
X