• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Need help to get MAC address

Started by Chris Chancellor, May 08, 2018, 07:23:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

I'm looking for some code to get the MAC address of the current pc. 

i partially translated some code from PB , but gotten stuck bcos of the headers so if you can help
and appreciate your help and below is my code

i need a new  O2 header file   iphlpapi.inc     ( i had used Jose's PB headers but encounter lots of  compiling problems )




Get_MAC.o2bas


' Get_MAC.o2bas
   $ filename "Get_MAC.exe"
       uses rtl32
       uses corewin

#lookahead

include "iphlpapi.inc"

DECLARE FUNCTION GetAdaptersInfo IMPORT "iphlpapi.dll" ALIAS "GetAdaptersInfo" ( _
   BYREF pAdapterInfo AS IP_ADAPTER_INFO _              ' __out   PIP_ADAPTER_INFO pAdapterInfo
, BYREF pOutBufLen AS DWORD _                          ' __inout PULONG pOutBufLen
) AS DWORD                                             ' DWORD


%ERROR_BUFFER_OVERFLOW =  0x6F
%ERROR_SUCCESS  = 0x0


typedef struct _IP_ADAPTER_ADDRESSES {
  union {
    ULONGLONG Alignment;
    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;
  ULONG64                            TransmitLinkSpeed;
  ULONG64                            ReceiveLinkSpeed;
  PIP_ADAPTER_WINS_SERVER_ADDRESS_LH FirstWinsServerAddress;
  PIP_ADAPTER_GATEWAY_ADDRESS_LH     FirstGatewayAddress;
  ULONG                              Ipv4Metric;
  ULONG                              Ipv6Metric;
  IF_LUID                            Luid;
  SOCKET_ADDRESS                     Dhcpv4Server;
  NET_IF_COMPARTMENT_ID              CompartmentId;
  NET_IF_NETWORK_GUID                NetworkGuid;
  NET_IF_CONNECTION_TYPE             ConnectionType;
  TUNNEL_TYPE                        TunnelType;
  SOCKET_ADDRESS                     Dhcpv6Server;
  BYTE                               Dhcpv6ClientDuid[MAX_DHCPV6_DUID_LENGTH];
  ULONG                              Dhcpv6ClientDuidLength;
  ULONG                              Dhcpv6Iaid;
  PIP_ADAPTER_DNS_SUFFIX             FirstDnsSuffix;
} IP_ADAPTER_ADDRESSES, *PIP_ADAPTER_ADDRESSES;




typedef struct _IP_ADAPTER_INFO {
  struct _IP_ADAPTER_INFO  *Next;
  DWORD                   ComboIndex;
  char                    AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
  char                    Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
  UINT                    AddressLength;
  BYTE                    Address[MAX_ADAPTER_ADDRESS_LENGTH];
  DWORD                   Index;
  UINT                    Type;
  UINT                    DhcpEnabled;
  PIP_ADDR_STRING         CurrentIpAddress;
  IP_ADDR_STRING          IpAddressList;
  IP_ADDR_STRING          GatewayList;
  IP_ADDR_STRING          DhcpServer;
  BOOL                    HaveWins;
  IP_ADDR_STRING          PrimaryWinsServer;
  IP_ADDR_STRING          SecondaryWinsServer;
  time_t                  LeaseObtained;
  time_t                  LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;





TYPE IP_ADAPTER_INF
  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
  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
           
'! GetAdaptersInfo lib "Iphlpapi.dll" alias "GetAdaptersInfo" (IP_ADAPTER_INFO pAdapterInfo , sys pOutBufLen ) as sys



'==================================
'   FUNCTION  MacAddressGet
'  this function gets the MAC address of the current machine
FUNCTION MacAddressGet(MacAddress() AS STRING) AS LONG
     LOCAL AdapterPointer    AS IP_ADAPTER_INF POINTER
     LOCAL AdapterBufferSize AS sys
     LOCAL AdapterBuffer     AS STRING
     LOCAL MacAddressCount   AS LONG
     LOCAL Looper            AS LONG
     LOCAL Temp              AS STRING

     'The GetAdaptersInfo function can retrieve information only for IPv4 addresses, see GetAdaptersAddresses
     IF GetAdaptersInfo(BYVAL 0, AdapterBufferSize) = %ERROR_BUFFER_OVERFLOW THEN
          AdapterBuffer = nuls AdapterBufferSize
          AdapterPointer = STRPTR(AdapterBuffer)
          IF GetAdaptersInfo(BYVAL AdapterPointer, AdapterBufferSize) = %ERROR_SUCCESS THEN
               DO 'For each network adaptor
                    INCR MacAddressCount
                    REDIM PRESERVE MacAddress(1 TO MacAddressCount)
'                    Temp = "Description: " + @AdapterPointer.Description
'                   + chr(13,10) + "AdapterName: " + @AdapterPointer.AdapterName
                    Temp = @AdapterPointer.Description
                 '   REPLACE CHR$(0) WITH "" IN Temp
                    MacAddress(MacAddressCount) = Temp$ +chr(13,10) + "MAC: "
                    FOR Looper = 0 TO @AdapterPointer.AddressLength - 1  'Build MAC address
                         MacAddress(MacAddressCount) = _
                         MacAddress(MacAddressCount) & HEX$(@AdapterPointer.Address(Looper), 2)
                    NEXT
                    MacAddress(MacAddressCount) = MacAddress(MacAddressCount) + chr(13,10)
                    AdapterPointer = @AdapterPointer.pNext
               LOOP WHILE AdapterPointer
          END IF
     END IF

     FUNCTION = MacAddressCount

END FUNCTION 





Charles Pegge

#1
Hi Chris,

This is out of my scope, do we have any IP experts here who could assist?

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366073(v=vs.85).aspx

Chris Chancellor

Thanxx Charles

the program Get_MAC.o2bas  is already done , what it needs now are the appropriate headers
for O2  namely the iphlpapi.inc   

Jose has headers for PB but not for  O2 ,  as in the attechment

Hopefully some one can translate it for O2 ?

Charles Pegge

Hi Chris,

Some concerns were raised regarding the nature of your company's business. Cryptography / LAN MACS etc. Can you clarify what your software is for, and what does your company do?