lib/delegate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#requires -version 6
using namespace System.Reflection
using namespace System.Reflection.Emit
using namespace System.Linq.Expressions
using namespace System.Runtime.InteropServices

function Get-ProcAddress {
  [OutputType([Hashtable])]
  [CmdletBinding()]
  param(
    [Parameter(Mandatory, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$Module,

    [Parameter(Mandatory, Position=1)]
    [ValidateNotNullOrEmpty()]
    [String[]]$Function
  )

  process {
    $kernel32 = @{}

    [Assembly]::LoadFile("$(
      [RuntimeEnvironment]::GetRuntimeDirectory()
    )Microsoft.Win32.SystemEvents.dll"
).GetType('Interop').GetNestedType(
      'Kernel32', [BindingFlags]'NonPublic'
    ).GetMethods([BindingFlags]'NonPublic, Static, Public').Where{
      $_.Name -cmatch '\AGet(Proc|Mod)'
    }.ForEach{ $kernel32[$_.Name] = $_ }

    if ((
      $mod = $kernel32.GetModuleHandle.Invoke($null, @($Module))
    ) -eq [IntPtr]::Zero) { throw [DllNotFoundException]::new() }

    $funcs = @{}
    $Function.ForEach{
      if ((
        $$ = $kernel32.GetProcAddress.Invoke($null, @($mod, $_))
      ) -ne [IntPtr]::Zero) { $funcs.$_ = $$ }
    }
    $funcs
  }
}

function Set-Delegate {
  [OutputType([Type])]
  [CmdletBinding(DefaultParameterSetName='Prototype')]
  param(
    [Parameter(Mandatory, ParameterSetName='Prototype', Position=0)]
    [ValidateNotNull()]
    [Alias('p')]
    [Type]$Prototype,

    [Parameter(Mandatory, ParameterSetName='PrototypeAsTypeArray', Position=0)]
    [ValidateNotNullOrEmpty()]
    [Alias('pa')]
    [Type[]]$PrototypeAsTypeArray,

    [Parameter(Mandatory, Position=1)]
    [ValidateScript({$_ -ne [IntPtr]::Zero})]
    [IntPtr]$Address,

    [Parameter(Position=2)]
    [ValidateNotNullOrEmpty()]
    [CallingConvention]$CallingConvention = 'StdCall'
  )

  process {
    switch ($PSCmdlet.ParameterSetName) {
      'Prototype'            {
        $method = $Prototype.GetMethod('Invoke')
        $returntype, $paramtypes = $method.ReturnType, $method.GetParameters().ParameterType
        $paramtypes = ($paramtypes, $null)[!$paramtypes]
        $il, $sz = ($holder = [DynamicMethod]::new(
          'Invoke', $returntype, $paramtypes, $Prototype
        )).GetILGenerator(), [IntPtr]::Size

        if ($paramtypes) {
          (0..($paramtypes.Length - 1)).ForEach{ $il.Emit([OpCodes]::ldarg, $_) }
        }

        $il.Emit([OpCodes]::"ldc_i$sz", $Address."ToInt$((32, 64)[$sz / 4 - 1])"())
        $il.EmitCalli([OpCodes]::calli, $CallingConvention, $returntype, $paramtypes)
        $il.Emit([OpCodes]::ret)

        $holder.CreateDelegate($Prototype)
      }
      'PrototypeAsTypeArray' {
        [Marshal]::GetDelegateForFunctionPointer(
          $Address, [Delegate]::CreateDelegate(
            [Func[[Type[]], Type]],
            [Expression].Assembly.GetType(
              'System.Linq.Expressions.Compiler.DelegateHelpers'
            ).GetMethod(
              'MakeNewCustomDelegate', [BindingFlags]'NonPublic, Static'
            )
          ).Invoke($PrototypeAsTypeArray)
        )
      }
    } # switch
  }
}

function New-Delegate {
  [OutputType([Hashtable])]
  [CmdletBinding()]
  param(
    [Parameter(Mandatory, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$Module,

    [Parameter(Mandatory, Position=1)]
    [ValidateNotNull()]
    [Hashtable]$Signature
  )

  process {
    $funcs, $addr = @{}, (Get-ProcAddress -Module $Module -Function $Signature.Keys)
    $addr.Keys.ForEach{
      $ptr, $sig = $addr.$_, $Signature.$_
      if (!$sig) { throw [InvalidOperationException]::new() }
      $funcs.$_ = switch -Regex ($sig.Name) {
        '\A(Action|Func)' { Set-Delegate -Address $ptr -Prototype $sig }
        default { Set-Delegate -Address $ptr -PrototypeAsTypeArray $sig }
      }
    }
    $funcs
  }
}