logo search
CSharp_Prog_Guide

Объявление и передача структур

В следующем примере показано определение структур Point и Rect в управляемом коде, и передача типов в качестве параметра функции PtInRect в файле библиотеки User32.dll. Для функции PtInRect используется следующая неуправляемая подпись:

BOOL PtInRect(const RECT *lprc, POINT pt);

Обратите внимание, что структуру Rect следует передавать по ссылке, так как функция должна получить указатель на тип RECT.

------

Delcaring and Passing Classes

You can pass members of a class to an unmanaged DLL function, as long as the class has a fixed member layout. The following example demonstrates how to pass members of the MySystemTime class, which are defined in sequential order, to the GetSystemTime in the User32.dll file. GetSystemTime has the following unmanaged signature:

void GetSystemTime(SYSTEMTIME* SystemTime);

Unlike value types, classes always have at least one level of indirection.

[StructLayout(LayoutKind.Sequential)]

public class MySystemTime {

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

class Win32API {

[DllImport("Kernel32.dll")]

public static extern void GetSystemTime(MySystemTime st);

}