Changed to library imports instead of dll imports

This commit is contained in:
2026-09-19 16:48:39 +03:00 Unverified
parent 171f94414d
commit 8e3002f771
3 changed files with 44 additions and 42 deletions
+17 -17
View File
@@ -8,7 +8,7 @@ using IPClab1.Enums;
namespace IPClab1.Services;
public class WindowInteractionService
public partial class WindowInteractionService
{
private const uint WM_MOUSEMOVE = 0x0200;
private const uint WM_LBUTTONDOWN = 0x0201;
@@ -21,22 +21,22 @@ public class WindowInteractionService
private const int SC_MAXIMIZE = 0xF030;
private const int SC_RESTORE = 0xF120;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
[LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
// 1. Get Class Name
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[LibraryImport("user32.dll", EntryPoint = "PostMessageW")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
// 2. Get Geometry
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[LibraryImport("user32.dll", EntryPoint = "GetClassNameW", SetLastError = true)]
private static partial int GetClassName(IntPtr hWnd, [Out] char[] lpClassName, int nMaxCount);
// 3. Get Parent
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetParent(IntPtr hWnd);
[LibraryImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[LibraryImport("user32.dll")]
private static partial IntPtr GetParent(IntPtr hWnd);
public void SimulateClick(IntPtr hwnd, int x, int y)
{
@@ -81,9 +81,9 @@ public class WindowInteractionService
public string GetWindowDetails(IntPtr hwnd)
{
StringBuilder classNameBuffer = new StringBuilder(256);
GetClassName(hwnd, classNameBuffer, classNameBuffer.Capacity);
string className = classNameBuffer.ToString();
char[] classNameBuffer = new char[256];
int length = GetClassName(hwnd, classNameBuffer, classNameBuffer.Length);
string className = length > 0 ? new string(classNameBuffer, 0, length) : string.Empty;
GetWindowRect(hwnd, out RECT rect);