zoukankan      html  css  js  c++  java
  • 读取相关连接的物理地址

    利用上次的匿名管道就可以读取cmd命令的相关信息

    将其写成类CmdInfoToPile     

    CmdInfoToPipe.h

     1 #ifndef NETINFO_CMDINFOTOPIPE_H_
     2 #define NETINFO_CMDINFOTOPIPE_H_
     3 #include <windows.h>
     4 #include <stdio.h>
     5 #include <string>
     6 #include <vector>
     7 
     8 class CmdInfoToPipe
     9 {
    10 public:
    11     CmdInfoToPipe();
    12     ~CmdInfoToPipe();
    13     DWORD getCmdInfo(char *cmd);//匿名管道读取cmd信息
    14     std::vector<std::string> get_Mac_Address(std::string strconnect = "本地连接", std::string straddress = "物理地址");//读取物理地址
    15 private:
    16     std::string strcmd_;
    17     std::string cmd_;
    18 };
    19 #endif//CmdInfoToPipe.h

    CmdInfoToPipe.cpp

     1 #include "CmdInfoToPipe.h"
     2 
     3 
     4 CmdInfoToPipe::CmdInfoToPipe()
     5 {
     6     cmd_ = "Cmd.exe /C ";
     7     strcmd_ = "";
     8 }
     9 
    10 
    11 CmdInfoToPipe::~CmdInfoToPipe()
    12 {
    13 }
    14 
    15 std::vector<std::string>  CmdInfoToPipe::get_Mac_Address(std::string strconnect, std::string straddress){
    16     int connect = 0;
    17     int address = 0;
    18     printf("%s %s
    ", strconnect.c_str(), straddress.c_str());
    19     std::vector<std::string> vstr;
    20 
    21     while ((connect = strcmd_.find(strconnect, connect)) != std::string::npos){//可能有多个这种连接
    22         if((address = strcmd_.find(straddress, connect + 1)) != std::string::npos){
    23             if ((address = strcmd_.find("-", address + 1)) != std::string::npos){
    24                 vstr.push_back(strcmd_.substr(address - 2, 17));
    25                 connect = address + 16;
    26             }
    27         }
    28     }
    29     
    30     return vstr;
    31 }
    32 DWORD CmdInfoToPipe::getCmdInfo(char *cmd){
    33     cmd_ +=std::string(cmd);
    34     char mycmd[100];
    35     sprintf_s(mycmd, "%s", cmd_.c_str());
    36     char Buffer[4096];
    37     STARTUPINFO sInfo;
    38     PROCESS_INFORMATION pInfo;
    39     SECURITY_ATTRIBUTES sa;
    40     HANDLE hRead, hWrite;
    41     DWORD bytesRead;
    42     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    43     sa.lpSecurityDescriptor = NULL;
    44     sa.bInheritHandle = TRUE;
    45 
    46     if (!CreatePipe(&hRead, &hWrite, &sa, 0)) //创建匿名管道
    47     {
    48         return GetLastError();
    49     }
    50 
    51     GetStartupInfo(&sInfo);
    52     sInfo.cb = sizeof(sInfo);
    53     sInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    54     sInfo.wShowWindow = SW_HIDE;
    55     sInfo.hStdError = hWrite;   //将管道的写端交给子进程
    56     sInfo.hStdOutput = hWrite;
    57     memset(&pInfo, 0, sizeof(pInfo));
    58 
    59     if (!CreateProcess(NULL, mycmd, NULL, NULL, TRUE, 0, NULL, NULL, &sInfo, &pInfo)) //创建子进程
    60     {
    61         CloseHandle(hWrite);
    62         CloseHandle(hRead);
    63         return GetLastError();
    64     }
    65     CloseHandle(hWrite); //关闭父进程的写端
    66     strcmd_ = "";
    67 
    68     for (int i = 0;; ++i)
    69     {
    70         if (!ReadFile(hRead, Buffer, sizeof(Buffer) - 1, &bytesRead, NULL)) //读取内容
    71         {
    72             break;
    73         }
    74         Buffer[bytesRead] = 0;
    75         strcmd_ += std::string(Buffer);//防止出现查找字符分散在各个BUFFER中
    76     }
    77 
    78     WaitForSingleObject(pInfo.hProcess, INFINITE);
    79     CloseHandle(hRead);
    80     return GetLastError();
    81 }

    测试代码

     1 #include <CmdInfoToPipe.h>
     2 #include <cstdio>
     3 #include<vector>
     4 #include<string>
     5 int main()
     6 {
     7     char *mycmd = "ipconfig/all";
     8     CmdInfoToPipe pCItP;
     9     pCItP.getCmdInfo(mycmd);
    10     std::vector<std::string> vtr = pCItP.get_Mac_Address("无线网络连接");
    11     for (auto i : vtr){
    12         printf("%s
    ", i.c_str());
    13     }
    14     system("pause");
    15     return 0;
    16 }
  • 相关阅读:
    什么是前后端分离?
    Ubuntu修改时区和更新时间
    待学
    Pycharm默认输入状态是insert状态,选中文字无法直接输入替换或删除
    使用jsonify返回json数据
    Linux
    Linux
    Linux
    JavaScript
    JavaScript
  • 原文地址:https://www.cnblogs.com/onlyac/p/5346549.html
Copyright © 2011-2022 走看看