< Back

Import-PInvokeMethod

Sat Jan 18, 2020 6:50 pm

NAME Import-PInvokeMethod



SYNOPSIS

Defines a Win32 function as a PInvoke method.





SYNTAX

Import-PInvokeMethod [-TypeBuilder] <TypeBuilder> [-DllName] <String> [-Name] <String> [-ReturnType] <Object>

[[-ParameterTypes] <Object[]>] [-SetLastError] [[-CharSet] {None | Ansi | Unicode | Auto}] [-PassThru]

[<CommonParameters>]





DESCRIPTION

Defines a Win32 function as a PInvoke method and control some of the marshaling techniques used for that function.





PARAMETERS

-TypeBuilder <TypeBuilder>

A TypeBuilder that represents the class to define the static methods in.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-DllName <String>

The name of the Dll that contains the function to import.



Required? true

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Name <String>

The name of the Win32 function to import.



Required? true

Position? 3

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ReturnType <Object>

The type that is returned by the function. Can also be a Hashtable with the following keys;



Type: The type of the return value of the function.

MarshalAs: An optional hash that contains the parameter for Set-MarshalAsAttribute.

Type: Sets the [MarshalAs(UnmanagedType.)] value for the parameter.

SizeConst: Optional SizeConst value for MarshalAs.

ArraySubType: Optional ArraySubType value for MarshalAs.



Required? true

Position? 4

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ParameterTypes <Object[]>

A list of types of the functions parameters. Each parameter is represented by a [Type] value for a simple

parameter type but can also be a hashtable with the following keys;



Type: Must be set to the type of the parameter

Ref: Whether it is a [Ref] type, e.g. is ref or has an out parameter in .NET.

MarshalAs: An optional hash that contains the parameter for Set-MarshalAsAttribute.

Type: Sets the [MarshalAs(UnmanagedType.)] value for the parameter.

SizeConst: Optional SizeConst value for MarshalAs.

ArraySubType: Optional ArraySubType value for MarshalAs.



Required? false

Position? 5

Default value [Object[]]@()

Accept pipeline input? false

Accept wildcard characters? false



-SetLastError [<SwitchParameter>]

Sets the 'SetLastError=true' field on the DllImport attribute of the function.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-CharSet

Sets the 'CharSet' field on the DllImport attribute of the function.



Required? false

Position? 6

Default value Auto

Accept pipeline input? false

Accept wildcard characters? false



-PassThru [<SwitchParameter>]

Output the MethodBuilder of the defined function.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS

None, System.Reflection.Emit.MethodBuilder

This cmdlet generates a System.Reflection.Emit.MethodInfo if you specify -PassThru, Otherwise this cmdlet does not

return any output.





-------------------------- EXAMPLE 1 --------------------------



PS C:\\>$module_builder = New-DynamicModule -Name PInvokeHelper



$type_builder = $module_buidler.DefineType('PInvokeHelper.NativeMethods', 'Public, Class')



$function_definition = @{

DllName = 'Kernel32.dll'

Name = 'CreateProcessW'

ReturnType = ([System.Boolean])

ParameterTypes = @(

@{

Type = ([System.String])

MarshalAs = @{

Type = [System.Runtime.InteropServices.UnmanagedType]::LPWStr

}

},

[System.Text.StringBuilder],

[System.IntPtr],

[System.IntPtr],

[System.Boolean],

[PSLimitedProcess.ProcessCreationFlags],

[System.IntPtr],

@{

Type = ([System.String])

MarshalAs = @{

Type = [System.Runtime.InteropServices.UnmanagedType]::LPWStr

}

},

@{ Ref = $true; Type = [PSLimitedProcess.STARTUPINFOEX] },

@{ Ref = $true; Type = [PSLimitedProcess.PROCESS_INFORMATION] }

)

SetLastError = $true

}

Import-PInvokeMethod -TypeBuilder $type_builder @function_definition



# Call once all functions have been defined in the type/class

$type_builder.CreateType() > $null











RELATED LINKS