RuntimeInfrastructure mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.Attribute System.AttributeUsage(System.AttributeTargets.Method, Inherited=false) System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates the use of the .

The non-standard API used in this example indicates the current local system time.

`using System; using System.Runtime.InteropServices; [ StructLayout( LayoutKind.Sequential )] public class SystemTime { public ushort year; public ushort month; public ushort dayOfWeek; public ushort day; public ushort hour; public ushort minute; public ushort second; public ushort milliseconds; } public class LibWrap { [ DllImportAttribute( "Kernel32", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall, EntryPoint="GetLocalTime" )] public static extern void GetLocalTime( SystemTime st ); } public class DllImportAttributeTest { public static void Main() { SystemTime st = new SystemTime(); LibWrap.GetLocalTime( st ); Console.Write( "The Date and Time is: " ); Console.Write( "{0:00}/{1:00}/{2} at ", st.month, st.day, st.year ); Console.WriteLine( "{0:00}:{1:00}:{2:00}", st.hour, st.minute, st.second ); } }`

When run at the given time on the given date, the output produced was

The Date and Time is: 05/16/2001 at 11:39:17

You can apply this attribute to methods.

The attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.

You apply this attribute directly to C# and C++ method definitions; however, the Visual Basic compiler emits this attribute when you use the Declare statement. For complex method definitions that include , , , , , or fields, you apply this attribute directly to Visual Basic method definitions.

Note JScript does not support this attribute. You can use C# or Visual Basic wrapper classes to access unmanaged API methods from JScript programs.

For additional information about using the platform invoke service to access functions in unmanaged DLLs, see [<topic://cpconconsumingunmanageddllfunctions>].

The does not support marshaling of generic types.

Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  If an unmanaged DLL file is included in an assembly, for example, by using the linker or the /linkresource compiler option, you can specify the assembly display name as part of . For example, if an unmanaged DLL named unmanaged.dll is included in a managed assembly named MyAssembly, the attribute might be specified as shown in the following code.

  code reference: System.Runtime.InteropServices.DllImportAttribute#2

  Initializes a new instance of the class with the name of the DLL containing the method to import.

  The name of the DLL that contains the unmanaged method. This can include an assembly display name, if the DLL is included in an assembly.
  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  If true, best-fit mapping behavior is enabled; otherwise, best-fit mapping is disabled. The field is true by default. Settings for this field override the any level settings for the attribute.

  The common language runtime converts to ANSI characters any managed Unicode characters passed to an unmanaged method executing on Windows 98 or Windows Me. Best-fit mapping enables the interop marshaler to provide a close-matching character when no exact match exists. For example, the marshaler converts the Unicode copyright character to 'c' for unmanaged methods that accept ANSI characters. Some characters lack a best-fit representation; these characters are called unmappable. Unmappable characters are usually converted to the default '?' ANSI character.

  Certain Unicode characters are converted to dangerous characters, such as the backslash '\' character, which can inadvertently change a path. By setting the field to true, you can signal the presence of an unmappable character to the caller by throwing an exception.

  You cannot change the default values provided by the and fields when passing a managed array whose elements are ANSI Chars or LPSTRs to an unmanaged safe array. Best-fit mapping is always enabled and no exception is thrown. Be aware that this combination can compromise your security model.

  Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.

- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.InteropServices.CallingConvention

  You set this field to one of the enumeration members. The default value for the field is , which in turn defaults to convention.

  For more information, see Calling Convention in the MSDN library.

  Indicates the calling convention of an entry point.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.InteropServices.CharSet

  Use this field with a member of the enumeration to specify the marshaling behavior of string parameters and to specify which entry-point name to invoke (the exact name given or a name ending with "A" or "W"). The default enumeration member for C# and Visual Basic is CharSet.Ansi and the default enumeration member for C++ is CharSet.None, which is equivalent to CharSet.Ansi. In Visual Basic, you use the Declare statement to specify the CharSet field.

  The field influences the behavior of the CharSet field in determining which entry-point name to invoke. For a detailed description and examples of the string marshaling and name matching behavior associated with the CharSet field, see Specifying a Character Set.

  Indicates how to marshal string parameters to the method and controls name mangling.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String

  You can specify the entry-point name by supplying a string indicating the name of the DLL containing the entry point, or you can identify the entry point by its ordinal. Ordinals are prefixed with the # sign, for example, #1. If you omit this field, the common language runtime uses the name of the.NET method marked with the .

  For additional information, see [<topic://cpconidentifyingfunctionsindlls>]. For examples showing how to use the field, see Specifying an Entry Point.

  Indicates the name or ordinal of the DLL entry point to be called.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  If false, the entry point name appended with the letter A is invoked when the field is set to CharSet.Ansi, and the entry-point name appended with the letter W is invoked when the field is set to the CharSet.Unicode. Typically, managed compilers set this field.

  The following table shows the relationship between the and fields, based on default values imposed by the programming language. You can override the default setting, but do so with caution.

  Language

  ANSI

  Unicode

  Auto

  Visual Basic

  ExactSpelling:=True

  ExactSpelling:=True

  ExactSpelling:=False

  C#

  ExactSpelling=false

  ExactSpelling=false

  ExactSpelling=false

  C++

  ExactSpelling=false

  ExactSpelling=false

  ExactSpelling=false

  Controls whether the field causes the common language runtime to search an unmanaged DLL for entry-point names other than the one specified.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  Set the field to true to directly translate unmanaged signatures with HRESULT or retval values; set it to false to automatically convert HRESULT or retval values to exceptions. By default, the field is true.

  When true, the resulting method signature returns an integer value that contains the HRESULT value. In this case, you must manually inspect the return value and respond accordingly in your application.

  When you set the field to false, the resulting method signature contains a void return type instead of an integer (HRESULT) return type. When the unmanaged method produces an HRESULT, the runtime automatically ignores a return value of S_OK (or 0) and does not throw an exception. For HRESULTs other than S_OK, the runtime automatically throws an exception that corresponds to the HRESULT. Note that the attribute only performs this conversion to methods that return an HRESULT.

  You might decide to change the default error reporting behavior from HRESULTs to exceptions in cases where exceptions better fit the error reporting structure of your application.

  This field is similar to the ; however, in contrast to the field, the default value for the attribute is false.

  In some cases, Visual Basic developers use the , instead of using the Declare statement, to define a DLL function in managed code. Setting the field is one of those cases.

  Indicates whether unmanaged methods that have HRESULT or retval return values are directly translated or whether HRESULT or retval return values are automatically converted to exceptions.

- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  true to indicate that the callee will call SetLastError; otherwise, false. The default is false.

  The runtime marshaler calls GetLastError and caches the value returned to prevent it from being overwritten by other API calls. You can retrieve the error code by calling .

  Indicates whether the callee calls the SetLastError Win32 API function before returning from the attributed method.

- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  true to indicate that an exception is thrown each time the interop marshaler converts an unmappable character; false to indicate that the field is disabled. This field is false by default.

  The common language runtime converts to ANSI characters any managed Unicode characters passed to an unmanaged method executing on Windows 98 or Windows Me. Best-fit mapping enables the interop marshaler to provide a close-matching character when no exact match exists. For example, the marshaler converts the Unicode copyright character to 'c' for unmanaged methods that accept ANSI characters. Some characters lack a best-fit representation; these characters are called unmappable. Unmappable characters are usually converted to the default '?' ANSI character.

  Certain Unicode characters are converted to dangerous characters, such as the backslash '\' character, which can inadvertently change a path. By setting the field to true, you can signal the presence of an unmappable character to the caller by throwing an exception.

  You cannot change the default values provided by the and fields when passing a managed array whose elements are ANSI Chars or LPSTRs to an unmanaged safe array. Best-fit mapping is always enabled and no exception is thrown. Be aware that this combination can compromise your security model.

  Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.

- Property
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.String

  A containing the name of the shared library file from which a function implementation is imported.

  You can provide a full or relative path. If you provide no path, the DLL must be in the current path at run time, unless the DLL is loaded by some other means. Be aware, however, that using a fully qualified path can introduce inaccuracy if the DLL is moved.

  Gets the name of the DLL file that contains the entry point.

  0
0