-
Notifications
You must be signed in to change notification settings - Fork 1
/
SafeNativeMethods.cs
186 lines (143 loc) · 6.02 KB
/
SafeNativeMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Runtime.InteropServices;
namespace Magic.Samples.DisplaySettings
{
static class SafeNativeMethods
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DEVMODE
{
// You can define the following constant
// but OUTSIDE the structure because you know
// that size and layout of the structure is very important
// CCHDEVICENAME = 32 = 0x50
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
// In addition you can define the last character array
// as following:
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
//public Char[] dmDeviceName;
// After the 32-bytes array
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSpecVersion;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverVersion;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSize;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverExtra;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmFields;
public POINTL dmPosition;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayOrientation;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFixedOutput;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmColor;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmDuplex;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmYResolution;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmTTOption;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmCollate;
// CCHDEVICENAME = 32 = 0x50
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
// Also can be defined as
//[MarshalAs(UnmanagedType.ByValArray,
// SizeConst = 32, ArraySubType = UnmanagedType.U1)]
//public Byte[] dmFormName;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmLogPixels;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmBitsPerPel;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsWidth;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsHeight;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFlags;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFrequency;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMMethod;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMIntent;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmMediaType;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDitherType;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved1;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved2;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningWidth;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningHeight;
/// <summary>
/// Initializes the structure variables.
/// </summary>
public void Initialize()
{
this.dmDeviceName = new string(new char[32]);
this.dmFormName = new string(new char[32]);
this.dmSize = (ushort)Marshal.SizeOf(this);
}
}
// 8-bytes structure
[StructLayout(LayoutKind.Sequential)]
public struct POINTL
{
public Int32 x;
public Int32 y;
}
[DllImport("User32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern Boolean EnumDisplaySettings(
[param: MarshalAs(UnmanagedType.LPTStr)]
String lpszDeviceName, // display device
[param: MarshalAs(UnmanagedType.U4)]
Int32 iModeNum, // graphics mode
[In, Out]
ref DEVMODE lpDevMode // graphics mode settings
);
public const int ENUM_CURRENT_SETTINGS = -1;
public const int DMDO_DEFAULT = 0;
public const int DMDO_90 = 1;
public const int DMDO_180 = 2;
public const int DMDO_270 = 3;
[DllImport("User32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int ChangeDisplaySettings(
[In, Out]
ref DEVMODE lpDevMode,
[param: MarshalAs(UnmanagedType.U4)]
uint dwflags);
[DllImport("kernel32.dll", SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern uint FormatMessage(
[param: MarshalAs(UnmanagedType.U4)]
uint dwFlags,
[param: MarshalAs(UnmanagedType.U4)]
uint lpSource,
[param: MarshalAs(UnmanagedType.U4)]
uint dwMessageId,
[param: MarshalAs(UnmanagedType.U4)]
uint dwLanguageId,
[param: MarshalAs(UnmanagedType.LPTStr)]
out string lpBuffer,
[param: MarshalAs(UnmanagedType.U4)]
uint nSize,
[param: MarshalAs(UnmanagedType.U4)]
uint Arguments);
public const uint FORMAT_MESSAGE_FROM_HMODULE = 0x800;
public const uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100;
public const uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x200;
public const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
public const uint FORMAT_MESSAGE_FLAGS = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;
}
}