【问题标题】:Programmatic system call WDCI Unexpected Behavior (Elevated Permissions)程序化系统调用 WDCI 意外行为(提升权限)
【发布时间】:2016-03-04 21:25:45
【问题描述】:

如果我想在 Widows 上禁用特定的 NIC,我通常会这样做:

wmic.exe path win32_networkadapter where "NetConnectionID = 'Local Area Connection 2'" call disable

从提升的权限/以管理员身份运行命令行提示符...它可以工作,生活很美好。

所以我编译了这个简单的 C++ CLI 应用程序:

#include "stdafx.h"
#include <string>
#include <iostream>
#include <cstdio>
#include <memory>

#ifndef popen
FILE *__cdecl popen(const char *_Command, const char *_Mode) { return _popen(_Command, _Mode); }
#endif

#ifndef pclose
int __cdecl pclose(FILE *_Stream) { return _pclose(_Stream); }
#endif

std::string exec(const char* cmd) {
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while (!feof(pipe.get())) {
        if (fgets(buffer, 128, pipe.get()) != NULL) result += buffer;
    }
    return result;
}

int main() {
    std::cout << exec("wmic path win32_networkadapter where ""NetConnectionID = 'Local Area Connection 2'"" call disable");
    return 0;
}

我从相同的提升权限命令行提示符执行...但我得到的响应是:

C:\elevatesdh\Debug>elevatesdh.exe
Invalid Verb.

什么给了?

【问题讨论】:

    标签: c++ process network-programming wmic elevated-privileges


    【解决方案1】:

    我认为你必须改变这一行

    std::cout << exec("wmic path win32_networkadapter where ""NetConnectionID = 'Local Area Connection 2'"" call disable");
    

    std::cout << exec("wmic path win32_networkadapter where \"NetConnectionID = 'Local Area Connection 2'\" call disable");
    

    但我可能错了……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2012-10-21
      • 2023-03-09
      • 2020-05-21
      • 2011-11-13
      • 2023-04-08
      相关资源
      最近更新 更多