Assembly: mscorlib (in mscorlib.dll)
A pointer to the IUnknown interface.
Return ValueAn 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.
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 EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.