-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNative.cs
123 lines (96 loc) · 4.47 KB
/
Native.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
// Copyright (c) 2021 Eliah Kagan
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
namespace VidDraw {
/// <summary>Native methods (via P/Invoke) and supporting types.</summary>
internal static class Native {
internal const int MAX_PATH = 260;
/// <summary>Menu flags ("MF_") constants.</summary>
#pragma warning disable RCS1154 // Semantic order is clearer here.
[Flags]
internal enum MF : uint {
STRING = 0x0000,
SEPARATOR = 0x0800,
GRAYED = 0x0001,
CHECKED = 0x0008,
}
#pragma warning restore RCS1154
/// <summary>Menu item info member ("MIIM_") constants.</summary>
#pragma warning disable RCS1135 // API type has no (documented) 0 enumerator.
[Flags]
internal enum MIIM : uint {
STATE = 0x0001,
STRING = 0x0040,
}
#pragma warning restore RCS1135
/// <summary>Window message ("WM_") constants.</summary>
internal enum WM : uint {
INITDIALOG = 0x0110,
SYSCOMMAND = 0x0112,
INITMENU = 0x0116,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct MENUITEMINFO {
internal MENUITEMINFO(MIIM fMask)
{
// Most fields will start with their types' default values.
this = default;
// There is only one correct value for the size, so set it.
cbSize = (uint)Marshal.SizeOf<MENUITEMINFO>();
// Set the mask that determines which fields to use and how.
this.fMask = fMask;
}
internal readonly uint cbSize;
internal readonly MIIM fMask;
internal uint fType;
internal MF fState;
internal uint wID;
internal nint hSubMenu;
internal nint hbmpChecked;
internal nint hbmpUnchecked;
internal nuint dwItemData;
[MarshalAs(UnmanagedType.LPWStr)]
internal string? dwTypeData;
internal uint cch;
internal nint hbmpItem;
}
[DllImport("shell32", CharSet = CharSet.Unicode)]
internal static extern nint FindExecutable(string lpFile,
string? lpDirectory,
StringBuilder lpResult);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool AppendMenu(nint hMenu,
MF uFlags,
nuint uIDNewItem,
string? lpNewItem);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool GetMenuItemInfo(nint hmenu,
uint item,
bool fByPosition,
ref MENUITEMINFO lpmii);
[DllImport("user32")]
internal static extern nint GetSystemMenu(nint hWnd, bool bRevert);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool SetMenuItemInfo(nint hmenu,
uint item,
bool fByPosition,
ref MENUITEMINFO lpmii);
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool SetWindowText(nint hWnd, string lpString);
internal static void ThrowLastError()
=> throw new Win32Exception(Marshal.GetLastWin32Error());
}
}