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

    技术2026-06-18  6

     

    用来获取网卡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)