VC修改mac地址的方法

来源:互联网 发布:中国乐队 知乎 编辑:程序博客网 时间:2024/06/06 15:51

Windows10注册表修改网卡mac地址:

https://jingyan.baidu.com/article/84b4f56598958b60f6da32bd.html

xp 设置 修改网卡mack地址

https://jingyan.baidu.com/article/48a420579a90f1a924250484.html


vc++ 中如何读取和修改MAC的地址


读取mac地址方法:

CString  GetMacByCmd(){//command buf sizeconst long lBufSize = 4096;//command line stringchar szFetCmd[] = "ipconfig /all";//Search stringstring str4Search = "Physical Address. . . . . . . . . : ";CString strRet = _T("");BOOL bRet;SECURITY_ATTRIBUTES sa; HANDLE hReadPipe,hWritePipe;//Initsa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE;  //Create pipebRet = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);if(FALSE == bRet){return strRet;}//Get infomationSTARTUPINFO si;PROCESS_INFORMATION pi;si.cb = sizeof(STARTUPINFO); GetStartupInfo(&si);si.hStdError = hWritePipe; si.hStdOutput = hWritePipe; si.wShowWindow = SW_HIDE; //Hide command windowssi.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;//Create command processbRet = CreateProcess (NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL,   NULL, &si, &pi );char szBuffer[lBufSize + 1];string strBuffer;if (TRUE == bRet) { WaitForSingleObject (pi.hProcess, INFINITE); //Read infomationunsigned long count;memset(szBuffer, 0x00, sizeof(szBuffer));bRet  =  ReadFile(hReadPipe,  szBuffer,  lBufSize,  &count,  0);if(TRUE == bRet){strBuffer = szBuffer;long ipos;ipos = strBuffer.find(str4Search);//Get adress infomationstrBuffer = strBuffer.substr(ipos+str4Search.length());ipos = strBuffer.find("\n");strBuffer = strBuffer.substr(0, ipos);strRet.Format("%s", strBuffer.c_str()); //remove“00-50-EB-0F-27-82”'-' get 0050EB0F2782strRet.Remove('-');strRet = strRet.Left(strRet.GetLength() - 2 );}}//close handleCloseHandle(hWritePipe);CloseHandle(pi.hProcess); CloseHandle(pi.hThread); CloseHandle(hReadPipe);return strRet;}

VC获得正在使用的网卡的MAC地址PIP_ADAPTER_INFO pAdapterInfo;PIP_ADAPTER_INFO pAdapter = NULL;DWORD dwRetVal = 0;UINT i;/* variables used to print DHCP time info */struct tm newtime;char buffer[32];errno_t error;ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));if (pAdapterInfo == NULL) {printf("Error allocating memory needed to call GetAdaptersinfo\n");return 1;}// Make an initial call to GetAdaptersInfo to get// the necessary size into the ulOutBufLen variableif (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {FREE(pAdapterInfo);pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);if (pAdapterInfo == NULL) {printf("Error allocating memory needed to call GetAdaptersinfo\n");return 1;}}if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {pAdapter = pAdapterInfo;while (pAdapter) {if (pAdapter->DhcpEnabled) {/*printf("\t Lease Obtained: ");/* Display local timeerror = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained);if (error)printf("Invalid Argument to _localtime32_s\n");else {// Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime);if (error)printf("Invalid Argument to asctime_s\n");elseasctime_s returns the string terminated by \n\0printf("%s", buffer);}*/if( pAdapter->LeaseObtained > 10 ){for (i = 0; i < pAdapter->AddressLength; i++) {if (i == (pAdapter->AddressLength - 1))printf("%.2X\n", (int) pAdapter->Address[i]);elseprintf("%.2X-", (int) pAdapter->Address[i]);}}} pAdapter = pAdapter->Next;printf("\n");}} else {printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);}if (pAdapterInfo)FREE(pAdapterInfo);


修改IP、DNS、MAC工具VC源码实现

http://blog.csdn.net/cjf_iceking/article/details/7667522


原创粉丝点击