windows 通过 Visual C++ (API) 访问 Powershell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4611415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Powershell via Visual C++ (API)
提问by Disco
I'd like to write a program that effectively "pipes" Powershell, so that I can send commands and parse the responses in C++, without actually opening the prompt on the screen.
我想编写一个有效地“管道”Powershell 的程序,以便我可以在 C++ 中发送命令并解析响应,而无需实际打开屏幕上的提示。
Any tips?
有小费吗?
采纳答案by Josh
You can certainly do this but you'll need to use managed code as PowerShell's architecture is based entirely on the CLR object model.
您当然可以这样做,但您需要使用托管代码,因为 PowerShell 的体系结构完全基于 CLR 对象模型。
I'm not sure of the C++ syntax but you can start with the PowerShellclass in System.Management.Automation.dll and use its static Createmethod to create an instance to which you can pipe data and run commands.
我不确定 C++ 语法,但您可以从System.Management.Automation.dll 中的PowerShell类开始,并使用其静态Create方法创建一个实例,您可以将数据传输到该实例并运行命令。
回答by dr. rAI
I have an answer that might solve your problem although it is not literally an answer to the question you asked.
我有一个可能会解决您的问题的答案,尽管它实际上并不是您提出的问题的答案。
Well, what I need in my current project is a system call that does not open a window and that does give me the opportunity to read out the results written to standard-output or standard-error.
好吧,我在当前项目中需要的是一个不打开窗口的系统调用,它让我有机会读出写入标准输出或标准错误的结果。
In case you can live with that - here is some code from the before-mentioned codebase:
如果您可以接受 - 这里是来自前面提到的代码库的一些代码:
public class RsbSystem
{
string command = null;
string param = null;
string commandLine = null;
public int ExitCode = 0;
//..
/// <summary>Exec for apps that don't want console output</summary>
/// <param name="msg">returns output of called program</param>
/// <returns>0 if ok</returns>
/// <remarks>RsbSystem instance keeps the result in member ExitCode</remarks>
public int Exec(ref string msg)
{
var p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.Arguments = param;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
if (!p.StandardOutput.EndOfStream)
msg = p.StandardOutput.ReadToEnd();
if (!p.StandardError.EndOfStream)
msg += p.StandardError.ReadToEnd();
p.WaitForExit(120000); // this needs to come after readToEnd() RSB: https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx
ExitCode = p.ExitCode;
p.Dispose();
return ExitCode;
}
// ..
public RsbSystem(string cmdLine)
{
commandLine = cmdLine;
var pos = 0;
if (cmdLine[0] == '"')
pos = cmdLine.IndexOf("\" ") + 1;
else pos = cmdLine.IndexOf(" ");
command = pos > -1 ? cmdLine.Substring(0, pos).Trim() : cmdLine;
param = pos > -1 ? cmdLine.Substring(pos + 1).TrimStart() : "";
}
}
Also: please forgive me that the code is in C# instead of C++.
另外:请原谅我的代码是 C# 而不是 C++。
As you can see it calls any program - and does not use PowerShell. If that's already a workaround for you - fine. If not, you might consider calling the Powershell from the commandline using this approachand possible get where you want to be that way.
如您所见,它调用任何程序 - 并且不使用 PowerShell。如果这对您来说已经是一种解决方法 - 很好。如果没有,您可以考虑使用这种方法从命令行调用 Powershell,并可能以这种方式到达您想要的位置。
Hope it helps or at least gives you an idea.
希望它有帮助或至少给你一个想法。