如果实在不行的话,那你就采用下面的方法吧:// File: GetMAC.cpp - Windows console application // // This program demonstrates how to find the hardware/MAC // address of all adapters on the computer. Example output: // // 3Com EtherLink PCI: 00-01-02-d4-de-79 // // This only works with Windows 98, Windows 2000/XP etc. For // Windows 95, CoCreateGuid is an easy way to get the MAC, if // you're not concerned about multiple adapters. // // Gert -> codeblast.com#include <windows.h> #include <stdio.h> #include <time.h> #include <stdlib.h>/////////////////////////////////////////////////////////////////////////////// // You can either link directly to Iphlpapi.lib: // //#include <Iphlpapi.h> //#pragma comment(lib, "Iphlpapi.lib") // // but instead we will dynamically load GetAdaptersInfo so // the code will at least compile and load on Windows 95... // So we have to copy some stuff declared in iptypes.h:#define MAX_ADAPTER_DESCRIPTION_LENGTH 128 // arb. #define MAX_ADAPTER_NAME_LENGTH 256 // arb. #define MAX_ADAPTER_ADDRESS_LENGTH 8 // arb.// // IP_ADDRESS_STRING - store an IP address as a dotted decimal string //typedef struct { char String[4 * 4]; } IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;// // IP_ADDR_STRING - store an IP address with its corresponding subnet mask, // both as dotted decimal strings //typedef struct _IP_ADDR_STRING { struct _IP_ADDR_STRING* Next; IP_ADDRESS_STRING IpAddress; IP_MASK_STRING IpMask; DWORD Context; } IP_ADDR_STRING, *PIP_ADDR_STRING;// // ADAPTER_INFO - per-adapter information. All IP addresses are stored as // strings //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;typedef DWORD (WINAPI* GetAdaptersInfoFunc)( PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data PULONG pOutBufLen // size of data returned ); ///////////////////////////////////////////////////////////////////////////////void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6) { int n; LPSTR p = lpsz; for (n = 0; n < nAddressLength; n++) { p += sprintf(p, n ? "-%02x" : "%02x", Address[n]); } }void main() { HMODULE hLib; GetAdaptersInfoFunc GetAdaptersInfo = NULL; PIP_ADAPTER_INFO pai = NULL; DWORD dwSize = 0; CHAR szMac[64];
hLib = LoadLibrary("Iphlpapi.dll"); if (!hLib) { puts("Failed to load Iphlpapi.dll"); exit(1); } GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo"); if (GetAdaptersInfo == NULL) { puts("Failed to load GetAdaptersInfo in Iphlpapi.dll"); } // Get size of buffer needed: GetAdaptersInfo(NULL, &dwSize); pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize); GetAdaptersInfo(pai, &dwSize); PIP_ADAPTER_INFO p = pai; while (p) { MacAddressToString(p->Address, szMac, p->AddressLength); printf("%s: %s\n", p->Description, szMac); p = p->Next; } GlobalFree(pai); FreeLibrary(hLib); }
//
// This program demonstrates how to find the hardware/MAC
// address of all adapters on the computer. Example output:
//
// 3Com EtherLink PCI: 00-01-02-d4-de-79
//
// This only works with Windows 98, Windows 2000/XP etc. For
// Windows 95, CoCreateGuid is an easy way to get the MAC, if
// you're not concerned about multiple adapters.
//
// Gert -> codeblast.com#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>///////////////////////////////////////////////////////////////////////////////
// You can either link directly to Iphlpapi.lib:
//
//#include <Iphlpapi.h>
//#pragma comment(lib, "Iphlpapi.lib")
//
// but instead we will dynamically load GetAdaptersInfo so
// the code will at least compile and load on Windows 95...
// So we have to copy some stuff declared in iptypes.h:#define MAX_ADAPTER_DESCRIPTION_LENGTH 128 // arb.
#define MAX_ADAPTER_NAME_LENGTH 256 // arb.
#define MAX_ADAPTER_ADDRESS_LENGTH 8 // arb.//
// IP_ADDRESS_STRING - store an IP address as a dotted decimal string
//typedef struct {
char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;//
// IP_ADDR_STRING - store an IP address with its corresponding subnet mask,
// both as dotted decimal strings
//typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING* Next;
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;//
// ADAPTER_INFO - per-adapter information. All IP addresses are stored as
// strings
//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;typedef DWORD (WINAPI* GetAdaptersInfoFunc)(
PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data
PULONG pOutBufLen // size of data returned
);
///////////////////////////////////////////////////////////////////////////////void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6)
{
int n;
LPSTR p = lpsz;
for (n = 0; n < nAddressLength; n++)
{
p += sprintf(p, n ? "-%02x" : "%02x", Address[n]);
}
}void main()
{
HMODULE hLib;
GetAdaptersInfoFunc GetAdaptersInfo = NULL; PIP_ADAPTER_INFO pai = NULL;
DWORD dwSize = 0;
CHAR szMac[64];
hLib = LoadLibrary("Iphlpapi.dll");
if (!hLib)
{
puts("Failed to load Iphlpapi.dll");
exit(1);
} GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo");
if (GetAdaptersInfo == NULL)
{
puts("Failed to load GetAdaptersInfo in Iphlpapi.dll");
} // Get size of buffer needed:
GetAdaptersInfo(NULL, &dwSize); pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize);
GetAdaptersInfo(pai, &dwSize); PIP_ADAPTER_INFO p = pai;
while (p)
{
MacAddressToString(p->Address, szMac, p->AddressLength);
printf("%s: %s\n", p->Description, szMac);
p = p->Next;
} GlobalFree(pai);
FreeLibrary(hLib);
}
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
方法肯定是可以的。
唯一需要注意的一点就是要安装platform sdk而且设置
C:\Program Files\Microsoft SDK\Include为第一路径。当然如果你没有安装platform sdk那么你就采用我说的第二种方法吧。
大概是原先的那个工程里改动些什么东西吧
谢谢 StdAfx(任杰)
留个联系方法给我好吗?
我的MSN: [email protected]