#pragma comment(lib, "Netapi32.lib")unsigned int GetMACaddress(CString strAddress[], unsigned int max)
{
unsigned char MACData[8];
char addr[20]; // Allocate data structure for MAC (6 bytes needed)
unsigned int nCount = 0;
WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure for Netbios
DWORD dwEntriesRead;
DWORD dwTotalEntries;
BYTE *pbBuffer;
    
// Get MAC address via NetBios's enumerate function
NET_API_STATUS dwStatus = NetWkstaTransportEnum(
NULL, // [in]  server name
0, // [in]  data structure to return
&pbBuffer, // [out] pointer to buffer
MAX_PREFERRED_LENGTH, // [in]  maximum length
&dwEntriesRead, // [out] counter of elements actually enumerated
&dwTotalEntries, // [out] total number of elements that could be enumerated
NULL); // [in/out] resume handle
assert(dwStatus == NERR_Success); pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer for(DWORD i=1; i< dwEntriesRead && nCount <= max; i++) // first address is 00000000, skip it
{ // enumerate MACs and print
swscanf((wchar_t *)pwkti[i].wkti0_transport_address, L"%2hx%2hx%2hx%2hx%2hx%2hx", 
&MACData[0], &MACData[1], &MACData[2], &MACData[3], &MACData[4], &MACData[5]);

sprintf(addr,"%02X-%02X-%02X-%02X-%02X-%02X",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
if(!strcmp(addr,_T("00-00-00-00-00-00")))
continue; strAddress[nCount++] = addr;
}

// Release pbBuffer allocated by above function
dwStatus = NetApiBufferFree(pbBuffer);
assert(dwStatus == NERR_Success);
return nCount;
}=======================================================================
在2000 xp下都行,可98下就是不行,我把98的Netapi32.dll DEPENDS看了一下发现和2000下的不一样,它[98]下的,只有一个导出函数。
替换之后,又要我一个netxxx.dll 我又COPY过去,一运行,要nt??.dll我KAO,看来没完了。我把#pragma comment(lib, "Netapi32.lib")和函数注掉,能运行了!
各位同志,如何解之,还有啊,谁有好用的得网CARDMAC的函数?
当然要能在98下RUN

解决方案 »

  1.   

    How do I get a MAC in C or C++, Win32 environment?
    There are numerous ways to do this. As I was on my investigation, I realized that there was no clear and simple way to do this. A lot of people said to use NETSTAT and to parse the result. Some people said to create a UUID and pull the MAC from there. A couple of people said to use NetBIOS, and finally one said to query the NDIS miniport driver itself.Although the last solution sounded the coolest, apparently doing that from user mode wasn't the easiest thing to do. I'll give you a set of solutions from worst to best and then a quick discussion of the Miniport method.
    Method One: UuidCreate
    One quick way to find out your MAC address, which is very hacky and I wouldn't recommend would be to create a sequential Uuid. Apparently Microsoft uses your MAC address to help it create a universally unique identifier.All you have to do is check out bytes 2 through 8 and you are done. The code is below and the downloadable EXE and sample code is listed at the end of the article.
    // Fetches the MAC address and prints it
    static void GetMACaddress(void)
    {
      unsigned char MACData[6];  UUID uuid;
      UuidCreateSequential( &uuid );    // Ask OS to create UUID  for (int i=2; i<8; i++)  // Bytes 2 through 7 inclusive 
                               // are MAC address
        MACData[i - 2] = uuid.Data4[i];  PrintMACaddress(MACData);         // Print MAC address
    }This code will only work in Windows 2000/XP since Microsoft replaced UuidCreate in Windows 2000/XP with one that doesn't use the PC's MAC address. UuidCreate with your MAC address can easily be considered a security risk since you are distributing your ethernet card's address.
    Microsoft created UuidCreateSequential in Win2K and XP to do what the old UuidCreate did on Windows 95/98/Me. The current UuidCreate in Windows 2000/XP is not composed of a number which includes the MAC address of your primary NIC, hence they moved over that functionality to UuidCreateSequential. On the older OSes you may still use the UuidCreate function to obtain its MAC address.This example only supports one NIC card on your PC.
    Method Two: Use NetBIOS
    This solution is a lot more complicated then the final solution. It supports multiple NIC cards, but requires NetBIOS to be installed on the computer. It also requires you to have the cable connected to a valid NetBIOS network. It works great under all OSes including 95/98/Me/NT/2000/XP.
    // Fetches the MAC address and prints it
    static void GetMACaddress(void)
    {
      unsigned char MACData[8];      // Allocate data structure 
                                     // for MAC (6 bytes needed)  WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure 
                                     // for NetBIOS
      DWORD dwEntriesRead;
      DWORD dwTotalEntries;
      BYTE *pbBuffer;
        
      // Get MAC address via NetBIOS's enumerate function
      NET_API_STATUS dwStatus = NetWkstaTransportEnum(
       NULL,                 // [in]  server name
       0,                    // [in]  data structure to return
       &pbBuffer,            // [out] pointer to buffer
       MAX_PREFERRED_LENGTH, // [in]  maximum length
       &dwEntriesRead,       // [out] counter of elements 
                             //       actually enumerated
       &dwTotalEntries,      // [out] total number of elements 
                             //       that could be enumerated
       NULL);                // [in/out] resume handle
      assert(dwStatus == NERR_Success);  pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer  for(DWORD i=1; i< dwEntriesRead; i++)  // first address is
                                                // 00000000, skip it
      {                                         // enumerate MACs & print
        swscanf((wchar_t *)pwkti[i].wkti0_transport_address,
                L"%2hx%2hx%2hx%2hx%2hx%2hx",
                &MACData[0],
                &MACData[1],
                &MACData[2], 
                &MACData[3],
                &MACData[4],
                &MACData[5]);
        PrintMACaddress(MACData);
      }  // Release pbBuffer allocated by above function
      dwStatus = NetApiBufferFree(pbBuffer);
      assert(dwStatus == NERR_Success);
    }As you can tell converting the wide string which is returned from NetWkstaTransportEnum to an BYTE based array is a mess in itself. The great thing about this is that it goes through all the NICs located on your PC. You are also able to easily query other people's PCs by passing in a NetBIOS computer name as the first parameter to this function.Method Three: Use GetAdaptersInfo
    The cleanest way I could find to get all the MAC addresses located on a PC was to use the GetAdaptersInfo method. It includes almost as much information as IPCONFIG /ALL including your DHCP server, Gateway, IP address list, subnet mask and WINS servers. It also enumerates all the NICs on your PC and is supported in 95/98/Me/NT/2000/XP. Finally it also works if your NICs are not connected to valid networks (eg. wires are not even hooked up), but the NICs do have to be "enabled" in Windows.
    // Fetches the MAC address and prints it
    static void GetMACaddress(void)
    {
      IP_ADAPTER_INFO AdapterInfo[16];       // Allocate information 
                                             // for up to 16 NICs
      DWORD dwBufLen = sizeof(AdapterInfo);  // Save memory size of buffer  DWORD dwStatus = GetAdaptersInfo(      // Call GetAdapterInfo
        AdapterInfo,                 // [out] buffer to receive data
        &dwBufLen);                  // [in] size of receive data buffer
      assert(dwStatus == ERROR_SUCCESS);  // Verify return value is 
                                          // valid, no buffer overflow  PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
                                                   // current adapter info
      do {
        PrintMACaddress(pAdapterInfo->Address); // Print MAC address
        pAdapterInfo = pAdapterInfo->Next;    // Progress through 
                                              // linked list
      }
      while(pAdapterInfo);                    // Terminate if last adapter
    }I probably should mention that statically allocating an array for up to 16 NICs is not the best way to do this. It is a quick and dirty solution that should show you essentially how to get and enumerate all the MAC addresses on your PC.Method Four: The Miniport Driver
    I never implemented this method since it required some very low level coding. It also probably wouldn't be a good method since you are directly talking to the underlying NDIS miniport driver. The basic concept of this is to hit the miniport driver with an OIS query of OID_802_3_CURRENT_ADDRESS. This should return a buffer with the current MAC address.The way problem with this solution is that there is no easy way to do this from user mode, which is exactly the mode we are coding all our apps in, this is as opposed to kernal mode which drivers reside in.
    Conclusion
    Although there are no functions named GetMACaddress in the Win32 API, ATL, MFC, or C#, it is fairly easy to find and associate your MAC address with its related IP addresses by calling GetAdaptersInfo(). GetAdaptersInfo is located in the Platform SDK.I hope you had an interesting time reading this article.
      

  2.   

    To oldworm(oldworm):
    我的信箱是:
    [email protected]
    谢谢了。
      

  3.   

    不行啊,
    // GetMACAdapters.cpp : Defines the entry point for the console application.
    //
    // Author: Khalid Shaikh [[email protected]]
    // Date: April 5th, 2002
    //
    // This program fetches the MAC address of the localhost by fetching the 
    // information through GetAdapatersInfo.  It does not rely on the NETBIOS
    // protocol and the ethernet adapter need not be connect to a network.
    //
    // Supported in Windows NT/2000/XP
    // Supported in Windows 95/98/Me
    //
    // Supports multiple NIC cards on a PC.#include "stdafx.h"
    #include <Windows.h>
    #include <Iphlpapi.h>
    #include <Assert.h>
    #pragma comment(lib, "iphlpapi.lib")// Prints the MAC address stored in a 6 byte array to stdout
    static void PrintMACaddress(unsigned char MACData[])
    {
    printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n", 
    MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
    }// Fetches the MAC address and prints it
    static void GetMACaddress(void)
    {
    IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
    DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
    AdapterInfo, // [out] buffer to receive data
    &dwBufLen); // [in] size of receive data buffer
    assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
    do {
    PrintMACaddress(pAdapterInfo->Address); // Print MAC address
    pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
    }
    while(pAdapterInfo); // Terminate if last adapter
    }int _tmain(int argc, _TCHAR* argv[])
    {
    GetMACaddress(); // Obtain MAC address of adapters return 0;
    }在VC6中好像不能编译啊。
    我把DEV C++中的相关头文件COPY到VC的INCLUDE中,能编译,但运行出错。