System.Runtime.InteropServices.Marshal
class allows to interact with managed memory and convert between unmanaged and managed memory etc. Marshal
class provides two (direct) memory allocation methods: AllocCoTaskMem
(The «COM task memory allocator»)
AllocHGlobal
(which allocates unmanaged memory and exposes the WinAPI function LocalAlloc
from kernel32.dll
)
FreeHGlobal
(which frees memory that was allocated with AllocHGlobal
) PtrToXXX
methods allocate managed memory and fills it with the data that a (unmaanged) pointer points at: PtrToStringAnsi
PtrToStringAuto
PtrToStringBSTR
PtrToStringUni
PtrToStringUTF8
PtrToStructure
(Compare with StructureToPtr
)
StringToXXX
allocate different «kinds» of unmanaged memory and copies the managed string data to that newly allocated memory: StringToBSTR
StringToCoTaskMemAnsi
StringToCoTaskMemAuto
StringToCoTaskMemUni
StringToCoTaskMemUTF8
StringToHGlobalAnsi
StringToHGlobalAuto
StringToHGlobalUni
ReadXXX
and WriteXXXX
methods read 1, 2, 4 or 8 bytes and return a corresponding System.Byte
short
(= System.Int16
?), System.Int32
etc: ReadByte
/ WriteByte
ReadInt16
/ WriteInt16
ReadInt32
/ WriteByte32
ReadInt64
/ WriteByte64
ReadIntPtr
/ WriteIntPtr64
GetActiveObject(progid)
method tries to locate an object with the given progid in the Running Object Table and returns the corresponding COM Object if it was found. If such an object is not found, a System.Runtime.InteropServices.COMException
is thrown. $progId = 'Excel.Application'; try { $comObj = [System.Runtime.InteropServices.Marshal]::GetActiveObject($progId); write-output "$progId was found in the RunningObject Table, activating it"; # # https://stackoverflow.com/a/36717974/180275 # $comObj.windowState = -4140 # xlMinimized $comObj.windowState = -4137 # xlMaximized } catch [System.Runtime.InteropServices.COMException] { write-output "$progId was not found in the Running Object Table, starting it."; $comObj = new-object -comObject $progId $comObj.visible = $true }
GetActiveObject()
seems to be getObject()
AddRef
and its counterpart Release
GetComInterfaceForObject
(returns IntPtr
)
GetIUnknownForObject
(returns IntPtr
)
GetIDispatchForObject
(returns IntPtr
)
QueryInterface
GetEndComSlot
IsComObject
GetObjectForIUnknown
AreComObjectsAvailableForCleanup() | |
BindToMoniker() | |
ChangeWrapperHandleStrength() | |
CleanupUnusedObjectsInCurrentContext() | |
Copy() | |
CreateAggregatedObject() | |
CreateWrapperOfType() | |
DestroyStructure() | |
FinalReleaseComObject() | |
FreeBSTR() | |
FreeCoTaskMem() | |
GenerateGuidForType() | |
GenerateProgIdForType() | |
GetComObjectData() | |
GetDelegateForFunctionPointer() and GetFunctionPointerForDelegate() | converts an unmanaged function pointer to a System.Delegate and vice versa |
GetExceptionCode() | |
GetExceptionForHR() | |
GetExceptionPointers() | |
GetHINSTANCE() | |
GetHRForException() | |
GetHRForLastWin32Error() | |
GetLastPInvokeError() | |
GetLastSystemError() | |
GetLastWin32Error() | |
GetNativeVariantForObject() | |
GetObjectForNativeVariant() | |
GetObjectsForNativeVariants() | |
GetStartComSlot() | |
GetTypedObjectForIUnknown() | |
GetTypeFromCLSID() | Compare with the method with the same name in System.Type |
GetTypeInfoName() | |
GetUniqueObjectForIUnknown() | |
InitHandle() | |
IsTypeVisibleFromCom() | |
OffsetOf() | |
Prelink() | |
PrelinkAll() | |
ReAllocCoTaskMem() | |
ReAllocHGlobal() | |
ReleaseComObject() | |
SecureStringToBSTR() | |
SecureStringToCoTaskMemAnsi() | |
SecureStringToCoTaskMemUnicode() | |
SecureStringToGlobalAllocAnsi() | |
SecureStringToGlobalAllocUnicode() | |
SetComObjectData() | |
SetLastPInvokeError() | |
SetLastSystemError() | |
StructureToPtr() | |
ThrowExceptionForHR() | |
UnsafeAddrOfPinnedArrayElement() | |
ZeroFreeBSTR() | |
ZeroFreeCoTaskMemAnsi() | |
ZeroFreeCoTaskMemUnicode() | |
ZeroFreeCoTaskMemUTF8() | |
ZeroFreeGlobalAllocAnsi() | |
ZeroFreeGlobalAllocUnicode() |
SystemDefaultCharSize
and SystemMaxDBCSCharSize
correspond to the size of a System.Char
and the maximum size of the double byte character set (DBCS) in the current system SecureStringToBSTR
can be used to get the value of a secure string.