This just for completeness' sake:
PowerShell has an API - see System.Management.Automation.PowerShell. The API is managed (i. e. .NET-based). It's possible to build a mixed mode C++ application and invoke said API from the managed part.
Place the following into a separate C++ file:
#include "stdafx.h"
#include <vcclr.h>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Management.Automation.dll>
using namespace System;
using namespace System::Management::Automation;
void RunPowerShell(LPCWSTR s)
{
PowerShell::Create()->AddScript(gcnew String(s))->Invoke();
}
In Project Properties, under VC++ Directories, add C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0
to Reference Directories (your path may vary).
Set the following compiler options for that file only:
- Common Language RunTime Support (/clr)
- Debug information format - Program Database (/Zi)
- Enable C++ exceptions - No
- Basic runtime checks - Default
- Precompiled header - Not Using Precompiled Headers
You need /clr
for calling .NET from C++, but /clr
is incompatible with a bunch of other C++ options. If you miss something, the compiler error messages will let you know.
Declare void RunPowerShell(LPCWSTR)
as a regular external function in the unmanaged parts of the project, invoke as needed.
That said, whatever your Powershell does, C++/Win32 probably can do too. That said, some one-line features of Powershell (e. g. the remoting) would translate into hundreds of lines of C++.