Bagikan melalui


SysTime Sample

This sample demonstrates how to pass a class pointer to an unmanaged function that expects a pointer to a structure.

The SysTime sample uses the following unmanaged function, shown with its original function declaration:

  • GetSystemTime exported from Kernel32.dll.

    VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);
    

The original structure passed to the function contains the following elements:

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

In this sample, the SystemTime class contains the elements of the original structure represented as class members. The StructLayoutAttribute attribute is set to ensure that the members are arranged in memory sequentially, in the order in which they appear.

The LibWrap class contains a managed prototype of the GetSystemTime method, which passes the SystemTime class as an In/Out parameter by default. The parameter must be declared with the InAttribute and OutAttribute attributes because classes, which are reference types, are passed as In parameters by default. For the caller to receive the results, these directional attributes must be applied explicitly. The App class creates a new instance of the SystemTime class and accesses its data fields.

The source code for the following code examples is provided by the .NET Framework Platform Invoke Technology Sample.

Code Samples

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

Expected Output:

VB .NET SysTime Sample using Platform Invoke

The Date is: 10 31 2006

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 );
    }
}

Expected Output:

C# SysTime Sample using Platform Invoke

The Date is: 10 31 2006

See Also

Concepts

Marshaling Classes, Structures, and Unions

Platform Invoke Data Types

Creating Prototypes in Managed Code