Chapter 13: Advanced Interaction

team lib

Chapter 12 showed you how to use Platform Invoke to call unmanaged functions defined in dynamic-link libraries (DLLs). For simple functions, the procedures outlined in the previous chapter will work very well, but you will come across cases that demand more advanced techniques.

This chapter will show you how to use unmanaged functions that take string arguments or array arguments, or require pointers to be passed. It will also show you how to pass pointers to managed objects into unmanaged code, and how to specify the path to a Platform Invoke DLL dynamically.

The MarshalAs Attribute

You use this attribute to specify how data should be marshaled between managed and unmanaged code when you need to override the defaults. When you're passing a string to a COM method, the default conversion is to a COM BSTR; when you're passing a string to a non-COM method using Platform Invoke, the default conversion is to a C-style LPSTR . For example, if you want to pass a C-style null- terminated string to a COM method, you will need to use MarshalAs to override the default conversion.

You can attach the MarshalAs attribute to method parameters and return values, and you always need to specify the type that is to be used for marshaling. The type to be used is represented by a member of the UnmanagedType enumeration, whose possible values are shown in Table 13-1.


The MarshalAs attribute is used in two different scenarios: interacting with COM components using COM interop, and calling unmanaged functions using Platform Invoke. Some of the members listed in the following table will be relevant only to one or other of these scenarios.

Table 13-1: The Members of the UnmanagedType Enumeration




Used to marshal strings as ANSI BSTR s.


Determines the type of an object at run time, and marshals the object as the appropriate type.


Represents a 4-byte Boolean type, equivalent to the Microsoft Windows API BOOL type.


Used to marshal strings as Unicode BSTR s. This is the default string marshaling type for string arguments in COM methods .


Marshals an array argument by value. See the 'Marshaling Arrays' section that follows for more details.


Marshals fixed-length character arrays that appear in structures.


Used to marshal a System.Decimal type as a COM currency type instead of as a Decimal .


Used to specify a custom marshaler class that will be used to marshal a parameter.


Can be applied to integer types to cause them to be represented as an HRESULT in an emitted type library.


Represents an integer that can be used as a function pointer. You can use this on a delegate type.


A one-byte integer. This can be applied to managed Boolean arguments to convert them to a one-byte C-style boolean.

I2 , I4 , I8

Two-, four-, and eight-byte signed integers.


Represents a COM IDispatch pointer.


Represents a COM interface pointer.


Represents a COM IUnknown pointer.


A pointer to the first element in a C-style array. See the 'Marshaling Arrays"' section that follows for more details.


Marshals string data as a null-terminated ANSI string.


Represents a pointer to a formatted structure. Used only with Platform Invoke. See the 'Marshaling Structs' section that follows for more details.


Represents a platform-dependent string, which will be ANSI on Windows 98 and Windows ME, and Unicode on Windows NT, Windows 2000, and Windows XP. This value is used only with Platform Invoke.


Marshals string data as a null-terminated Unicode string.

R4 , R8

Represents 4- and 8-byte floating-point values.


Represents a SAFEARRAY structure. See the 'Marshaling Arrays' section that follows for more details.


Represents a VARIANT .

SysInt , SysUInt

Signed and unsigned integers with platform-dependent size . These will be 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.


A length-prefixed, platform-dependent string. This is rarely used.

U1 , U2 , U4 , U8

Represents 1-, 2-, 4-, and 8-byte unsigned integer values.


Represents the OLE VARIANT_BOOL type used to represent Boolean values in older COM methods.


Represents a string passed by value so that Microsoft Visual Basic .NET can change a string in managed code and have the change propagated back to unmanaged code.

The MarshalAs attribute can be applied to a parameter like this:


The attribute instructs the interop marshaler to marshal the string object as an ANSI null-terminated string.

team lib

COM Programming with Microsoft .NET
COM Programming with Microsoft .NET
ISBN: 0735618755
EAN: 2147483647
Year: 2006
Pages: 140

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: