Returns an instance of a type that represents a COM object by a pointer to its IUnknown interface.
Assembly: mscorlib (in mscorlib.dll)
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] public static Object GetObjectForIUnknown( IntPtr pUnk )
- Type: System.IntPtr
A pointer to the IUnknown interface.
Return ValueType: System.Object
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 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.
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC