System.Runtime.InteropServices.DllImportAttribute Class

Assembly: Mscorlib.dll
Namespace: System.Runtime.InteropServices
Summary
Indicates that the attributed method is implemented as an export from an unmanaged DLL.
C# Syntax:
[AttributeUsage(AttributeTargets.Method)]
public sealed class DllImportAttribute : Attribute
Remarks
You can apply this attribute to methods.

This attribute provides the information needed to call an exported function in an unmanaged DLL.

Example
 [DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
 CharSet=CharSet.Unicode, ExactSpelling=true,
 CallingConvention=CallingConvention.StdCall)]
 public static extern bool MoveFile(String src, String dst);

    
See also:
System.Runtime.InteropServices Namespace

System.Runtime.InteropServices.DllImportAttribute Member List:

Public Constructors
ctor #1 Initializes a new instance of the DllImportAttribute class with the name of the DLL containing the method to import.
Public Fields
CallingConvention Indicates the CallingConvention value used when passing method arguments to the unmanaged implementation.
CharSet Controls name mangling and indicates how to marshal String arguments to the method.
EntryPoint Indicates the name or ordinal of the DLL entry point to be called.
ExactSpelling Indicates whether the name of the entry point in the unmanaged DLL should be modified to correspond to the CharSet value specified in the DllImportAttribute.CharSet field.
PreserveSig Indicates the managed method signature should not be transformed into an unmanaged signature that returns an HRESULT, and may have an additional [out, retval] argument for the return value.
SetLastError Indicates that the callee will call the Win32 API SetLastError before returning from the attributed method.
Public Properties
TypeId
(inherited from System.Attribute)
Read-only

See base class member description: System.Attribute.TypeId


When implemented in a derived class, gets a unique identifier for this Attribute.
Value Read-only

Gets the name of the DLL file with the entry point.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Attribute)
See base class member description: System.Attribute.GetHashCode


Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IsDefaultAttribute
(inherited from System.Attribute)
See base class member description: System.Attribute.IsDefaultAttribute


When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
Match
(inherited from System.Attribute)
See base class member description: System.Attribute.Match


When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Runtime.InteropServices.DllImportAttribute Member Details

ctor #1
Summary
Initializes a new instance of the DllImportAttribute class with the name of the DLL containing the method to import.
C# Syntax:
public DllImportAttribute(
   string dllName
);
Parameters:

dllName

The name of the DLL containing the unmanaged method.

Return to top


Field: CallingConvention
Summary
Indicates the CallingConvention value used when passing method arguments to the unmanaged implementation.
C# Syntax:
public CallingConvention CallingConvention;
Remarks
This field is set to one of the CallingConvention values. The default CallingConvention value is CallingConvention.StdCall.

See Calling Conventions in the MSDN library for details on each.

Return to top


Field: CharSet
Summary
Controls name mangling and indicates how to marshal String arguments to the method.
C# Syntax:
public CharSet CharSet;
Remarks
This field is set to one of the CharSet values. If the CharSet field is set to CharSet.Unicode, all string arguments are converted to Unicode characters before being passed to the unmanaged implementation. This also causes the letter 'W' to be appended to the name of the DLL DllImportAttribute.EntryPoint. If the field is set to CharSet.Ansi the strings are converted to ANSI strings, and the letter 'A' is appended to the name of the DLL DllImportAttribute.EntryPoint. This convention of appending 'W' or 'A' is used by most Win32 API's. If CharSet is set to CharSet.Auto, the conversion is platform dependent (Unicode on Windows NT and Ansi on Windows 98). The default value for CharSet is CharSet.Ansi. The CharSet field is also used to determine which version of a function will be imported from the specified DLL. The name matching rules for CharSet.Ansi and CharSet.Unicode are quite different. With Ansi, setting DllImportAttribute.EntryPoint to "MyMethod" returns "MyMethod" if it exists. If "MyMethod" is absent from the DLL, but "MyMethodA" is present, "MyMethodA" is returned. The opposite is true for Unicode. Setting DllImportAttribute.EntryPoint to "MyMethod" returns "MyMethodW" if it exists. If "MyMethodW" is absent from the DLL, but "MyMethod" is present, "MyMethod" is returned. When Auto is used, the matching rules are platform dependent (Unicode on Windows NT and Ansi on Windows 98). If DllImportAttribute.ExactSpelling is set to true, "MyMethod" is returned only if "MyMethod" is present in the DLL.
See also:
CharSet

Return to top


Field: EntryPoint
Summary
Indicates the name or ordinal of the DLL entry point to be called.
C# Syntax:
public string EntryPoint;
Remarks
Ordinals are prefixed with the # sign, for example, #1.

Return to top


Field: ExactSpelling
Summary
Indicates whether the name of the entry point in the unmanaged DLL should be modified to correspond to the CharSet value specified in the DllImportAttribute.CharSet field.
C# Syntax:
public bool ExactSpelling;
Remarks
If true, the letter A is appended to the method name when the DllImportAttribute.CharSet field is set to the CharSet value CharSet.Ansi, and the letter W is appended to the method name when the DllImportAttribute.CharSet field is set to the CharSet value CharSet.Unicode. The default is value for this field is false.

Return to top


Field: PreserveSig
Summary
Indicates the managed method signature should not be transformed into an unmanaged signature that returns an HRESULT, and may have an additional [out, retval] argument for the return value.
C# Syntax:
public bool PreserveSig;
Remarks
Most methods being called through PInvoke do not return HRESULT's so it does not make sense to do the HRESULT/[out, retval] conversion. This is why the default is to preserve the signature exactly as defined. Occasionally you do call a method through PInvoke that returns an HRESULT, so convert the signature when it makes sense. The method has to return an HRESULT to do the conversion.

The definition of a method such as "HRESULT CoCreateInstance(...)" would set this to false. The default is true.

See the COM interop specification for details.

Return to top


Field: SetLastError
Summary
Indicates that the callee will call the Win32 API SetLastError before returning from the attributed method.
C# Syntax:
public bool SetLastError;
Remarks
true indicates the callee will call SetLastError, the default is false.

The runtime marshaler will call GetLastError and cache the value returned to prevent it from being overwritten by other API calls. Users can retrieve the error code by calling Marshal.GetLastWin32Error.

Return to top


Property: TypeId (read-only)
Inherited
See base class member description: System.Attribute.TypeId

Summary
When implemented in a derived class, gets a unique identifier for this Attribute.
C# Syntax:
public virtual object TypeId {get;}
Remarks
As implemented, this identifier is merely the Type of the attribute. However, it is intended that the unique identifier be used to identify two attributes of the same type.

Return to top


Property: Value (read-only)
Summary
Gets the name of the DLL file with the entry point.
C# Syntax:
public string Value {get;}

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DllImportAttribute();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Attribute.GetHashCode

Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A 32-bit signed integer hash code.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IsDefaultAttribute()
Inherited
See base class member description: System.Attribute.IsDefaultAttribute

Summary
When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class.
C# Syntax:
public virtual bool IsDefaultAttribute();
Return Value:
true if this instance is the default attribute for the class; otherwise, false.
Remarks
The default implementation of this class returns false, and must be implemented in the derived class to be useful to that class.

The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.

Return to top


Method: Match(
   object obj
)
Inherited
See base class member description: System.Attribute.Match

Summary
When overridden in a derived class, returns a value indicating whether this instance equals a specified object.
C# Syntax:
public virtual bool Match(
   object obj
);
Parameters:

obj

An Object to compare with this instance of Attribute.

Return Value:
true if this instance equals obj; otherwise, false.
Remarks
This method determines if one Attribute equals another. Its default implementation is the same as Attribute.Equals, which performs a value and reference comparison. Override this method to implement support for attribute values, such as flags or bitfields, that consist of components that are meaningful in themselves. For example, consider an attribute whose value is a binary field divided into a bitfield of flags. Two instances of this attribute have one flag in set in common while all the other flags differ. The Equal method cannot determine that the two instances have the same flag set, but the Match method can.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.