This documentation is archived and is not being maintained.

Marshal.GetIDispatchForObject Method

Returns an IDispatch interface from a managed object.

[Visual Basic]
Public Shared Function GetIDispatchForObject( _
   ByVal o As Object _
) As IntPtr
public static IntPtr GetIDispatchForObject(
 object o
public: static IntPtr GetIDispatchForObject(
 Object* o
public static function GetIDispatchForObject(
   o : Object
) : IntPtr;


The object whose IDispatch interface is requested.

Return Value

The IDispatch pointer for the o parameter.


Exception Type Condition
InvalidCastException o does not support the requested interface.


In managed code, you seldom work directly with the IDispatch interface. However, GetIDispatchForObject is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. Calling an object with this method causes the reference count to increment on the interface pointer before the pointer is returned. Always use Marshal.Release to decrement the reference count once you have finished with the pointer.

You can also use this method on a managed object to obtain an interface pointer to the COM callable wrapper for the object. For additional information, see COM Callable Wrapper.

Note   This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace | Release