en-US/Ctypes.dll-Help.xml
<?xml version="1.0" encoding="utf-8"?> <helpItems schema="maml" xmlns="http://msh"> <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"> <command:details> <command:name>New-CtypesLib</command:name> <command:verb>New</command:verb> <command:noun>CtypesLib</command:noun> <maml:description> <maml:para>Create an object that represents a local library to call PInvoke methods on.</maml:para> </maml:description> </command:details> <maml:description> <maml:para>Creates an object that can be used to invoke a native method in a native library. The returned object is a dynamic object that allows the caller to invoke any exported function in the native library. Simply invoke the method by name and provide the required arguments. See about_CtypesInterface (./about_CtypesInterface.md)for more informatoin.</maml:para> </maml:description> <command:syntax> <command:syntaxItem> <maml:name>New-CtypesLib</maml:name> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="0" aliases="none"> <maml:name>Name</maml:name> <maml:description> <maml:para>The native library to interact with. On Windows this is typically a `.dll`, on Linux is `.so`, and on macOS is `.dylib`.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">String[]</command:parameterValue> <dev:type> <maml:name>String[]</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="proga"> <maml:name>ProgressAction</maml:name> <maml:description> <maml:para>New common parameter introduced in PowerShell 7.4.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ActionPreference</command:parameterValue> <dev:type> <maml:name>ActionPreference</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> </command:syntaxItem> </command:syntax> <command:parameters> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="0" aliases="none"> <maml:name>Name</maml:name> <maml:description> <maml:para>The native library to interact with. On Windows this is typically a `.dll`, on Linux is `.so`, and on macOS is `.dylib`.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">String[]</command:parameterValue> <dev:type> <maml:name>String[]</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="proga"> <maml:name>ProgressAction</maml:name> <maml:description> <maml:para>New common parameter introduced in PowerShell 7.4.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ActionPreference</command:parameterValue> <dev:type> <maml:name>ActionPreference</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> </command:parameters> <command:inputTypes> <command:inputType> <dev:type> <maml:name>System.String[]</maml:name> </dev:type> <maml:description> <maml:para>The library name can be piped into the cmdlet.</maml:para> </maml:description> </command:inputType> </command:inputTypes> <command:returnValues> <command:returnValue> <dev:type> <maml:name>Ctypes.Library</maml:name> </dev:type> <maml:description> <maml:para>The ctypes library object that was created. This is a dynamic object where new PInvoke methods can be called directly.</maml:para> </maml:description> </command:returnValue> </command:returnValues> <maml:alertSet> <maml:alert> <maml:para></maml:para> </maml:alert> </maml:alertSet> <command:examples> <command:example> <maml:title>---- Example 1 - Create object to call Kernel32 OpenProcess ----</maml:title> <dev:code>PS C:\> $k32 = New-CtypesLib kernel32.dll PS C:\> $k32.Returns([IntPtr]).SetLastError().OpenProcess(0x400, $false, $processId)</dev:code> <dev:remarks> <maml:para>Creates an object that exposes the functions in `kernel32.dll`. It then calls OpenProcess (https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess)that is exported in that library.</maml:para> </dev:remarks> </command:example> </command:examples> <command:relatedLinks> <maml:navigationLink> <maml:linkText>Online Version:</maml:linkText> <maml:uri>https://www.github.com/jborean93/PowerShell-ctypes/blob/main/docs/en-US/New-CtypesLib.md</maml:uri> </maml:navigationLink> </command:relatedLinks> </command:command> <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp"> <command:details> <command:name>New-CtypesStruct</command:name> <command:verb>New</command:verb> <command:noun>CtypesStruct</command:noun> <maml:description> <maml:para>Dynamically define a new struct type in PowerShell.</maml:para> </maml:description> </command:details> <maml:description> <maml:para>Dynamically define a struct ValueType in PowerShell. The struct is defined in a dynamic assembly when created through reflection.</maml:para> </maml:description> <command:syntax> <command:syntaxItem> <maml:name>New-CtypesStruct</maml:name> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="False" position="0" aliases="none"> <maml:name>Name</maml:name> <maml:description> <maml:para>The name of the struct to define.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">String</command:parameterValue> <dev:type> <maml:name>String</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="False" position="1" aliases="none"> <maml:name>Body</maml:name> <maml:description> <maml:para>The struct fields with each line defining the field information.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ScriptBlock</command:parameterValue> <dev:type> <maml:name>ScriptBlock</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>CharSet</maml:name> <maml:description> <maml:para>Set the `CharSet` on the struct, this corresponds to the `[StructLayout(..., CharSet = CharSet.Unicode)]` attribute.</maml:para> </maml:description> <command:parameterValueGroup> <command:parameterValue required="false" command:variableLength="false">None</command:parameterValue> <command:parameterValue required="false" command:variableLength="false">Ansi</command:parameterValue> <command:parameterValue required="false" command:variableLength="false">Unicode</command:parameterValue> <command:parameterValue required="false" command:variableLength="false">Auto</command:parameterValue> </command:parameterValueGroup> <command:parameterValue required="true" variableLength="false">CharSet</command:parameterValue> <dev:type> <maml:name>CharSet</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>LayoutKind</maml:name> <maml:description> <maml:para>Set the `LayoutKind` on the struct, this corresponds to the `[StructLayout(LayoutKind.Sequential)]` attribute and defaults to `Sequential`.</maml:para> </maml:description> <command:parameterValueGroup> <command:parameterValue required="false" command:variableLength="false">Sequential</command:parameterValue> <command:parameterValue required="false" command:variableLength="false">Explicit</command:parameterValue> <command:parameterValue required="false" command:variableLength="false">Auto</command:parameterValue> </command:parameterValueGroup> <command:parameterValue required="true" variableLength="false">LayoutKind</command:parameterValue> <dev:type> <maml:name>LayoutKind</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>Pack</maml:name> <maml:description> <maml:para>Set the `Pack` on the struct, this corresponds to the `[StructLayout(..., Pack = 0)]` attribute.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">Int32</command:parameterValue> <dev:type> <maml:name>Int32</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="proga"> <maml:name>ProgressAction</maml:name> <maml:description> <maml:para>New common parameter introduced in PowerShell 7.4.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ActionPreference</command:parameterValue> <dev:type> <maml:name>ActionPreference</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> </command:syntaxItem> </command:syntax> <command:parameters> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="False" position="1" aliases="none"> <maml:name>Body</maml:name> <maml:description> <maml:para>The struct fields with each line defining the field information.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ScriptBlock</command:parameterValue> <dev:type> <maml:name>ScriptBlock</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>CharSet</maml:name> <maml:description> <maml:para>Set the `CharSet` on the struct, this corresponds to the `[StructLayout(..., CharSet = CharSet.Unicode)]` attribute.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">CharSet</command:parameterValue> <dev:type> <maml:name>CharSet</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>LayoutKind</maml:name> <maml:description> <maml:para>Set the `LayoutKind` on the struct, this corresponds to the `[StructLayout(LayoutKind.Sequential)]` attribute and defaults to `Sequential`.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">LayoutKind</command:parameterValue> <dev:type> <maml:name>LayoutKind</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="true" variableLength="true" globbing="false" pipelineInput="False" position="0" aliases="none"> <maml:name>Name</maml:name> <maml:description> <maml:para>The name of the struct to define.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">String</command:parameterValue> <dev:type> <maml:name>String</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none"> <maml:name>Pack</maml:name> <maml:description> <maml:para>Set the `Pack` on the struct, this corresponds to the `[StructLayout(..., Pack = 0)]` attribute.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">Int32</command:parameterValue> <dev:type> <maml:name>Int32</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> <command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="proga"> <maml:name>ProgressAction</maml:name> <maml:description> <maml:para>New common parameter introduced in PowerShell 7.4.</maml:para> </maml:description> <command:parameterValue required="true" variableLength="false">ActionPreference</command:parameterValue> <dev:type> <maml:name>ActionPreference</maml:name> <maml:uri /> </dev:type> <dev:defaultValue>None</dev:defaultValue> </command:parameter> </command:parameters> <command:inputTypes> <command:inputType> <dev:type> <maml:name>None</maml:name> </dev:type> <maml:description> <maml:para>This cmdlet does not accept pipeline input.</maml:para> </maml:description> </command:inputType> </command:inputTypes> <command:returnValues> <command:returnValue> <dev:type> <maml:name>System.Object</maml:name> </dev:type> <maml:description> <maml:para>This cmdlet does not output any object.</maml:para> </maml:description> </command:returnValue> </command:returnValues> <maml:alertSet> <maml:alert> <maml:para>Defining a struct with the same name as a previous one will overwrite the old type.</maml:para> </maml:alert> </maml:alertSet> <command:examples> <command:example> <maml:title>-------- Example 1 - Create SECURITY_ATTRIBUTES struct --------</maml:title> <dev:code>PS C:\> ctypes_struct SECURITY_ATTRIBUTES { [int]$Length [IntPtr]$SecurityDescriptor [bool]$InheritHandle } PS C:\> $sa = [SECURITY_ATTRIBUTES]::new() PS C:\> $sa.Length = [System.Runtime.InteropServices.Marshal]::SizeOF($sa) PS C:\> $sa.InheritHandle = $true</dev:code> <dev:remarks> <maml:para>Defines the SECURITY_ATTRIBUTES (https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa379560(v=vs.85))struct. Each field is defined on a new line with the type and field name.</maml:para> </dev:remarks> </command:example> <command:example> <maml:title>------- Example 2 - Create struct with marshaling field -------</maml:title> <dev:code>PS C:\> ctypes_struct STARTUPINFOW -CharSet Unicode { [int]$CB [MarshalAs('LPWStr')][string]$Reserved [MarshalAs('LPWStr')][string]$Desktop [MarshalAs('LPWStr')][IntPtr]$Title [int]$X [int]$Y [int]$XSize [int]$YSize [int]$XCountChars [int]$YCountChars [int]$FillAttribute [int]$Flags [short]$ShowWindow [short]$Reserved2 [IntPtr]$Reserved3 [IntPtr]$StdInput [IntPtr]$StdOutput [IntPtr]$StdError }</dev:code> <dev:remarks> <maml:para>Defines the STARTUPINFOW (https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow)struct with the string fields with explicit MarshalAs attributes. The `MarshalAs` attribute accepts a value from UnmanagedType (https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedtype?view=net-7.0)as well as values for `SizeConst` and `ArraySubType`.</maml:para> </dev:remarks> </command:example> <command:example> <maml:title>-------- Example 3 - Create struct with explicit layout --------</maml:title> <dev:code>PS C:\> ctypes_struct SYSTEM_INFO -LayoutKind Explicit { [FieldOffset(0)][int]$OemId [FieldOffset(8)][int]$PageSize [FieldOffset(16)][int]$ActiveProcessorMask [FieldOffset(24)][int]$NumberOfProcessors [FieldOffset(32)][int]$ProcessorType }</dev:code> <dev:remarks> <maml:para>Defines a struct with the `Explicit` layout and field offsets for each field inside the struct.</maml:para> </dev:remarks> </command:example> </command:examples> <command:relatedLinks> <maml:navigationLink> <maml:linkText>Online Version:</maml:linkText> <maml:uri>https://www.github.com/jborean93/PowerShell-ctypes/blob/main/docs/en-US/New-CtypesStruct.md</maml:uri> </maml:navigationLink> </command:relatedLinks> </command:command> </helpItems> |