CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Guest

    how to find MAC address

    Hello,
    Please tell me how can I find mac adress(physical address of network interface card) with out using any of net bios
    protocol or winipcfg or ipconfig commands.That is to get mac address
    using c or c++ language program.Is there any interrupt no for it
    Please send me any relevent material or any site .


  2. #2
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: how to find MAC address

    If you mean without using command-line utilities then you should refer to MSDN Article Q118623.

    It requires that netbios be bound to the adapter in question.

    Here's their source code:

    #include <windows.h>
    #include <wincon.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>

    typedef struct _ASTAT_
    {
    ADAPTER_STATUS adapt;
    NAME_BUFFER NameBuff [30];
    }ASTAT, * PASTAT;

    ASTAT Adapter;

    void main (void)
    {
    NCB Ncb;
    UCHAR uRetCode;
    char NetName[50];
    LANA_ENUM lenum;
    int i;

    memset( &Ncb, 0, sizeof(Ncb) );
    Ncb.ncb_command = NCBENUM;
    Ncb.ncb_buffer = (UCHAR *)&lenum;
    Ncb.ncb_length = sizeof(lenum);
    uRetCode = Netbios( &Ncb );
    printf( "The NCBENUM return code is: 0x%x \n", uRetCode );

    for(i=0; i < lenum.length ;i++)
    {
    memset( &Ncb, 0, sizeof(Ncb) );
    Ncb.ncb_command = NCBRESET;
    Ncb.ncb_lana_num = lenum.lana[i];

    uRetCode = Netbios( &Ncb );
    printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
    lenum.lana[i], uRetCode );

    memset( &Ncb, 0, sizeof (Ncb) );
    Ncb.ncb_command = NCBASTAT;
    Ncb.ncb_lana_num = lenum.lana[i];

    strcpy( Ncb.ncb_callname, "* " );
    Ncb.ncb_buffer = (char *) &Adapter;
    Ncb.ncb_length = sizeof(Adapter);

    uRetCode = Netbios( &Ncb );
    printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
    lenum.lana[i], uRetCode );
    if ( uRetCode == 0 )
    {
    printf( "The Ethernet Number on LANA %d is:
    %02x%02x%02x%02x%02x%02x\n",
    lenum.lana[i],
    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] );
    }
    }
    }




    Derek Viljoen
    [email protected]


  3. #3
    Join Date
    May 1999
    Posts
    60

    Re: how to find MAC address

    I think you are wel in network programming can you tell me that how to get mac frame comming on default gateway on windows nt network



  4. #4
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: how to find MAC address

    Actually, the only thing this proves is that I know how to search for the keywords "MAC address" on MSDN web site. Sorry, I don't know specifically how to do what you want.

    I do know that you need to put your network adapter in "permiscuous" mode, and be on the same subnet as the machine you are interested in tracing.

    Actually, Linux has a utility for this called tcpdump which will give you dumps of all packets seen by an adapter on its subnet. You might do a websearch for a windows port of that utility.

    Derek Viljoen
    [email protected]


  5. #5
    Guest

    Re: how to find MAC address

    derek, how stupit you are and non sense too.you don't know how to deal with an asian boy.I asked you a question .If you don't know the answer just say I don't know.


  6. #6
    Join Date
    Jun 1999
    Location
    Lumberton, NJ
    Posts
    70

    Re: how to find MAC address

    Sorry, but I don't understand how I insulted you. If you'd like to talk about this offline, please send me your email.


  7. #7
    Guest

    Re: how to find MAC address

    This method does not require the netbios protocol to be attached. It is a bit of a hack but after a fair amount of research it is the best I can find.

    This works based on the algorithm used to create GUIDs. Microsoft is using unpublished system APIs to retrieve the MAC id. This is used as the last 6 bytes of the GUIDs generated on any machine. So we call UuidCreate (rpcrt4.lib) to generate a guid and read off uuid.Data4[2] - [7].

    (Actually in my code I use CoCreateGuid (ole32.lib) which in turn calls UuidCreate so that users of my lib don't need to include libcrt4.lib)

    static CString getMACID()
    {
    // TODO: - find less hacky way of getting MAC

    // Ask RPC to create a UUID for us. If this machine has an Ethernet
    // adapter, the last six bytes of the UUID (bytes 2-7 inclusive in
    // the Data4 element) should be the MAC address of the local
    // Ethernet adapter.
    UUID uuid;
    UuidCreate(&uuid);

    CString macID;
    macID.Format("%02d:%02:%02d:%02d:%02d:%02d", int(uuid.Data4[2]), int(uuid.Data4[3]), int(uuid.Data4[4]),
    int(uuid.Data4[5]), int(uuid.Data4[6]), int(uuid.Data4[7]));


    return macID;
    }





  8. #8
    Guest

    Re: how to find MAC address

    I'd also used this approach, but there are 2 problems I'd hit:
    1) On some windows 95 machines there seems to be a bug which causes random values to be returned rather than the network card address. So need to check for them.
    2) When a network card is not installed, the OS provides a random "unique" value. There is a return code which on windows9x & NT which identifies this case.

    I use:
    if(UuidCreate(&guid) == RPC_S_OK // get network card ID number (if there is one)
    && (memcmp(guid.Data4 + 2, "DEST\0\0", 6)))

    But, on Windows 2000 Pro, the UuidCreate() does not seem to return the network card, but a random "unique" value. I've not had time to check into this more, anyone else hit this? Anyone know of any other ways of getting the MAC?

    Regards,
    Charly



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured