PoshCode Archive  Artifact [4b0d7bb3f0]

Artifact 4b0d7bb3f0579b27c2bc888039eef74f296ccb73cd1963aca4c73d23c5303cc1:

  • File New-PInvoke.ps1 — part of check-in [f4703baddc] at 2018-06-10 12:57:01 on branch trunk — A fixed version of the New-PInvoke function that’s in the Windows 7 resource kit (PowerShellPack) PSCodeGen module. This one has correct documentation and a better example. It also generates a (global) PowerShell wrapper function so you can call the API function easily. (user: Joel Bennett size: 2650)

# encoding: ascii
# api: powershell
# title: New-PInvoke
# description: A fixed version of the New-PInvoke function that’s in the Windows 7 resource kit (PowerShellPack) PSCodeGen module.  This one has correct documentation and a better example. It also generates a (global) PowerShell wrapper function so you can call the API function easily.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: New-PInvoke
# x-poshcode-id: 1409
# x-archived: 2017-05-30T18:49:58
# x-published: 2010-10-20T10:22:00
#
#
function New-PInvoke
{
    <#
    .Synopsis
        Generate a powershell function alias to a Win32|C api function
    .Description
        Creates C# code to access a C function, and exposes it via a powershell function
    .Example
        New-PInvoke user32 "void FlashWindow(IntPtr hwnd, bool bInvert)"
        
Generates a function for FlashWindow which ignores the boolean return value, and allows you to make a window flash to get the user's attention. Once you've created this function, you could use this line to make a PowerShell window flash at the end of a long-running script:

        C:\PS>FlashWindow (ps -id $pid).MainWindowHandle $true
    .Parameter Library
        A C Library containing code to invoke
    .Parameter Signature
        The C# signature of the external method
    .Parameter OutputText
        If Set, retuns the source code for the pinvoke method.
        If not, compiles the type. 
    #>
    param(
    [Parameter(Mandatory=$true, 
        HelpMessage="The C Library Containing the Function, i.e. User32")]
    [String]
    $Library,
    
    [Parameter(Mandatory=$true,
        HelpMessage="The Signature of the Method, i.e.: int GetSystemMetrics(uint Metric)")]
    [String]
    $Signature,
    
    [Switch]
    $OutputText
    )
    
    process {
        if ($Library -notlike "*.dll*") {
            $Library+=".dll"
        }
        if ($signature -notlike "*;") {
            $Signature+=";"
        }
        if ($signature -notlike "public static extern*") {
            $signature = "public static extern $signature"
        }
        
        $name = $($signature -replace "^.*?\s(\w+)\(.*$",'$1')
        
        $MemberDefinition = "[DllImport(`"$Library`")]`n$Signature"
        
        if (-not $OutputText) {
            $type = Add-Type -PassThru -Name "PInvoke$(Get-Random)" -MemberDefinition $MemberDefinition
            iex "New-Item Function:Global:$name -Value { [$($type.FullName)]::$name.Invoke( `$args ) }"
        } else {
            $MemberDefinition
        }
    }
}