PInvoke code cleanup

This commit is contained in:
Sardelka9515
2023-03-11 16:39:20 +08:00
parent f44715cdf4
commit cea49dae2b
3 changed files with 47 additions and 484 deletions

View File

@ -10,28 +10,26 @@ using System.IO;
using System;
using SHVDN;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace RageCoop.Client
{
internal static class Shared
{
private static string GetBasePath()
private static unsafe string GetBasePath()
{
FileInfo info;
string realScriptDir = Directory.GetParent(Instance.FilePath).FullName;
nextTarget:
info = new(realScriptDir);
if (info.LinkTarget != null)
{
realScriptDir = info.LinkTarget;
goto nextTarget;
}
if (Path.GetFileName(realScriptDir).ToLower() != "scripts")
throw new FileNotFoundException($"Unexpected link target {realScriptDir}");
using var fs = File.OpenRead(Instance.FilePath);
var buf = stackalloc char[1024];
GetFinalPathNameByHandleW(fs.SafeFileHandle.DangerousGetHandle(), buf, 1024, 0);
ErrorCheck32();
var scriptDir = Directory.GetParent(Marshal.PtrToStringUni((IntPtr)buf)).FullName;
if (Path.GetFileName(scriptDir).ToLower() != "scripts")
throw new Exception("Unexpected script location");
var baseDir = Directory.GetParent(realScriptDir).FullName;
Logger.Debug($"Base directory is {baseDir}");
return baseDir;
var result = Directory.GetParent(scriptDir).FullName;
Logger.Debug($"Base path is: {result}");
return result;
}
public static string BasePath = GetBasePath();

View File

@ -0,0 +1,34 @@
global using static RageCoop.Client.PInvoke;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace RageCoop.Client
{
internal partial class PInvoke
{
public static void ClearLastError()
{
SetLastErrorEx(0, 0);
}
/// <summary>
/// Check and throw if an error occurred during last winapi call in current thread
/// </summary>
public static void ErrorCheck32()
{
var error = Marshal.GetLastWin32Error();
if (error != 0)
{
ClearLastError();
throw new Win32Exception(error);
}
}
[LibraryImport("user32.dll", SetLastError = true)]
internal static partial void SetLastErrorEx(uint dwErrCode, uint dwType);
[LibraryImport("Kernel32.dll", SetLastError = true)]
public static unsafe partial uint GetFinalPathNameByHandleW(IntPtr hFile, char* lpszFilePath, uint cchFilePath, uint dwFlags);
}
}

View File

@ -1,469 +0,0 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo
// ReSharper disable UnusedMember.Local
namespace RageCoop.Client
{
internal class Win32
{
#region ENUM
/// <summary>
/// Enumerates the valid hook types passed as the idHook parameter into a call to SetWindowsHookEx.
/// </summary>
public enum HookType
{
/// <summary>
/// Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box,
/// message box, menu, or scroll bar. For more information, see the MessageProc hook procedure.
/// </summary>
WH_MSGFILTER = -1,
/// <summary>
/// Installs a hook procedure that records input messages posted to the system message queue. This hook is
/// useful for recording macros. For more information, see the JournalRecordProc hook procedure.
/// </summary>
WH_JOURNALRECORD = 0,
/// <summary>
/// Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure.
/// For more information, see the JournalPlaybackProc hook procedure.
/// </summary>
WH_JOURNALPLAYBACK = 1,
/// <summary>
/// Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc
/// hook procedure.
/// </summary>
WH_KEYBOARD = 2,
/// <summary>
/// Installs a hook procedure that monitors messages posted to a message queue. For more information, see the
/// GetMsgProc hook procedure.
/// </summary>
WH_GETMESSAGE = 3,
/// <summary>
/// Installs a hook procedure that monitors messages before the system sends them to the destination window
/// procedure. For more information, see the CallWndProc hook procedure.
/// </summary>
WH_CALLWNDPROC = 4,
/// <summary>
/// Installs a hook procedure that receives notifications useful to a CBT application. For more information,
/// see the CBTProc hook procedure.
/// </summary>
WH_CBT = 5,
/// <summary>
/// Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box,
/// message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the
/// same desktop as the calling thread. For more information, see the SysMsgProc hook procedure.
/// </summary>
WH_SYSMSGFILTER = 6,
/// <summary>
/// Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook
/// procedure.
/// </summary>
WH_MOUSE = 7,
/// <summary>
/// </summary>
WH_HARDWARE = 8,
/// <summary>
/// Installs a hook procedure useful for debugging other hook procedures. For more information, see the
/// DebugProc hook procedure.
/// </summary>
WH_DEBUG = 9,
/// <summary>
/// Installs a hook procedure that receives notifications useful to shell applications. For more information,
/// see the ShellProc hook procedure.
/// </summary>
WH_SHELL = 10,
/// <summary>
/// Installs a hook procedure that will be called when the application's foreground thread is about to become
/// idle. This hook is useful for performing low priority tasks during idle time. For more information, see the
/// ForegroundIdleProc hook procedure.
/// </summary>
WH_FOREGROUNDIDLE = 11,
/// <summary>
/// Installs a hook procedure that monitors messages after they have been processed by the destination window
/// procedure. For more information, see the CallWndRetProc hook procedure.
/// </summary>
WH_CALLWNDPROCRET = 12,
/// <summary>
/// Installs a hook procedure that monitors low-level keyboard input events. For more information, see the
/// LowLevelKeyboardProc hook procedure.
/// </summary>
WH_KEYBOARD_LL = 13,
/// <summary>
/// Installs a hook procedure that monitors low-level mouse input events. For more information, see the
/// LowLevelMouseProc hook procedure.
/// </summary>
WH_MOUSE_LL = 14
}
#endregion
/*
public static Image CaptureWindow(IntPtr handle)
{
var windowDC = User32.GetWindowDC(handle);
var rECT = new RECT();
User32.GetWindowRect(handle, ref rECT);
var num = rECT.right - rECT.left;
var num1 = rECT.bottom - rECT.top;
var intPtr = GDI32.CreateCompatibleDC(windowDC);
var intPtr1 = GDI32.CreateCompatibleBitmap(windowDC, num, num1);
var intPtr2 = GDI32.SelectObject(intPtr, intPtr1);
GDI32.BitBlt(intPtr, 0, 0, num, num1, windowDC, 0, 0, 13369376);
GDI32.SelectObject(intPtr, intPtr2);
Image image = Image.FromHbitmap(intPtr1);
GDI32.DeleteObject(intPtr1);
GDI32.DeleteDC(intPtr);
User32.ReleaseDC(handle, windowDC);
return image;
}
*/
public static void ClearLastError()
{
SetLastErrorEx(0, 0);
}
public static class GDI32
{
public const int SRCCOPY = 13369376;
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth, int nHeight,
IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
public static class User32
{
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
}
#region INPUT-HOOK
[DllImport("user32.dll", SetLastError = true)]
internal static extern void SetLastErrorEx(uint dwErrCode, uint dwType);
[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType idHook, MouseProc lpfn, int hInstance, int threadId);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern int CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern int GetCurrentThreadId();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern int GetModuleHandle(string lpModuleName);
public delegate int MouseProc(int nCode, IntPtr wParam, IntPtr lParam);
#endregion
#region WM-CONST
public const uint WM_ACTIVATE = 0x0006;
public const uint WM_ACTIVATEAPP = 0x001C;
public const uint WM_AFXFIRST = 0x0360;
public const uint WM_AFXLAST = 0x037F;
public const uint WM_APP = 0x8000;
public const uint WM_ASKCBFORMATNAME = 0x030C;
public const uint WM_CANCELJOURNAL = 0x004B;
public const uint WM_CANCELMODE = 0x001F;
public const uint WM_CAPTURECHANGED = 0x0215;
public const uint WM_CHANGECBCHAIN = 0x030D;
public const uint WM_CHANGEUISTATE = 0x0127;
public const uint WM_CHAR = 0x0102;
public const uint WM_CHARTOITEM = 0x002F;
public const uint WM_CHILDACTIVATE = 0x0022;
public const uint WM_CLEAR = 0x0303;
public const uint WM_CLOSE = 0x0010;
public const uint WM_CLIPBOARDUPDATE = 0x031D;
public const uint WM_COMMAND = 0x0111;
public const uint WM_COMPACTING = 0x0041;
public const uint WM_COMPAREITEM = 0x0039;
public const uint WM_CONTEXTMENU = 0x007B;
public const uint WM_COPY = 0x0301;
public const uint WM_COPYDATA = 0x004A;
public const uint WM_CREATE = 0x0001;
public const uint WM_CTLCOLORBTN = 0x0135;
public const uint WM_CTLCOLORDLG = 0x0136;
public const uint WM_CTLCOLOREDIT = 0x0133;
public const uint WM_CTLCOLORLISTBOX = 0x0134;
public const uint WM_CTLCOLORMSGBOX = 0x0132;
public const uint WM_CTLCOLORSCROLLBAR = 0x0137;
public const uint WM_CTLCOLORSTATIC = 0x0138;
public const uint WM_CUT = 0x0300;
public const uint WM_DEADCHAR = 0x0103;
public const uint WM_DELETEITEM = 0x002D;
public const uint WM_DESTROY = 0x0002;
public const uint WM_DESTROYCLIPBOARD = 0x0307;
public const uint WM_DEVICECHANGE = 0x0219;
public const uint WM_DEVMODECHANGE = 0x001B;
public const uint WM_DISPLAYCHANGE = 0x007E;
public const uint WM_DRAWCLIPBOARD = 0x0308;
public const uint WM_DRAWITEM = 0x002B;
public const uint WM_DROPFILES = 0x0233;
public const uint WM_ENABLE = 0x000A;
public const uint WM_ENDSESSION = 0x0016;
public const uint WM_ENTERIDLE = 0x0121;
public const uint WM_ENTERMENULOOP = 0x0211;
public const uint WM_ENTERSIZEMOVE = 0x0231;
public const uint WM_ERASEBKGND = 0x0014;
public const uint WM_EXITMENULOOP = 0x0212;
public const uint WM_EXITSIZEMOVE = 0x0232;
public const uint WM_FONTCHANGE = 0x001D;
public const uint WM_GETDLGCODE = 0x0087;
public const uint WM_GETFONT = 0x0031;
public const uint WM_GETHOTKEY = 0x0033;
public const uint WM_GETICON = 0x007F;
public const uint WM_GETMINMAXINFO = 0x0024;
public const uint WM_GETOBJECT = 0x003D;
public const uint WM_GETTEXT = 0x000D;
public const uint WM_GETTEXTLENGTH = 0x000E;
public const uint WM_HANDHELDFIRST = 0x0358;
public const uint WM_HANDHELDLAST = 0x035F;
public const uint WM_HELP = 0x0053;
public const uint WM_HOTKEY = 0x0312;
public const uint WM_HSCROLL = 0x0114;
public const uint WM_HSCROLLCLIPBOARD = 0x030E;
public const uint WM_ICONERASEBKGND = 0x0027;
public const uint WM_IME_CHAR = 0x0286;
public const uint WM_IME_COMPOSITION = 0x010F;
public const uint WM_IME_COMPOSITIONFULL = 0x0284;
public const uint WM_IME_CONTROL = 0x0283;
public const uint WM_IME_ENDCOMPOSITION = 0x010E;
public const uint WM_IME_KEYDOWN = 0x0290;
public const uint WM_IME_KEYLAST = 0x010F;
public const uint WM_IME_KEYUP = 0x0291;
public const uint WM_IME_NOTIFY = 0x0282;
public const uint WM_IME_REQUEST = 0x0288;
public const uint WM_IME_SELECT = 0x0285;
public const uint WM_IME_SETCONTEXT = 0x0281;
public const uint WM_IME_STARTCOMPOSITION = 0x010D;
public const uint WM_INITDIALOG = 0x0110;
public const uint WM_INITMENU = 0x0116;
public const uint WM_INITMENUPOPUP = 0x0117;
public const uint WM_INPUTLANGCHANGE = 0x0051;
public const uint WM_INPUTLANGCHANGEREQUEST = 0x0050;
public const uint WM_KEYDOWN = 0x0100;
public const uint WM_KEYFIRST = 0x0100;
public const uint WM_KEYLAST = 0x0108;
public const uint WM_KEYUP = 0x0101;
public const uint WM_KILLFOCUS = 0x0008;
public const uint WM_LBUTTONDBLCLK = 0x0203;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
public const uint WM_MBUTTONDBLCLK = 0x0209;
public const uint WM_MBUTTONDOWN = 0x0207;
public const uint WM_MBUTTONUP = 0x0208;
public const uint WM_MDIACTIVATE = 0x0222;
public const uint WM_MDICASCADE = 0x0227;
public const uint WM_MDICREATE = 0x0220;
public const uint WM_MDIDESTROY = 0x0221;
public const uint WM_MDIGETACTIVE = 0x0229;
public const uint WM_MDIICONARRANGE = 0x0228;
public const uint WM_MDIMAXIMIZE = 0x0225;
public const uint WM_MDINEXT = 0x0224;
public const uint WM_MDIREFRESHMENU = 0x0234;
public const uint WM_MDIRESTORE = 0x0223;
public const uint WM_MDISETMENU = 0x0230;
public const uint WM_MDITILE = 0x0226;
public const uint WM_MEASUREITEM = 0x002C;
public const uint WM_MENUCHAR = 0x0120;
public const uint WM_MENUCOMMAND = 0x0126;
public const uint WM_MENUDRAG = 0x0123;
public const uint WM_MENUGETOBJECT = 0x0124;
public const uint WM_MENURBUTTONUP = 0x0122;
public const uint WM_MENUSELECT = 0x011F;
public const uint WM_MOUSEACTIVATE = 0x0021;
public const uint WM_MOUSEFIRST = 0x0200;
public const uint WM_MOUSEHOVER = 0x02A1;
public const uint WM_MOUSELAST = 0x020D;
public const uint WM_MOUSELEAVE = 0x02A3;
public const uint WM_MOUSEMOVE = 0x0200;
public const uint WM_MOUSEWHEEL = 0x020A;
public const uint WM_MOUSEHWHEEL = 0x020E;
public const uint WM_MOVE = 0x0003;
public const uint WM_MOVING = 0x0216;
public const uint WM_NCACTIVATE = 0x0086;
public const uint WM_NCCALCSIZE = 0x0083;
public const uint WM_NCCREATE = 0x0081;
public const uint WM_NCDESTROY = 0x0082;
public const uint WM_NCHITTEST = 0x0084;
public const uint WM_NCLBUTTONDBLCLK = 0x00A3;
public const uint WM_NCLBUTTONDOWN = 0x00A1;
public const uint WM_NCLBUTTONUP = 0x00A2;
public const uint WM_NCMBUTTONDBLCLK = 0x00A9;
public const uint WM_NCMBUTTONDOWN = 0x00A7;
public const uint WM_NCMBUTTONUP = 0x00A8;
public const uint WM_NCMOUSEHOVER = 0x02A0;
public const uint WM_NCMOUSELEAVE = 0x02A2;
public const uint WM_NCMOUSEMOVE = 0x00A0;
public const uint WM_NCPAINT = 0x0085;
public const uint WM_NCRBUTTONDBLCLK = 0x00A6;
public const uint WM_NCRBUTTONDOWN = 0x00A4;
public const uint WM_NCRBUTTONUP = 0x00A5;
public const uint WM_NCXBUTTONDBLCLK = 0x00AD;
public const uint WM_NCXBUTTONDOWN = 0x00AB;
public const uint WM_NCXBUTTONUP = 0x00AC;
public const uint WM_NCUAHDRAWCAPTION = 0x00AE;
public const uint WM_NCUAHDRAWFRAME = 0x00AF;
public const uint WM_NEXTDLGCTL = 0x0028;
public const uint WM_NEXTMENU = 0x0213;
public const uint WM_NOTIFY = 0x004E;
public const uint WM_NOTIFYFORMAT = 0x0055;
public const uint WM_NULL = 0x0000;
public const uint WM_PAINT = 0x000F;
public const uint WM_PAINTCLIPBOARD = 0x0309;
public const uint WM_PAINTICON = 0x0026;
public const uint WM_PALETTECHANGED = 0x0311;
public const uint WM_PALETTEISCHANGING = 0x0310;
public const uint WM_PARENTNOTIFY = 0x0210;
public const uint WM_PASTE = 0x0302;
public const uint WM_PENWINFIRST = 0x0380;
public const uint WM_PENWINLAST = 0x038F;
public const uint WM_POWER = 0x0048;
public const uint WM_POWERBROADCAST = 0x0218;
public const uint WM_PRINT = 0x0317;
public const uint WM_PRINTCLIENT = 0x0318;
public const uint WM_QUERYDRAGICON = 0x0037;
public const uint WM_QUERYENDSESSION = 0x0011;
public const uint WM_QUERYNEWPALETTE = 0x030F;
public const uint WM_QUERYOPEN = 0x0013;
public const uint WM_QUEUESYNC = 0x0023;
public const uint WM_QUIT = 0x0012;
public const uint WM_RBUTTONDBLCLK = 0x0206;
public const uint WM_RBUTTONDOWN = 0x0204;
public const uint WM_RBUTTONUP = 0x0205;
public const uint WM_RENDERALLFORMATS = 0x0306;
public const uint WM_RENDERFORMAT = 0x0305;
public const uint WM_SETCURSOR = 0x0020;
public const uint WM_SETFOCUS = 0x0007;
public const uint WM_SETFONT = 0x0030;
public const uint WM_SETHOTKEY = 0x0032;
public const uint WM_SETICON = 0x0080;
public const uint WM_SETREDRAW = 0x000B;
public const uint WM_SETTEXT = 0x000C;
public const uint WM_SETTINGCHANGE = 0x001A;
public const uint WM_SHOWWINDOW = 0x0018;
public const uint WM_SIZE = 0x0005;
public const uint WM_SIZECLIPBOARD = 0x030B;
public const uint WM_SIZING = 0x0214;
public const uint WM_SPOOLERSTATUS = 0x002A;
public const uint WM_STYLECHANGED = 0x007D;
public const uint WM_STYLECHANGING = 0x007C;
public const uint WM_SYNCPAINT = 0x0088;
public const uint WM_SYSCHAR = 0x0106;
public const uint WM_SYSCOLORCHANGE = 0x0015;
public const uint WM_SYSCOMMAND = 0x0112;
public const uint WM_SYSDEADCHAR = 0x0107;
public const uint WM_SYSKEYDOWN = 0x0104;
public const uint WM_SYSKEYUP = 0x0105;
public const uint WM_TCARD = 0x0052;
public const uint WM_TIMECHANGE = 0x001E;
public const uint WM_TIMER = 0x0113;
public const uint WM_UNDO = 0x0304;
public const uint WM_UNINITMENUPOPUP = 0x0125;
public const uint WM_USER = 0x0400;
public const uint WM_USERCHANGED = 0x0054;
public const uint WM_VKEYTOITEM = 0x002E;
public const uint WM_VSCROLL = 0x0115;
public const uint WM_VSCROLLCLIPBOARD = 0x030A;
public const uint WM_WINDOWPOSCHANGED = 0x0047;
public const uint WM_WINDOWPOSCHANGING = 0x0046;
public const uint WM_WININICHANGE = 0x001A;
public const uint WM_XBUTTONDBLCLK = 0x020D;
public const uint WM_XBUTTONDOWN = 0x020B;
public const uint WM_XBUTTONUP = 0x020C;
#endregion
#region STRUCT
[StructLayout(LayoutKind.Sequential)]
private struct MouseHookStructEx
{
public readonly MOUSEHOOKSTRUCT mouseHookStruct;
public readonly int MouseData;
}
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEHOOKSTRUCT
{
public Point pt;
public IntPtr hwnd;
public uint wHitTestCode;
public IntPtr dwExtraInfo;
}
#endregion
}
}