windows 从非托管 C++ 调用 PowerShell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/492953/
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
Calling PowerShell scripts from unmanaged C++
提问by Austin Ziegler
Microsoft appears to be moving a lot of configuration and query capabilities to PowerShell (accessible from C# or managed C++), while deprecating and even removing older APIs (accessible for C or unmanaged C++). Those of us who have extensive unmanaged C++ programs that can't switch to managed C++ may have a need to call PowerShell cmdlets (at least, I have one now) -- how can we do so?
Microsoft 似乎正在将许多配置和查询功能转移到 PowerShell(可从 C# 或托管 C++ 访问),同时弃用甚至删除旧 API(可用于 C 或非托管 C++)。我们这些拥有大量无法切换到托管 C++ 的非托管 C++ 程序的人可能需要调用 PowerShell cmdlet(至少,我现在有一个)——我们怎么能这样做呢?
There are suggestions that a reverse-PInvoke (managed/unmanaged thunking) might be able to do something, but I'm hoping for a better way than either thunking or parsing text output from a PowerShell script.
有人建议反向 PInvoke(托管/非托管 thunking)可能能够做一些事情,但我希望有一种比从 PowerShell 脚本 thunking 或解析文本输出更好的方法。
[Note: This is a reworked and generalized query I made yesterdaythat may have been too specialized.]
采纳答案by Nick
The MS suggestion would probably be to compile specific classes/modules in your generally unmanaged C++ app as managed code (compiling individual files with /clr), and letting the IJW transition code manage the calls to/from it (and calling PowerShell API's from the managed bits). I've been told (from MS VC++ people) that's the expected methodology for adding bits of managed calls to large unmanaged applications.
MS 的建议可能是将通常非托管的 C++ 应用程序中的特定类/模块编译为托管代码(使用 /clr 编译单个文件),并让 IJW 转换代码管理对它的调用(并从托管位)。有人告诉我(从 MS VC++ 人员那里)这是向大型非托管应用程序添加托管调用位的预期方法。
Hope that helps.
希望有帮助。