-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSWin32.psm1
64 lines (52 loc) · 2.04 KB
/
PSWin32.psm1
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
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# load CsWin32
ls $PSScriptRoot\res\CsWin32 -Filter *.dll | Import-Module
function GenerateWin32Function($GeneratedFunctions) {
$Options = [Microsoft.Windows.CsWin32.GeneratorOptions]::new()
$Options.EmitSingleFile = $true
$Options.Public = $true
$Generator = [Microsoft.Windows.CsWin32.Generator]::new("$PSScriptRoot\res\Windows.Win32.winmd", $null, $Options, $null, $null)
$GeneratedFunctions | % {
$Success = $Generator.TryGenerate($_, [ref]$null, [System.Threading.CancellationToken]::None)
if (-not $Success) {
throw "Failed to generate '$_'."
}
}
$Files = $Generator.GetCompilationUnits([System.Threading.CancellationToken]::None)
$GeneratedStr = $Files.'NativeMethods.g.cs'.ToFullString()
Add-Type $GeneratedStr -CompilerOptions /unsafe
}
function TypeToStr($Type) {
$Generics = $Type.GetGenericArguments()
if (-not $Generics) {
return $Type.Name
} elseif ($Type.Namespace -eq "System" -and ($Type.Name -eq "Nullable" -or $Type.Name.StartsWith("Nullable``"))) {
return (TypeToStr $Generics[0]) + "?"
} else {
$GenericStr = $Generics | % {TypeToStr $_}
return $Type.Name + "[" + ($GenericStr -join ", ") + "]"
}
}
function Invoke-PSWin32 {
[CmdletBinding()]
[Alias("win32")]
param(
[Parameter(Mandatory, ValueFromRemainingArguments)]
[string[]]
$GeneratedFunctions
)
GenerateWin32Function $GeneratedFunctions
[Windows.Win32.PInvoke].GetMethods("DeclaredOnly,Static,Public") | group Name | % {
$Header = "[Windows.Win32.PInvoke]::" + $_.Name
Write-Host $Header -ForegroundColor Green
Write-Host ("-" * $Header.Length) -ForegroundColor Green
$_.Group | % {
$Params = $_.GetParameters() | % {
(TypeToStr $_.ParameterType) + " " + $_.Name
}
Write-Host "$(TypeToStr $_.ReturnType) $($_.Name)($($Params -join ", "))"
}
Write-Host ""
}
}