Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Ip Address from GetAdaptersAddresses
Message
 
À
21/06/2010 04:13:37
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01469839
Message ID:
01469842
Vues:
58
>I'm using GetAdaptersAddresses() to get information on all network adapters on a machine, thanks to FoxPro WinAPI Online Reference.
>
>The example shows the MAC Address, name etc being broken out from the _IP_ADAPTER_ADDRESSES Structure, but I also want to also break out the IP address for each adaptor. I must admit I find reading of structures rather complex, and although I've given it a go I'm not getting very far.
>
>
>*!*    typedef struct _IP_ADAPTER_ADDRESSES {
>*!*      union {
>*!*        ULONGLONG Alignment; 0:8
>*!*        struct {
>*!*              ULONG Length;
>*!*              DWORD IfIndex;
>*!*         };
>*!*      };
>*!*      struct _IP_ADAPTER_ADDRESSES* Next;
>*!*      PCHAR AdapterName;
>*!*      PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress;
>*!*      PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress;
>*!*      PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress;
>*!*      PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress;
>*!*      PWCHAR DnsSuffix;
>*!*      PWCHAR Description;
>*!*      PWCHAR FriendlyName;
>*!*      BYTE PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH];
>*!*      DWORD PhysicalAddressLength;
>*!*      DWORD Flags;
>*!*      DWORD Mtu;
>*!*      DWORD IfType;
>*!*      IF_OPER_STATUS OperStatus;
>*!*      DWORD Ipv6IfIndex;
>*!*      DWORD ZoneIndices[16];
>*!*      PIP_ADAPTER_PREFIX FirstPrefix;
>*!*    } IP_ADAPTER_ADDRESSES, *PIP_ADAPTER_ADDRESSES;
>
>
>From what I can work out bytes 8-11 ,12-15,16-19 & 20-23 point to IP Address Structures, am I right? And how to I read those structures?
>
>I know there are other ways of getting "my IP Address", but since I am using GetAdaptersAddresses to get MAC Address, I would like to get IP Address at the same time.
>
>Gary.
lcComputer = "."
loWMIService = GetObject("winmgmts:\\" + lcComputer + "\root\cimv2")
IPConfigSet = loWMIService.ExecQuery ;
("Select IPAddress from Win32_NetworkAdapterConfiguration")
For Each IPConfig in IPConfigSet
If !IsNull(IPConfig.IPAddress)
  ?"IP Address is:"+IPConfig.IPAddress(0)
  ? "Mac address is :"+IpToMacAddress(IPConfig.IPAddress(0))
endif
next


FUNCTION IpToMacAddress(lcIP)
        DECLARE INTEGER inet_addr IN ws2_32.dll STRING cIP
        DECLARE INTEGER SendARP IN iphlpapi.dll;
                INTEGER destIP, INTEGER sourceIP,;
                STRING @ pMacAddr, INTEGER @ PhyAddrLen
        LOCAL lnHr, lnIpAddr, lcMacAddr, lnLen
        lnIpAddr = inet_addr(lcIp)
        lcMacAddr = REPLICATE(CHR(0),6)
        lnLen = 6
        lnHr = SendARP(lnIpAddr,0,@lcMacAddr,@lnLen)
        RETURN BinaryToMac(lcMacAddr,lnLen)
ENDFUNC

FUNCTION BinaryToMac(lcMacAddr, lnLen)
        LOCAL lcMac, xj
        lcMac = ""
        FOR xj = 1 TO lnLen - 1
                lcMac = lcMac + RIGHT(TRANSFORM(ASC(;
                        SUBSTR(lcMacAddr,xj,1)),"@0"),2) + ":"
        ENDFOR
        lcMac = lcMac + RIGHT(TRANSFORM(ASC(;
                SUBSTR(lcMacAddr,lnLen,1)),"@0"),2)
        RETURN lcMac
ENDFUNC
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform