Description





Description

This attribute provides the information needed to call a method exported from an unmanaged shared library. This attribute provides the name of the shared library file, the name of the method within that library, the calling convention, and character set of the unmanaged function.

[Note: A shared library refers to Dynamically Linked Libraries on Windows systems, and Shared Libraries on UNIX systems.]

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

Example

Figure

[Note: The non-standard GetLocalTime 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: 08/21/2004 at 12:59:08


Figure


using System;

using System.IO;

using System.Reflection;

using System.Runtime.InteropServices;



/// <summary>

/// Sample demonstrating the use of the DllImportAttribute class.

/// Use this attribute to define unmanaged method signatures that can be called

/// from managed code.

/// </summary>

internal class DllImportAttributeSample

{



    private static void Main()

    {

        bool     fullySigned   = false;

        bool     verified      = false;

        string[] assemblyPaths = new string[2];



        assemblyPaths[0] = Assembly.GetExecutingAssembly().Location;

        assemblyPaths[1] =

            Path.Combine(

                RuntimeEnvironment.GetRuntimeDirectory(),

                "System.Windows.Forms.dll");

        foreach (string assemblyPath in assemblyPaths)

        {

            // There is no managed API for determining if an assembly's has a

            // valid strong name, although an unmanaged method,

            // StrongNameSignatureVerificationEx, is available and can be

            // called from managed code.

            Console.WriteLine("Verifying {0}...", assemblyPath);

            verified =

                StrongNameSignatureVerificationEx(

                    assemblyPath, false, ref fullySigned);



            if (verified && fullySigned)

            {

                Console.WriteLine("  Verified (signed).");

            }

            else if (verified)

            {

                Console.WriteLine(

                    "  Verified (delay signed, verification skipped).");

            }

            else

            {

                Console.WriteLine(

                    "  Not verified (not signed or signed and tampered with).");

            }



            Console.WriteLine();

        }



        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }



    // Unmanaged method for determining if an assembly, identified by a file

    // path, has a valid strong name.

    [DllImport("mscoree.dll", CharSet=CharSet.Unicode)]

    private static extern bool StrongNameSignatureVerificationEx(

        string wszFilePath, bool fForceVerification, ref bool pfWasVerified);



}


The output is

Verifying C:\System.Runtime\InteropServices\DllImportAttribute\DllImportAttribute.exe...

  Not verified (not signed or signed and tampered with).



Verifying C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll...

  Verified (signed).







Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows