Windows Mobile 获得 MAC,IP,IMEI,IMSI

关键字: windows mobile 获得 mac,ip,imei,imsi

How to get the MAC address of your network adapter.

24 05 2008


MAC address stands for Media Access Control address which is 6 bytes( 48 bits ) long . MAC address is the unique address which is used to identify network interface hardware. So how to get the MAC address of your network adapter?


You can use the function - GetAdaptersInfo() . The network adapter information will be populated and filled back in IP_ADAPTER_INFO structure. In that structure, you can get the network adapter name, MAC address and a couple of other information. See the sample code snippet.

#include "Iphlpapi.h"

...

// Get the buffer length required for IP_ADAPTER_INFO.

ULONG BufferLength = 0;

BYTE* pBuffer = 0;

if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo( 0, &BufferLength ))

{

    // Now the BufferLength contain the required buffer length.

    // Allocate necessary buffer.

    pBuffer = new BYTE[ BufferLength ];

}

else

{

    // Error occurred. handle it accordingly.

}



// Get the Adapter Information.

PIP_ADAPTER_INFO pAdapterInfo =

      reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);

GetAdaptersInfo( pAdapterInfo, &BufferLength );



// Iterate the network adapters and print their MAC address.

while( pAdapterInfo )

{

    // Assuming pAdapterInfo->AddressLength is 6.

    CString csMacAddress;

    csMacAddress.Format(_T("%02x:%02x:%02x:%02x:%02x:%02x"),

                        pAdapterInfo->Address[0],

                        pAdapterInfo->Address[1],

                        pAdapterInfo->Address[2],

                        pAdapterInfo->Address[3],

                        pAdapterInfo->Address[4],

                        pAdapterInfo->Address[5]);



    cout << "Adapter Name :" << pAdapterInfo->AdapterName << " "

         << "MAC :" << (LPCTSTR) csMacAddress << endl;



    // Get next adapter info.

    pAdapterInfo = pAdapterInfo->Next;

}



// deallocate the buffer.

delete[] pBuffer;



using System;

using System.Collections;

using System.Runtime.InteropServices;

namespace AdapterInfoTest

{

/// <summary>

/// Summary description for AdapterInfo.

/// </summary>

public sealed class AdapterInfo

{

const int MAX_ADAPTER_NAME_LENGTH = 256;

const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;

const int MAX_ADAPTER_ADDRESS_LENGTH = 8;

const int ERROR_BUFFER_OVERFLOW = 111;

const int ERROR_SUCCESS = 0;

[ DllImport ( "iphlpapi.dll" , CharSet= CharSet .Ansi)]

private static extern int GetAdaptersInfo( IntPtr pAdapterInfo, ref Int64 pBufOutLen);

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADDRESS_STRING

{

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=16)]

public string Address;

}

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADDR_STRING

{

public IntPtr Next;

public IP_ADDRESS_STRING IpAddress;

public IP_ADDRESS_STRING Mask;

public Int32 Context;

}

 

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADAPTER_INFO

{

public IntPtr Next;

public Int32 ComboIndex;

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=MAX_ADAPTER_NAME_LENGTH + 4)]

public string AdapterName;

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]

public string AdapterDescription;

public UInt32 AddressLength;

[ MarshalAs ( UnmanagedType .ByValArray, SizeConst=MAX_ADAPTER_ADDRESS_LENGTH)]

public byte [] Address;

public Int32 Index;

public UInt32 Type;

public UInt32 DhcpEnabled;

public IntPtr CurrentIpAddress;

public IP_ADDR_STRING IpAddressList;

public IP_ADDR_STRING GatewayList;

public IP_ADDR_STRING DhcpServer;

public bool HaveWins;

public IP_ADDR_STRING PrimaryWinsServer;

public IP_ADDR_STRING SecondaryWinsServer;

public Int32 LeaseObtained;

public Int32 LeaseExpires;

}

public AdapterInfo()

{

}

public static Adapter [] GetAdaptersInfo()

{

Adapter [] adaptersList;

ArrayList adapters = new ArrayList ();

long structSize = Marshal .SizeOf( typeof ( IP_ADAPTER_INFO ) );

IntPtr pArray = Marshal .AllocHGlobal(( int ) structSize );

int ret = GetAdaptersInfo(pArray, ref structSize );

if (ret == ERROR_BUFFER_OVERFLOW ) // ERROR_BUFFER_OVERFLOW == 111

{

// Buffer was too small, reallocate the correct size for the buffer.

pArray = Marshal .ReAllocHGlobal( pArray, new IntPtr (structSize) );

ret = GetAdaptersInfo( pArray, ref structSize );

} // if

if ( ret == 0 )

{

// Call Succeeded

IntPtr pEntry = pArray;

do

{

// Retrieve the adapter info from the memory address

IP_ADAPTER_INFO entry = ( IP_ADAPTER_INFO ) Marshal .PtrToStructure( pEntry, typeof ( IP_ADAPTER_INFO ));

Adapter adapter = new Adapter ();

adapter.index = entry.Index.ToString();

adapter.name = entry.AdapterName;

adapter.description = entry.AdapterDescription;

adapter.ip = entry.IpAddressList.IpAddress.Address;

// MAC Address (data is in a byte[])

string tmpString = string .Empty;

for ( int i = 0; i < entry.AddressLength ; i++)

{

tmpString += string .Format( "{0:X2}" , entry.Address );

}

adapter.macAddress = tmpString;

adapters.Add(adapter);

// Get next adapter (if any)

pEntry = entry.Next;

}

while ( pEntry != IntPtr .Zero );

Marshal .FreeHGlobal(pArray);

adaptersList = new Adapter [adapters.Count];

adapters.CopyTo(adaptersList);

}

else

{

adaptersList = new Adapter [0];

Marshal .FreeHGlobal(pArray);

throw new InvalidOperationException ( "GetAdaptersInfo failed with " + ret );

}

return adaptersList;

}

}

public class Adapter

{

public string index;

public string name;

public string description;

public string ip;

public string macAddress;

}

}

 

How to get mac address using win32 programming in windows?

get_Adapter getMacAddress(void)
{
get_Adapter getMac;
char getAdap[100];
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;

pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}

if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
wsprintf(getAdap , "MacAddress: %02X:%02X:%02X:%02X:%02X:%02X",
pAdapterInfo->Address[0],pAdapterInfo->Address[1],pAdapterInfo->Address[2],
pAdapterInfo->Address[3],pAdapterInfo->Address[4],pAdapterInfo->Address[5]);
getMac.AdapterAddress = getAdap;

wsprintf(getAdap,"Adp Name:%s", pAdapter->AdapterName);
getMac.AdapterName = getAdap;

wsprintf(getAdap,"Adapter Desc: %s", pAdapter->Description);
getMac.AdapterDesc = getAdap;

wsprintf(getAdap,"IP Address: %s", pAdapter->IpAddressList.IpAddress.String);
getMac.IpAddress = getAdap;

wsprintf(getAdap,"IP Mask: %s", pAdapter->IpAddressList.IpMask.String);
getMac.IpMask = getAdap;

_Adapter_Set._Adapter.push_back(getMac);
pAdapter = pAdapter->Next;
}
}
else {
MessageBox(NULL,"Call to GetAdaptersInfo failed","Error",0);
}
return getMac;
}

 


Don’t forget to add Iphlpapi.lib to project settings.

你可能感兴趣的:(Windows Mobile)