logo search
CSharp_Prog_Guide

Основы описания

Как показано в следующих примерах, управляемые описания неуправляемых функций зависят от используемого языка.

Adjusting the Definition

Whether you set them explicitly or not, attribute fields are at work defining the behavior of managed code. Platform invoke operates according to the default values set on various fields that exist as metadata in an assembly. You can alter this default behavior by adjusting the values of one or more fields. In many cases, you use the DllImportAttribute to set a value.

The following table lists the complete set of attribute fields that pertain to platform invoke. For each field, the table includes the default value and a link to information on how to use these fields to define unmanaged DLL functions.

Field

Description

BestFitMapping

Enables or disables best-fit mapping.

CallingConvention

Specifies the calling convention to use in passing method arguments. The default is WinAPI, which corresponds to __stdcall for the 32-bit Intel-based platforms.

CharSet

Controls name mangling and the way that string arguments should be marshaled to the function. The default is CharSet.Ansi.

EntryPoint

Specifies the DLL entry point to be called.

ExactSpelling

Controls whether an entry point should be modified to correspond to the character set. The default value varies by programming language.

PreserveSig

Controls whether the managed method signature should be transformed into an unmanaged signature that returns an HRESULT and has an additional [out, retval] argument for the return value.

The default is true (the signature should not be transformed).

SetLastError

Enables the caller to use the Marshal.GetLastWin32Error API function to determine whether an error occurred while executing the method. In Visual Basic, the default is true; in C# and C++, the default is false.

ThrowOnUnmappableChar

Controls throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.