MFC编程 得到网卡MAC地址(转载)

 

用来获取网卡MAC号,经检测挺管用的。

转载的:http://blog.csdn.net/HJBGraphics/archive/2005/08/28/466869.aspx

MFC编程 得到网卡MAC地址

 

用命令行config /all来获取网卡相关的信息,然后重定向到管道,就可以获取各种和网卡相关的信息了。只要能够在机器上执行“ipconfig /all”命令,就可以得到MAC地址,而这个命令在所有的网卡可用的情况下都是可用的。

 

文件GetMacByCmd.cpp

 

#include "stdafx.h"

 

bool GetMacByCmd( CString &strMac )

{

       const long MAX_COMMAND_SIZE = 10000;

       //获取MAC的命令行

       char szFetCmd[] = "ipconfig /all";

       //网卡MAC地址的前导信息

       const CString str4Search = "Physical Address. . . . . . . . . : ";

 

       strMac = "";

       BOOL bRet;

 

       SECURITY_ATTRIBUTES sa;

       HANDLE hReadPipe , hWritePipe;

 

       sa.nLength = sizeof( SECURITY_ATTRIBUTES );

       sa.lpSecurityDescriptor = NULL;

       sa.bInheritHandle = TRUE;

 

       //创建管道

       bRet = CreatePipe( &hReadPipe , &hWritePipe , &sa , 0 );

       if( !bRet )

       {

              return false;

       }   

 

       //控制命令行窗口信息

       STARTUPINFO si;

       //返回进程信息

       PROCESS_INFORMATION pi;

 

       si.cb = sizeof( STARTUPINFO );

       GetStartupInfo( &si );

       si.hStdError = hWritePipe;

       si.hStdOutput = hWritePipe;

       //隐藏命令行窗口

       si.wShowWindow = SW_HIDE;

       si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

     

       //创建获取命令行进程

       bRet = CreateProcess( NULL , szFetCmd , NULL , NULL , TRUE , 0 , NULL , NULL , &si , &pi );

       //放置命令行输出缓冲区

       char szBuffer[ MAX_COMMAND_SIZE + 1 ];

       CString strBuffer;

       if( bRet )

       {

              WaitForSingleObject( pi.hProcess , INFINITE );

              unsigned long count;

              CloseHandle( hWritePipe );

            

              memset( szBuffer , 0x00 , sizeof( szBuffer ) );

              bRet = ReadFile( hReadPipe , szBuffer , MAX_COMMAND_SIZE , &count , 0 );

            

              if( !bRet )

              {

                     //关闭所有的句柄

                     CloseHandle( hWritePipe );

                     CloseHandle( pi.hProcess );

                     CloseHandle( pi.hThread );

                     CloseHandle( hReadPipe );

                     return false;

              }

              else

              {

                     strBuffer = szBuffer;

                     long ipos;

                   

                     ipos = strBuffer.Find( str4Search );

                     strBuffer = strBuffer.Right( strBuffer.GetLength() - str4Search.GetLength() - ipos );

                     strBuffer = strBuffer.Left( 17 );

            

                     for( int i = 0 ; i < strBuffer.GetLength() ; i++ )

                     {

                            if( strBuffer.GetAt( i ) != '-' )

                            {

                                   strMac.AppendChar( strBuffer.GetAt( i ) );

                            }

                     }

 

                     return true;

              }

       }

       else

       {

              return false;

       }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值