Windows获得MAC地点

时间:2022-04-03 08:48:56

Windows获得MAC地点,StringBuffer是一个自界说的字符串类,,可参考

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <windowsx.h> extern "C" { #include "StringBuffer.h" } typedef struct _Mac { char MacName[256]; char MacAdd[256]; }Mac; int WindowsCommand(StringBuffer *sb,char *cmd) { SECURITY_ATTRIBUTES sa; HANDLE hRead,hWrite; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; if (!CreatePipe(&hRead,&hWrite,&sa,0)) return 0; char command[1024]; ZeroMemory(command,sizeof(command)); strcpy(command,TEXT("Cmd.exe /C ")); strcat(command,cmd); STARTUPINFO si; PROCESS_INFORMATION pi; si.cb = sizeof(STARTUPINFO); GetStartupInfo(&si); si.hStdError = hWrite; si.hStdOutput = hWrite; si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES; if (!CreateProcess(NULL,command,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) { CloseHandle(hWrite); CloseHandle(hRead); return 0; } CloseHandle(hWrite); char buffer[2048],*result = NULL; ZeroMemory(buffer,sizeof(buffer)); DWORD bytesRead; while(ReadFile(hRead,buffer,sizeof(buffer),&bytesRead,NULL) && bytesRead>0) { StringBufferAddEx(sb,buffer,bytesRead); ZeroMemory(buffer,sizeof(buffer)); } CloseHandle(hRead); return 1; } int main(int argc,char *argv[]) { StringBuffer sb; StringBufferInit(&sb); WindowsCommand(&sb,"ipconfig /all"); char *buf = StringBufferToString(&sb); StringBufferFree(&sb); char *ms = strstr(buf,"描述"); while(ms != NULL) { Mac mac; memset(&mac,0,sizeof(Mac)); sscanf(ms,"%*[^:]%*[^ ]%[^\n]",mac.MacName); ms = strstr(ms,"物理地点"); if(ms) sscanf(ms,"%*[^:]%*[^ ]%[^\n]",mac.MacAdd); printf("%s\n%s\n\n",mac.MacName+1,mac.MacAdd+1); ms = strstr(ms,"描述"); } return 1; }

Windows获得MAC地点