-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNativeMethods.cs
192 lines (139 loc) · 8.46 KB
/
NativeMethods.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
187
188
189
190
191
192
using System;
using System.Runtime.InteropServices;
using System.Text;
// Disable IDE warnings that showed up after going from C# 7 to C# 9
#pragma warning disable IDE0079 // Disable message about unnecessary suppression
#pragma warning disable IDE1006 // Disable messages about capitalization of control names
#pragma warning disable IDE0063 // Disable messages about Using expression simplification
#pragma warning disable IDE0090 // Disable messages about New expression simplification
#pragma warning disable IDE0028,IDE0300,IDE0305 // Disable message about collection initialization
#pragma warning disable IDE0074 // Disable message about compound assignment for checking if null
#pragma warning disable IDE0066 // Disable message about switch case expression
// Nullable reference types
#nullable enable
// My classes
using EditClipboardContents;
using static EditClipboardContents.ClipboardFormats;
namespace EditClipboardContents
{
internal static class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool CloseClipboard();
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EmptyClipboard();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint EnumClipboardFormats(uint format);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int GetClipboardFormatNameA(uint format, [Out] StringBuilder lpszFormatName, int cchMaxCount);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern UIntPtr GlobalSize(IntPtr hMem);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern IntPtr GlobalFree(IntPtr hMem);
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, UIntPtr count);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern uint DragQueryFile(IntPtr hDrop, uint iFile, StringBuilder? lpszFile, uint cch);
[DllImport("gdi32.dll")]
public static extern int GetObject(IntPtr hObject, int nCount, ref ClipboardFormats.BITMAP lpObject);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hdc);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, string? lpszFile);
[DllImport("user32.dll", SetLastError = true)]
public static extern int CountClipboardFormats();
[DllImport("gdi32.dll")]
public static extern IntPtr CopyMetaFile(IntPtr hMF, string? lpFileName);
[DllImport("gdi32.dll")]
public static extern bool DeleteMetaFile(IntPtr hMF);
[DllImport("gdi32.dll")]
public static extern IntPtr SetEnhMetaFileBits(uint cbBuffer, byte[] lpData);
[DllImport("gdi32.dll")]
public static extern bool DeleteEnhMetaFile(IntPtr hemf);
[DllImport("gdi32.dll")]
public static extern uint GetMetaFileBitsEx(IntPtr hmf, uint nSize, [In, Out] byte[]? lpvData);
[DllImport("gdi32.dll")]
public static extern uint GetEnhMetaFileBits(IntPtr hemf, uint cbBuffer, [In, Out] byte[]? lpbBuffer);
[DllImport("shell32.dll", CharSet = CharSet.Ansi)]
public static extern uint DragQueryFileA(IntPtr hDrop, uint iFile, [Out] StringBuilder lpszFile, uint cch);
//[DllImport("gdi32.dll")]
//public static extern IntPtr CreatePalette([In] ref ClipboardFormats.LOGPALETTE lplgpl); // Version of the method that takes a ref
[DllImport("gdi32.dll", SetLastError = true)]
public static extern IntPtr CreatePalette(IntPtr lplgpl); // Version of the method that takes a pointer
[DllImport("gdi32.dll")]
public static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint uStartScan, uint cScanLines, [Out] byte[]? lpvBits, ref BITMAPINFO lpbi, uint uUsage);
// ------------------------- Related to Diagnostics ---------------------------
[DllImport("user32.dll", SetLastError = true)]
public static extern bool IsClipboardFormatAvailable(uint format);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetClipboardOwner();
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetClipboardSequenceNumber();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetOpenClipboardWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClipboardFormatName(uint format, [Out] StringBuilder lpszFormatName, int cchMaxCount);
[DllImport("user32.dll", SetLastError = true)]
internal static extern void SetLastErrorEx(uint dwErrCode, uint dwType);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// Format message of error
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int FormatMessage(
int dwFlags,
IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
StringBuilder lpBuffer,
int nSize,
IntPtr Arguments
);
// -----------------------------------------------------------------------------
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); // Sending window messages like WM_RENDERFORMAT
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint RegisterClipboardFormat(string lpszFormat);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public static extern int GetObject(IntPtr hObject, int nCount, IntPtr lpObject);
[DllImport("gdi32.dll")]
public static extern UInt32 GetPaletteEntries(IntPtr hPalette, UInt32 iStartIndex, UInt32 nEntries, [Out] IntPtr lppe);
[DllImport("gdi32.dll")]
public static extern IntPtr SetMetaFileBitsEx(uint cbBuffer, IntPtr lpData);
// Constants
public const uint GMEM_MOVEABLE = 0x0002;
public const uint GMEM_ZEROINIT = 0x0040;
public const int WM_RENDERFORMAT = 0x0305;
public const int WM_RENDERALLFORMATS = 0x0306;
}
}