線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5067
推到 Plurk!
推到 Facebook!

Finding MAC address

 
conundrum
尊榮會員


發表:893
回覆:1272
積分:643
註冊:2004-01-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-10 13:14:37 IP:61.64.xxx.xxx 未訂閱
http://beta.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20707733.html     
 
 Solution Title: Finding MAC address
Author: jacobuecker 
Points: 200   Grade: A
Date: 08/12/2003 03:01PM PDT
 Is this what you are
looking for?
If not, try our Search      I need to retrieve my MAC address without using Netbios
 (because if the computer isn't plugged into the network it doesn't work). 
 I've seen that the guid contains the MAC address but 
that doesn't seem to be true in XP.  Thanks. 
     Comment from ged325 
Date: 08/12/2003 05:02PM PDT
 Comment          char ipconfig[] = "ipconfig > ipconfig.txt";    system(ipconfig);    then open the text file and parse it for the mac address.    be sure to close the stream and delete the file when you're done.     
Comment from ged325 
Date: 08/12/2003 05:03PM PDT
 Comment      (you're parsing for the physical address)
 
Comment from ged325 
Date: 08/12/2003 05:05PM PDT
 Comment      char ipconfig[] = "ipconfig > ipconfig.txt";    should be:    char ipconfig[] = "ipconfig /all > ipconfig.txt";
 
Comment from jkr 
Date: 08/12/2003 06:13PM PDT
 Comment      Try  'GetAdaptersAddresses()' 
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getadaptersaddresses.asp) 
 
Accepted Answer from skypalae 
Date: 08/12/2003 10:28PM PDT
 Accepted Answer      http://www.codeguru.com/network/GetMAC.html
 
Comment from ice911 
Date: 08/12/2003 11:43PM PDT
 Comment          download this library and header from here - www23.brinkster.com/monip/iphlpapi.zip     ///////////////////////////////////////////////////////////////////////////////////////////////
#include 
#include 
#include         PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
    DWORD AdapterInfoSize;
       // Allocate memory from sizing information
    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
    {
        printf("Memory allocation error\n");
        return;
    }        // Get actual adapter information
    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
    {
        printf("GetAdaptersInfo failed with error %d\n", Err);
        return;
    }        pAdapt = pAdapterInfo;
            printf("\tPhysical Address. . . . . . : ");
        for (UINT i=0; iAddressLength; i  )
        {
            if (i == (pAdapt->AddressLength - 1))
                printf("%.2X\n",(int)pAdapt->Address[i]);
            else
                printf("%.2X-",(int)pAdapt->Address[i]);
        }        
/////////////////////////////////////////////////////////////////////////////////////////////    --ice911
(hope this is what you need)     
Comment from tegila 
Date: 08/13/2003 07:09AM PDT
 Comment      #include 
#include 
#include 
#include 
#include     typedef struct _ASTAT_
{
    ADAPTER_STATUS adapt;
    NAME_BUFFER    NameBuff [30];
}ASTAT, * PASTAT;    ASTAT Adapter;    void main (void)
{
    NCB ncb;
    UCHAR uRetCode;
    char NetName[50];        memset( &ncb, 0, sizeof(ncb) );
    ncb.ncb_command = NCBRESET;
    ncb.ncb_lana_num = 0;        uRetCode = Netbios( &ncb );
    printf( "The NCBRESET return code is: 0x%x \n", uRetCode );        memset( &ncb, 0, sizeof(ncb) );
    ncb.ncb_command = NCBASTAT;
    ncb.ncb_lana_num = 0;        strcpy( ncb.ncb_callname,  "*               " );
    ncb.ncb_buffer = (char *) &Adapter;
    ncb.ncb_length = sizeof(Adapter);        uRetCode = Netbios( &ncb );
    printf( "The NCBASTAT return code is: 0x%x \n", uRetCode );
    if ( uRetCode == 0 )
    {
        printf( "The Ethernet Number is: xxxxxx\n",
                Adapter.adapt.adapter_address[0],
                Adapter.adapt.adapter_address[1],
                Adapter.adapt.adapter_address[2],
                Adapter.adapt.adapter_address[3],
                Adapter.adapt.adapter_address[4],
                Adapter.adapt.adapter_address[5] );
    }
}
 
Comment from tegila 
Date: 08/13/2003 07:11AM PDT
 Comment      Getting the MAC Address for an Ethernet Adapter    You can use the Netbios function to get the Media Access Control 
(MAC) address for an ethernet adapter if your card is bound to 
NetBIOS. The following example uses the NCBASTAT command, providing 
an asterisk (*) as the name in the ncb_callname member of the NCB structure.     Note  The following code does not work reliably on Windows 95, Windows 98, or Windows Me.         http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20834597.html    Solution Title: Look up DNS from Local host=>C  
Author: Barcaboy 
Points: 150   Grade: A
Date: 12/25/2003 10:37AM PST
 Is this what you are
looking for?
If not, try our Search      hy all    i need example code how to look up the local Domain Name Server. (primairy,secundairy,...)
this normaly depends from Isp to Isp.    i've been searching for it quite awhile, but never found example code    althought i know its possible, since there are many applications doing it...    it must have something to do with gethostbyname etc... but none of my code managed to pull it off    please if anyone knows how to do it, then help me :)    barcaboy    Comment from sirbounty 
Date: 12/25/2003 10:46AM PST
 Comment      Probably should put this up under Programming TA.
You can use IPCONFIG /ALL or WINIPCFG from most MS OSs (but I don't know if that helps you). 
Can you shell out to DOS and run IPCONFIG to get it?
 
Comment from Barcaboy 
Date: 12/25/2003 11:01AM PST
 Author Comment          yep with that command i see it    Dns-Servers...........    but however, i need C   code for it :(    @admin: could you please move this topic to programming, i didn't know this was for network only, without programming in it    sorry
 
Comment from sirbounty 
Date: 12/25/2003 11:04AM PST
 Comment      Post a 0 point question in the CS area. . .
 
Comment from bbao 
Date: 12/27/2003 08:47PM PST
 Comment      You may get them from array DNSServerSearchOrder by calling 
Win32_NetworkAdapterConfiguration class. The Win32_NetworkAdapterConfiguration WMI class represents the attributes 
and behaviors of a network adapter. This class has been extended
 to include extra properties and methods that support the management 
of the TCP/IP and Internetworking Packet Exchange (IPX) protocols 
(and are independent of the network adapter). This class will be 
deprecated and is not guaranteed to be supported after the 
ratification of the Distributed Management Task Force (DMTF) CIM 
network specification. The schema for classes such as Win32 network 
adapters, protocols, or bindings, will be updated to conform to the 
DMTF CIM network specification.    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_networkadapterconfiguration.asp
 
Comment from Barcaboy 
Date: 12/28/2003 03:19AM PST
 Author Comment          it requires Nt-like Os    like Windows Xp etc..    i need it fully compatible with all versions.
 
Administrative Comment from modulo 
Date: 12/28/2003 06:16AM PST
 Administrative Comment      Moved to C   topic area.    modulo    Community Support Moderator
Experts Exchange 
 
Accepted Answer from bbao 
Date: 12/28/2003 07:00AM PST
 Accepted Answer      if so, you may use GetNetworkParams function to retrieve network parameters (including DNS servers) 
for the local computer, it is compatible with 98/me/nt/2k/2k3.    http://msdn.microsoft.com/library/en-us/iphlp/iphlp/getnetworkparams.asp
 
Comment from Barcaboy 
Date: 12/28/2003 10:49AM PST
 Author Comment          thx    i'm having some trouble with yout GetNetworkParams.    i downloaded the iphlpapi because they aren't by default in Vc6.0 :s    but even with that it didn't compile    i keep getting the error: error C2061: syntax error : identifier 'PIP_ADAPTER_ADDRESSES'    searched google for a solution but none worked :s     
Comment from bbao 
Date: 12/28/2003 04:12PM PST
 Comment      hi, i didnt find iphlpapi lib on my computer too. where did you download the lib, from m$ site?    as for C2061 compiler error, it means the compiler found identifier where it was not expected. 
"This error can be caused by enclosing an initializer in parentheses. 
The error can be avoided by enclosing the declarator in parentheses 
or making it a typedef." [quoted from m$ vc   programmer's guide]    so please find the line that contains 'PIP_ADAPTER_ADDRESSES', either in .c or .h file, see if they are correct.
 
Comment from Barcaboy 
Date: 12/29/2003 09:25AM PST
 Author Comment          http://www.lastbit.com/trafmeter/iphlpapi.zip     i looked at it, this is the line (from iphlpapi.h)    #ifdef _WINSOCK2API_    //
// The following functions require Winsock2.
//    DWORD
WINAPI
GetAdaptersAddresses(
    ULONG Family,
    DWORD Flags,
    PVOID Reserved,
    PIP_ADAPTER_ADDRESSES pAdapterAddresses, 
    PULONG pOutBufLen
    );    #endif    nothing seems to be wrong, don't know how to fix it :s
 
Comment from bbao 
Date: 12/29/2003 09:31AM PST
 Comment      the key is who defines type PIP_ADAPTER_ADDRESSES? else the string is unknown to compiler. umm...
 maybe the .zip you downloaded is not full compatiblel with M$ SDK.
 
Comment from Barcaboy 
Date: 12/29/2003 09:37AM PST
 Author Comment          can't i download it from microsoft??    which SDK is needed for it?
 
Comment from bbao 
Date: 12/29/2003 08:41PM PST
 Comment      1. officially, order a Platform SDK CD at:
https://qmedia.e-storefront.com/showcontent.asp?contentname=PlatformSDKHome    2. i think you may post a comment at the following question to ask the experts there:
http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20770033.html    hope it helps,
bbao
 
Comment from Barcaboy 
Date: 12/30/2003 03:43AM PST
 Author Comment          i'm not really interestd in buying it thought :s:s
 
Comment from bbao 
Date: 12/30/2003 03:48AM PST
 Comment      hehe, ok no problem. have you posted a comment there to ask for the URL to download iphlpapi lib?
 
Comment from Barcaboy 
Date: 12/30/2003 04:16AM PST
 Author Comment          yeah    i'm waiting for a reply :)    i've seen there is a possibility to use WSAIoctl()    but the example code (from microsoft) only gave the subnetmask etc..    do you perhaps know how to change it so it finds the dns?.    ///
/*******************************************************************************      INTRFC.CPP
*
*     This program demonstrates how to programmatically enumerate IP interface
*     information such as a system's IP Address, Subnet mask, and broadcast
*     address through the WSAIoctl() API using the SIO_GET_INTERFACE_LIST 
*     option.  Additionally, this sample demonstrates how to interpret IP
*     status flags from each IP interface.  The flags are defined in the
*     Windows Sockets 2 Protocol-Specific Annex specification which can be
*     found in the January 98 MSDN Platform SDK.
*
*
*       Copyright 1996 - 1998 Microsoft Corporation.
*       All rights reserved.
*       This source code is only intended as a supplement to
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the
*       Microsoft samples programs.
\******************************************************************************/
#define _WIN32_WINNT 0x0400
#define WINVER 0x0400    #include 
#include 
#include     void main(void)
{
     WORD versionRequested;
     int wsError;
     WSADATA winsockData; 
     SOCKET s;
     DWORD bytesReturned;
     char* pAddrString;
     SOCKADDR_IN* pAddrInet;
     u_long SetFlags;
     INTERFACE_INFO localAddr[10];  // Assume there will be no more than 10 IP interfaces 
     int numLocalAddr;          versionRequested = MAKEWORD(2, 2);         wsError = WSAStartup(versionRequested, &winsockData); 
     if (wsError)
     { 
          fprintf (stderr, "Startup failed\n");
                return;
     }         if((s = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0)) == INVALID_SOCKET)
     {
          fprintf (stderr, "Socket creation failed\n");
          WSACleanup();
               return;
        }         // Enumerate all IP interfaces
     fprintf(stderr, "Scanning Interfaces . . .\n\n");
     wsError = WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0, &localAddr,
                      sizeof(localAddr), &bytesReturned, NULL, NULL);
     if (wsError == SOCKET_ERROR)
     {
          fprintf(stderr, "WSAIoctl fails with error %d\n", GetLastError());
          closesocket(s);
          WSACleanup();
          return;
     }         closesocket(s);         // Display interface information
     numLocalAddr = (bytesReturned/sizeof(INTERFACE_INFO));
     for (int i=0; isin_addr);
          if (pAddrString)
               printf("IP: %s  ", pAddrString);              pAddrInet = (SOCKADDR_IN*)&localAddr[i].iiNetmask;
          pAddrString = inet_ntoa(pAddrInet->sin_addr);
          if (pAddrString)
               printf(" SubnetMask: %s ", pAddrString);              pAddrInet = (SOCKADDR_IN*)&localAddr[i].iiBroadcastAddress;
          pAddrString = inet_ntoa(pAddrInet->sin_addr);
          if (pAddrString)
               printf(" Bcast Addr: %s\n", pAddrString);
          SetFlags = localAddr[i].iiFlags;
          if (SetFlags & IFF_UP)
               printf("This interface is up");
          if (SetFlags & IFF_BROADCAST)
               printf(", broadcasts are supported");
          if (SetFlags & IFF_MULTICAST)
               printf(", and so are multicasts");
          if (SetFlags & IFF_LOOPBACK)
               printf(". BTW, this is the loopback interface");
          if (SetFlags & IFF_POINTTOPOINT)
               printf(". BTW, this is a point-to-point link");
          printf("\n\n");
     }         WSACleanup();
}
///    link with ws2_32.lib
 
Comment from bbao 
Date: 12/30/2003 04:21AM PST
 Comment      has this code been test on your machine?
 
Comment from Barcaboy 
Date: 12/30/2003 04:23AM PST
 Author Comment          yep    it works, but gives the wrong info :s
 
Comment from Barcaboy 
Date: 12/30/2003 04:51AM PST
 Author Comment          found it!!!!    now it works, even without the SDK    this is what i did:    -instead of using the Api GetNetworkParams, i used LoadLibrary and GetProcAddress
to load the GetNetworkParams function from the iphlpapi.dll    then i converted the code to work with it, and modified the iphlpapi.h to not include the api GetNetworkParams.    so now it works perfect, althought it isn't the best way to do..     
Comment from bbao 
Date: 12/30/2003 05:02AM PST
 Comment      oh?! congratulations! thats a good idea to invoke iphlpapi.dll. :) i think we have found a right way, go ahead.
 
Comment from Barcaboy 
Date: 12/30/2003 05:04AM PST
 Author Comment          yeah, i'll give you the points :)
 
Comment from bbao 
Date: 12/30/2003 05:23AM PST
 Comment      thanks for your points and grade. btw, it looks you were right to post your C   programming
 question to NT networking TA, haha. :)) happy new year!     
Comment from bbao 
Date: 12/30/2003 05:31AM PST
 Comment      btw, it would be much better if you could post your source code at here to
 help more other people. and btw of btw, where are you? venetian? if you dont mind. :)
 
Comment from Barcaboy 
Date: 12/30/2003 09:27AM PST
 Author Comment          i'm dutch :)    source :
{
//
//       
   FIXED_INFO * FixedInfo;
   ULONG    ulOutBufLen;
   DWORD    dwRetVal;
   IP_ADDR_STRING * pIPAddr;
   //
   //
   typedef DWORD (WINAPI *GETNETWORKPARAMS) (PFIXED_INFO pFixedInfo,PULONG pOutBufLen);
     static HMODULE m_hIPHLAPI; 
     static GETNETWORKPARAMS m_GetNetworkParams; 
   //
     if(!m_hIPHLAPI) 
     { 
     m_hIPHLAPI = ::GetModuleHandle(TEXT("IPHLPAPI.DLL")); 
     if(!m_hIPHLAPI) 
     { 
     m_hIPHLAPI = ::LoadLibrary(TEXT("IPHLPAPI.DLL")); 
     } 
     if(m_hIPHLAPI) 
     { 
     m_GetNetworkParams = (GETNETWORKPARAMS)GetProcAddress(m_hIPHLAPI, "GetNetworkParams"); 
     } 
     } 
     FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
   ulOutBufLen = sizeof( FIXED_INFO );
     if(m_GetNetworkParams) 
     { 
     
     if( ERROR_BUFFER_OVERFLOW == m_GetNetworkParams( FixedInfo, &ulOutBufLen ) ) {
      GlobalFree( FixedInfo );
      FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, ulOutBufLen );
          }
     }
if ( dwRetVal = m_GetNetworkParams( FixedInfo, &ulOutBufLen ) ) {
        printf( "Call to GetNetworkParams failed. Return Value: x\n", dwRetVal );
     }
   else {
        char buff[260];
      sprintf(buff,"Dns Server:%s",FixedInfo->DnsServerList.IpAddress.String);
       MessageBox(0,buff,0,0);
      pIPAddr = FixedInfo -> DnsServerList.Next;
      while ( pIPAddr ) {
           sprintf(buff,"Dns Server:%s",pIPAddr->IpAddress.String);
           MessageBox(0,buff,0,0);
         pIPAddr = pIPAddr ->Next;
      }
   }
}
///    got that idea from some topic at codeguru.com
where they told that you could do it with loading the api, instead of needing to install sdk etc..    be sur to modify iphlpapi.h to exclude the function GetNetworkParams
 
Comment from bbao 
Date: 12/30/2003 09:33AM PST
 Comment      thanks for your comment, and nice to hear from you again from Holand... :)      
發表人 - conundrum 於 2004/07/10 13:27:15
系統時間:2024-05-09 17:09:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!