Powershellを効果的に「パイプ」するプログラムを書くと、実際に画面上にプロンプトを表示せずにコマンドを送信してC++で応答を解析できます。Visual C++(API)でPowerShellにアクセスする
ヒント?
Powershellを効果的に「パイプ」するプログラムを書くと、実際に画面上にプロンプトを表示せずにコマンドを送信してC++で応答を解析できます。Visual C++(API)でPowerShellにアクセスする
ヒント?
確かにこれを行うことができますが、PowerShellのアーキテクチャは完全にCLRオブジェクトモデルに基づいているため、マネージコードを使用する必要があります。
C++の構文はわかりませんが、System.Management.Automation.dllのPowerShellクラスで開始し、静的Createメソッドを使用して、データをパイプしてコマンドを実行できるインスタンスを作成できます。
私はあなたの問題を解決するかもしれない答えを持っていますが、それは文字通りあなたが求めた質問に対する答えではありません。
私の現在のプロジェクトで必要なのは、ウィンドウを開かずに標準出力または標準エラーに書き込まれた結果を読み出す機会を与えるシステムコールです。
あなたはそれと一緒に暮らすことができる場合 - ここでは、前に述べたコードベースからいくつかのコードです:
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() : "";
}
}
また:コードではなくC++のC#であることをご容赦ください。
ご覧のとおり、プログラムを呼び出すとPowerShellは使用されません。それが既にあなたのための回避策であれば - 大丈夫です。そうでない場合は、this approachを使用してコマンドラインからPowershellを呼び出すことを検討し、そのようになりたい場所に移動することも可能です。
希望するか、少なくともあなたにアイデアを与えてくれることを願っています。
このために、特に管理されたDLLを作成し、[ここのソリューション](http://stackoverflow.com/questions/2719985/calling-managed-c-sharp-functions-from-unmanaged)を使用することもできます。 -c)アンマネージC++から呼び出します。 – CherryDT