Export (0) Print
Expand All
Expand Minimize

Marshal.GetObjectForIUnknown Method

Returns an instance of a type that represents a COM object by a pointer to its IUnknown interface.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

public static Object GetObjectForIUnknown (
	IntPtr pUnk
)
public static Object GetObjectForIUnknown (
	IntPtr pUnk
)
public static function GetObjectForIUnknown (
	pUnk : IntPtr
) : Object
Not applicable.

Parameters

pUnk

A pointer to the IUnknown interface.

Return Value

An object representing the specified unmanaged COM object.

This method wraps IUnknown in a managed object. This has the effect of incrementing the reference count of the COM component. The reference count will be decremented when the runtime performs garbage collection on the managed object representing the COM object.

The first parameter, pUnk, represents an IUnknown interface pointer; however, because all COM interfaces derive directly or indirectly from IUnknown, you can pass any COM interface to this method. The object returned by GetObjectForIUnknown is a runtime callable wrapper, which the common language runtime manages as it does any other managed object. The type of this wrapper is often a generic System.__ComObject type, which is a hidden type used when the wrapper type is ambiguous. You can still make late-bound calls to such a generic type as long as the COM object implements the IDispatch interface. Likewise, you can cast the returned object to an appropriate COM interface. For additional information, see Runtime Callable Wrapper.

For an object to be wrapped with a specific managed class type (and not a generic wrapper type), you must adhere to the following requirements:

  • Implement the IProvideClassInfo interface for the COM object.

  • Register the containing assembly with the Assembly Registration Tool (Regasm.exe).

Alternatively, you can avoid these requirements and still get an object that is wrapped with a specific managed class type by using the Marshal.GetTypedObjectForIUnknown method.

NoteNote:

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.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft