Condividi tramite


Esempio di SysTime

In questo esempio viene dimostrato come passare un puntatore a una classe a una funzione non gestita per la quale è previsto un puntatore a una struttura.

Nell'esempio di SysTime viene utilizzata la seguente funzione non gestita, illustrata con la dichiarazione di funzione originale:

  • GetSystemTime esportata da Kernel32.dll.

    VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
    

La struttura originale passata alla funzione contiene i seguenti elementi:

typedef struct _SYSTEMTIME { 
    WORD wYear; 
    WORD wMonth; 
    WORD wDayOfWeek; 
    WORD wDay; 
    WORD wHour; 
    WORD wMinute; 
    WORD wSecond; 
    WORD wMilliseconds; 
} SYSTEMTIME, *PSYSTEMTIME;

In questo esempio, la classe SystemTime contiene gli elementi della struttura originale rappresentati come membri di classe. L'attributo StructLayoutAttribute è impostato in modo da garantire che i membri vengano inseriti in memoria in sequenza, nell'ordine in cui appaiono.

La classe LibWrap contiene un prototipo gestito del metodo GetSystemTime, che passa la classe SystemTime come parametro in/out per impostazione predefinita. È necessario dichiarare il parametro con gli attributi InAttribute e OutAttribute poiché le classi, che sono tipi di riferimento, vengono passate come parametri in per impostazione predefinita. Affinché il chiamante riceva i risultati, è necessario applicare questi attributi direzionali in modo esplicito. Con la classe App si crea una nuova istanza della classe SystemTime e si accede ai relativi campi di dati.

Esempi di codice

Imports System
Imports System.Runtime.InteropServices     ' For StructLayout,
                                           '  and DllImport


' Declares a class member for each structure element.
<StructLayout(LayoutKind.Sequential)> _
Public Class SystemTime
    Public year As Short
    Public month As Short
    Public weekday As Short
    Public day As Short
    Public hour As Short
    Public minute As Short
    Public second As Short
    Public millisecond As Short
End Class 'SystemTime

Public Class LibWrap
    ' Declares a managed prototype for the unmanaged function.
    Declare Sub GetSystemTime Lib "Kernel32.dll" _
        (<[In](), Out()> ByVal st As SystemTime)
End Class 'LibWrap

Public Class App
    Public Shared Sub Main()
        Console.WriteLine("VB .NET SysTime Sample using Platform Invoke")
        Dim st As New SystemTime()
        LibWrap.GetSystemTime(st)
        Console.Write("The Date is: {0} {1} {2}", st.month, st.day, st.year)
    End Sub 'Main
End Class 'App

' The program produces output similar to the following:
'
' VB .NET SysTime Sample using Platform Invoke
' The Date is: 3 21 2010
using System;
using System.Runtime.InteropServices;     // For StructLayout, DllImport

[StructLayout(LayoutKind.Sequential)]
public class SystemTime
{
    public ushort year;
    public ushort month;
    public ushort weekday;
    public ushort day;
    public ushort hour;
    public ushort minute;
    public ushort second;
    public ushort millisecond;
}

public class LibWrap
{
    // Declares a managed prototype for the unmanaged function using Platform Invoke.
    [DllImport("Kernel32.dll")]
    public static extern void GetSystemTime([In,Out] SystemTime st);
}

public class App
{
    public static void Main()
    {
        Console.WriteLine("C# SysTime Sample using Platform Invoke");
        SystemTime st = new SystemTime();
        LibWrap.GetSystemTime(st);
        Console.Write("The Date is: ");
        Console.Write("{0} {1} {2}",  st.month, st.day, st.year );
    }
}

// The program produces output similar to the following:
//
// C# SysTime Sample using Platform Invoke
// The Date is: 3 21 2010
using namespace System;
using namespace System::Runtime::InteropServices;     // For StructLayout, DllImport

[StructLayout(LayoutKind::Sequential)]
public ref class SystemTime
{
public:
    unsigned short year;
    unsigned short month;
    unsigned short weekday;
    unsigned short day;
    unsigned short hour;
    unsigned short minute;
    unsigned short second;
    unsigned short millisecond;
};

public class LibWrap
{
public:
    // Declares a managed prototype for the unmanaged function using Platform Invoke.
    [DllImport("Kernel32.dll")]
    static void GetSystemTime([In,Out] SystemTime^ st);
};

public class App
{
public:
    static void Main()
    {
        Console::WriteLine("C++/CLI SysTime Sample using Platform Invoke");
        SystemTime^ st = gcnew SystemTime();
        LibWrap::GetSystemTime(st);
        Console::Write("The Date is: ");
        Console::Write("{0} {1} {2}",  st->month, st->day, st->year);
    }
};

int main()
{
    App::Main();
}
// The program produces output similar to the following:
//
// C++/CLI SysTime Sample using Platform Invoke
// The Date is: 3 21 2010

Vedere anche

Concetti

Marshalling di classi, strutture e unioni

Tipi di dati di platform invoke

Creazione di prototipi nel codice gestito