Powershell: Enable native interop with static extern class methods

Created on 13 Dec 2016  路  2Comments  路  Source: PowerShell/PowerShell

It would be really neat if we could to native interop with classes.

class NativeMethods{
  [DllImport("kernel32", SetLastError=$true)]
  hidden static extern [bool] SetConsoleMode([IntPtr] $handle, [int] $mode)
}

How much work would it be to get this working?

WG-Language

Most helpful comment

Looks like we may be able to do this as soon as we're clear to move to .NET 3.0 - this already works on 3.0.0-preview2 release of the SDK https://github.com/IISResetMe/PowerShell/commit/4fa5363598e7736771e8cc1b06e8ce8edd405d4f (warrants a bit of a cleanup in PSType.cs, I'll keep working on that in the meantime)

All 2 comments

Maybe not a lot of work, but it is a common enough ask that we should do it.

There is one important language design choice to make, how to declare a struct. Pinvoke would be much less useful without structs.

A struct is probably not generally useful in PowerShell, we box way too often, so I was thinking of using an attribute like ExplicitLayout to imply struct instead of adding a keyword.

Looks like we may be able to do this as soon as we're clear to move to .NET 3.0 - this already works on 3.0.0-preview2 release of the SDK https://github.com/IISResetMe/PowerShell/commit/4fa5363598e7736771e8cc1b06e8ce8edd405d4f (warrants a bit of a cleanup in PSType.cs, I'll keep working on that in the meantime)

Was this page helpful?
0 / 5 - 0 ratings