[code language=”powershell” title=”Powershell Code”]
$signature = @’
[DllImport("user32.dll", SetLastError = true)]
public static extern bool LockWorkStation();
‘@
Add-Type -MemberDefinition $signature -Name CustomName -Namespace CustomNamespace -PassThru
[CustomNamespace.CustomName]::Lockworkstation()
[/code]
Notes
- Keyword PUBLIC is a must, or else wont be able to call the native function.
- Unlike variables, Add-Type cant be changed once created in a powershell session.
References
- http://msdn.microsoft.com/en-us/library/windows/desktop/aa376875(v=vs.85).aspx
- http://www.pinvoke.net/default.aspx/user32/LockWorkStation.html