首页 > 编程资源分享区 > C/C++源代码共享 > 一个简单的获取网卡mac地址的控制台程序
2006
07-08

一个简单的获取网卡mac地址的控制台程序

// ==========================================
// GetMAC.CPP – YangTze presents – 06/14/2001
// ==========================================

#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;
    LANA_ENUM lenum;
    int i;

    i=sizeof(Ncb);

    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_comm and = NCBRESET;
        Ncb.ncb_lana _num = lenum.lana;

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

        memset( &Ncb, 0, sizeof (Ncb) );
        Ncb.ncb_comm and = NCBASTAT;
        Ncb.ncb_lana _num = lenum.lana;

        strcpy( (char*)Ncb.ncb_callname,  ”*               ” );
        Ncb.ncb_buff er = (unsigned char *) &Adapter;
        Ncb.ncb_leng th = sizeof(Adapter);

        uRetCode = Netbios( &Ncb );
        printf( “The NCBASTAT on LANA %d return code is: 0x%x \n”,lenum.lana, uRetCode );
        if( uRetCode == 0 ) {
             printf(
                 ”The Ethernet Number on LANA %d is: %02X-%02X-%02X-%02X-%02X-%02X\n”,
                 lenum.lana ,
                 Adapter.adapt.adapter_ad dress[0],
                 Adapter.adapt.adapter_ad dress[1],
                 Adapter.adapt.adapter_ad dress[2],
                 Adapter.adapt.adapter_ad dress[3],
                 Adapter.adapt.adapter_ad dress[4],
                 Adapter.adapt.adapter_ad dress[5]
             );
        }
    }
}

编译方法:1、将此代码剪贴保存为getmacadd.c
          2、然后在命令行状态(dos提示符下)执行:cl getmacadd.c /link netapi32.lib。这里为了保证编译顺利执行,必须保证编译程序能找到cl.exe 和netapi32.lib。否则先执行vcvars32.bat(Visual C++ 6.0环境)或者vsvars32.bat(Visual C++ 7.0环境)。编译正常完成后,输出如下:

C:\>cl getmacadd.c /link netapi32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80×86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

getmacadd.c
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:getmacadd.exe
netapi32.lib
getmacadd.obj

执行getmacadd.exe即可。


留下一个回复