【问题标题】:Visual Studio C++ remote debugging - running CMD as admin on remote machineVisual Studio C++ 远程调试 - 在远程机器上以管理员身份运行 CMD
【发布时间】:2020-11-20 05:39:17
【问题描述】:

我正在使用 Visual Studio 2017 通过VS Remote Debugging 在运行 Windows 的 VM 上调试 C++ 代码。作为我的代码的一部分,我正在通过std::system 执行命令,它必须以管理员身份运行才能成功。

以管理员身份运行 VS 并在本地调试代码时——一切正常。

但是,在虚拟机上远程调试时,命令不会以管理员身份执行。我知道这是事实,因为某些命令需要这样做,并且输出明确指出事实并非如此。有没有办法让它工作?

我不介意使用不同的 API,std::system 似乎只是命令执行的“默认”。

【问题讨论】:

    标签: windows visual-studio visual-studio-2017 remote-debugging


    【解决方案1】:

    我建议您可以将Properties->Linker->Manifest File->UAC Execution Level 设置为requireAdministrator

    如果您想使用 Windows API,我发现ShellExcuteEx 可以满足您的需求。它的结构是SHELLEXECUTEINFOA。你可以参考这个例子:

    var ExeInfo = SHELLEXECUTEINFO();
    ExeInfo.lpVerb = “runas”;
    

    【讨论】:

    • 谢谢。这是一个很好的方向。
    【解决方案2】:

    感谢 Barrnet 的回答,我研究了 ShellExecuteEx 及其 “runas”动词

    这是一个完整的解决方案,以管理员身份运行 cmd 并将 cmd 输出作为字符串检索:(UAC 必须设置为最小值,以避免弹出窗口)

    #include <Windows.h>
    #include <vector>
    #include <string>
    #include <fstream>
    
    inline void Validate (const bool expression) 
    {
        if (!expression) 
            throw std::exception(); 
    }
    
    class TemporaryFile
    {
    public:
        TemporaryFile()
        {
            std::vector<char> tmpPath(MAX_PATH + 1, 0);
            Validate(0 != GetTempPathA(MAX_PATH, tmpPath.data()));
            std::vector<char> tmpFilename(MAX_PATH + 1, 0);
            Validate(0 != GetTempFileNameA(tmpPath.data(), "tmp", 0, tmpFilename.data()));
            _fullPath = std::string(tmpFilename.data());
        }
    
        ~TemporaryFile()
        {
            DeleteFileA(_fullPath.c_str());
        }
    
        std::string GetPath()
        {
            return _fullPath;
        }
        
    private:
        std::string _fullPath{};
    };
    
    std::string ReadFileContents(const std::string& filename)
    {
        std::ifstream ifs(filename);
        return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
    }
    
    std::string RunCmdAsAdmin(const std::string& cmd)
    {
        // file to hold cmd output
        TemporaryFile output;
    
        // the cmd will be passed to cmd.exe via '/c' flag, output will be written to tempfile
        const std::string modifiedCmd = "/c " + cmd + " > \"" + output.GetPath() + "\"";
    
        // create and launch cmd (async operation -- a seperate process is launched)
        SHELLEXECUTEINFO shellExecInfo{};
        shellExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
        shellExecInfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
        shellExecInfo.lpVerb = "runas";
        shellExecInfo.lpFile = "cmd.exe";
        shellExecInfo.lpParameters = modifiedCmd.c_str();
        shellExecInfo.nShow = SW_SHOWNORMAL;
        Validate(TRUE == ShellExecuteEx(&shellExecInfo));
    
        // wait for cmd to finish running and retrieve output
        static const DWORD MAX_WAIT_MS{ 5000 };
        Validate(WAIT_OBJECT_0 == WaitForSingleObject(shellExecInfo.hProcess, MAX_WAIT_MS));
        CloseHandle(shellExecInfo.hProcess);
        return ReadFileContents(output.GetPath());
    }
    
    int main()
    {
        const auto& cmdOutput = RunCmdAsAdmin("ipconfig"); 
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多