Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Marshal.CreateWrapperOfType Method

Wraps the specified COM object in an object of the specified type.

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

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static Object CreateWrapperOfType(
	Object o,
	Type t
)

Parameters

o
Type: System.Object

The object to be wrapped.

t
Type: System.Type

The Type of wrapper to create.

Return Value

Type: System.Object
The newly wrapped object that is an instance of the desired type.

ExceptionCondition
ArgumentException

t must derive from __ComObject.

ArgumentNullException

The t parameter is null.

InvalidCastException

o cannot be converted to the destination type since it does not support all required interfaces.

CreateWrapperOfType converts one COM class type, typically the generic __ComObject type, to another COM class type. The input COM object, represented by parameter o, is a runtime callable wrapper. If you are unfamiliar with this wrapper, see Runtime Callable Wrapper.

Both the t and o parameters must be classes whose signatures are attributed with System.Runtime.InteropServices.ComImportAttribute. The Type Library Importer (Tlbimp.exe) applies this attribute for you when it imports a type library. If you create the runtime callable wrapper manually in source code, you should apply this attribute to the managed signature representing the original coclass to signify its COM origins.

Tlbimp.exe imports a COM coclass as a managed class and an interface. The coclass interface has the same name as the original coclass and the managed class has the original coclass name appended with Class. For example, a coclass called MyCoclass becomes a coclass interface called MyCoclass and a managed class called MyCoclassClass. Since t must be a class, not an interface, be sure to specify the managed class (MyCoclassClass) and not the coclass interface.

NoteNote:

You lose the identity of the input COM object because a new runtime callable wrapper instance wraps the IUnknown pointer exposed by the original runtime callable wrapper.

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 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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft