Search notes:

System.Runtime.InteropServices.Marshal (class)

The System.Runtime.InteropServices.Marshal class allows to interact with managed memory and convert between unmanaged and managed memory etc.

Methods

Memory allocation and releasing

The 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

The 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

The 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 / WriteXXX

The 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

SizeOf

SizeOf comes in four overloads. This method returns the size of the (unmanaged) bytes that a managed byte in fact requires to store its data:
  • SizeOf(Object)
  • SizeOf(Type)
  • SizeOf<T>()
  • SizeOf<T>(T)

GetActiveObject

The 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.
The following PowerShell example makes sure that the script does not start an additional Excel instance.
$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
}
The VBA equivalent of GetActiveObject() seems to be getObject()
In .NET Core , there is no GetActiveObject() method anymore, so this functionality must be accessed with P/Invoke.

COM related methods

COM related COM methods include:
  • AddRef and its counterpart Release
  • GetComInterfaceForObject (returns IntPtr)
  • GetIUnknownForObject (returns IntPtr)
  • GetIDispatchForObject (returns IntPtr)
  • QueryInterface
  • GetEndComSlot
  • IsComObject
  • GetObjectForIUnknown

Other methods

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()

Properties

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

See also

This example shows how the static method SecureStringToBSTR can be used to get the value of a secure string.

Index