This topic has not yet been rated - Rate this topic

Marshal.QueryInterface Method

Requests a pointer to a specified interface from a COM object.

[Visual Basic]
Public Shared Function QueryInterface( _
   ByVal pUnk As IntPtr, _
   ByRef iid As Guid, _
   <Out()> ByRef ppv As IntPtr _
) As Integer
[C#]
public static int QueryInterface(
 IntPtr pUnk,
   ref Guid iid,
   out IntPtr ppv
);
[C++]
public: static int QueryInterface(
 IntPtr pUnk,
 Guid* iid,
   [
   Out
] IntPtr* ppv
);
[JScript]
public static function QueryInterface(
   pUnk : IntPtr,
 iid : Guid,
 ppv : IntPtr
) : int;

Parameters

pUnk
The interface to be queried.
iid
A Guid, passed by reference, that is the interface identifier (IID) of the requested interface.
ppv
When this method returns, contains a reference to the returned interface.

Return Value

The returned interface.

Remarks

The QueryInterface method exposes the IUnknown::QueryInterface of a COM object, which attempts to obtain a specific interface pointer. Using QueryInterface on a COM object is the same as performing a cast operation in managed code. 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. To obtain an IntPtr value that represents a IUnknown interface pointer, you can call Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, or Marshal.GetIDispatchForObject.

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.

Requirements

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 | AddRef | Release | GetComInterfaceForObject | GetIUnknownForObject | GetIDispatchForObject | GetObjectForIUnknown

Did you find this helpful?
(1500 characters remaining)