MFC编程 得到网卡MAC地址

王朝other·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

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;

}

}

文件GetMacByCmd.h

#pragma once

bool GetMacByCmd( CString &strMac );

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航