This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
ActivationContext.ps1
132 lines (115 loc) · 3.36 KB
/
ActivationContext.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
131
132
$Script:ActivationContexts = @()
<#
.Synopsis
Creates a Windows Activation Context.
.DESCRIPTION
Creates a Windows Activation Context. This cmdlet can optionally open
the activation context.
.EXAMPLE
Create-ActivationContext -Manifest E:\IE.EXE.Manifest
.EXAMPLE
Create-ActivationContext -Open -Manifest E:\IE.EXE.Manifest
#>
function New-ActivationContext
{
[CmdletBinding()]
param(
# The manifest to use for registry free COM activation
[Parameter(Mandatory)]
$manifest,
[Parameter()]
#Opens the context.
[Switch]$Open
)
End
{
if (-not (Test-Path $Manifest))
{
Write-Error "$Manifest does not exist"
return
}
[IntPtr]$ActivationContext = [IntPtr]::Zero
$actCtx = New-Object PoshInternals.ACTCTX
$actCtx.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf([Type]$actCtx.GetType())
$actCtx.dwFlags = 0
$actCtx.lpSource = $manifest
$actCtx.lpResourceName = $null
$ActivationContext = [PoshInternals.Kernel32]::CreateActCtx([ref]$actCtx)
if ($ActivationContext -eq [IntPtr]-1)
{
throw new-object System.ComponentModel.Win32Exception
}
$ActivationContextObject = @{Handle=$ActivationContext;Cookie=$cookie;Manifest=$Manifest}
if ($Open)
{
Open-ActivationContext -ActivationContext $ActivationContextObject
}
[PSCustomObject]$ActivationContextObject
}
}
<#
.Synopsis
Opens a Windows Activation Context.
.DESCRIPTION
Opens a Windows Activation Context. This cmdlet accepts a context created by
New-ActivationContext.
.EXAMPLE
Open-ActivationContext -ActivationContext $Context
#>
function Open-ActivationContext
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline=$true)]
[PSCustomObject]$ActivationContext
)
Process
{
[Int]$Cookie = 0
if (-not ([PoshInternals.Kernel32]::ActivateActCtx($ActivationContext.Handle, [ref]$Cookie)))
{
Write-Error (new-object System.ComponentModel.Win32Exception)
}
$ActivationContext.Cookie = $Cookie
}
}
<#
.Synopsis
Closes a Windows activation context.
.DESCRIPTION
Closes a Windows activation context that was opened by Enter-ActivationContext.
.EXAMPLE
Close-ActivationContext
#>
function Close-ActivationContext
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline=$true)]
[PSCustomObject]$ActivationContext
)
Process {
[PoshInternals.Kernel32]::DeactivateActCtx(0, $ActivationContext.Cookie) | Out-Null
}
}
<#
.Synopsis
Removes an activation context.
.DESCRIPTION
Removes an activation context that was created by New-ActivationContext. Open-ActivationContext will no longer
work for the removed activation context.
.EXAMPLE
Remove-ActivationContext -ActicationContext $Context
#>
function Remove-ActivationContext
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline=$true)]
[PSCustomObject]$ActivationContext
)
Process {
[PoshInternals.Kernel32]::DeactivateActCtx(0, $ActivationContext.Cookie) | Out-Null
[PoshInternals.Kernel32]::ReleaseActCtx($ActivationContext.Handle) | Out-Null
}
}