Last active
February 21, 2017 18:14
-
-
Save Cauterite/2eb65105c98e10f2385c83d366e0df1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------------ | |
# loads libcurl.dll and calls curl_version() | |
# | |
set-strictmode -version 'latest' | |
$erroractionpreference = 'Stop' | |
#if ([intptr]::size -ne 8) { | |
# throw 'not a 64-bit powershell instance'} | |
$w32 = Add-Type -Namespace Win32 -Name Win32 -PassThru -MemberDefinition @' | |
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] | |
public static extern IntPtr LoadLibrary(string Name); | |
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)] | |
public static extern IntPtr GetProcAddress(IntPtr Mod, string Name); | |
[DllImport("kernel32.dll")] | |
public static extern uint GetLastError(); | |
'@ | |
function enforce($cond) { | |
# [intptr]::zero is a truthy value apparently | |
# so explicit test for 0 | |
if (!$cond -or ($cond -eq 0)) { | |
throw (new-object applicationexception( | |
'enforcement failed @ line {0}' -f $myinvocation.scriptlinenumber))} | |
$cond} | |
function get-delegatetype { | |
param ( | |
[outputtype([type])] | |
[parameter(position = 0)] | |
[type[]] | |
$parameters = (new-object type[](0)), | |
[parameter(position = 1)] | |
[type] | |
$returntype = [void] | |
) | |
function randomuuid {[guid]::newguid().tostring()} | |
$domain = [appdomain]::currentdomain | |
$dynassembly = new-object reflection.assemblyname(randomuuid) | |
$assemblybuilder = $domain.definedynamicassembly( | |
$dynassembly, [reflection.emit.assemblybuilderaccess]::run) | |
$modulebuilder = $assemblybuilder.definedynamicmodule((randomuuid), $false) | |
$typebuilder = $modulebuilder.definetype( | |
(randomuuid), | |
'Class, Public, Sealed, AnsiClass, AutoClass', | |
[multicastdelegate]) | |
$constructorbuilder = $typebuilder.defineconstructor( | |
'RTSpecialName, HideBySig, Public', | |
[reflection.callingconventions]::standard, | |
$parameters) | |
$constructorbuilder.setimplementationflags('Runtime, Managed') | |
$methodbuilder = $typebuilder.definemethod( | |
'Invoke', | |
'Public, HideBySig, NewSlot, Virtual', | |
$returntype, | |
$parameters) | |
$methodbuilder.setimplementationflags('Runtime, Managed') | |
write-output $typebuilder.createtype()} | |
# ------------------------------------------------------------------------------ | |
$lib = enforce $w32::LoadLibrary('libcurl') | |
$f = [runtime.interopservices.marshal]::getdelegateforfunctionpointer( | |
(enforce $w32::GetProcAddress($lib, 'curl_version')), | |
(get-delegatetype @() ([intptr]))) | |
echo ([runtime.interopservices.marshal]::ptrtostringansi( | |
$f.invoke())) | |
# ------------------------------------------------------------------------------ | |
# ------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment