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
+26 -25
View File
@@ -13,7 +13,7 @@ using Windows.Graphics.Imaging;
namespace IPClab1.Services;
public class WindowListService
public partial class WindowListService
{
#region Win32 P/Invoke Declarations
@@ -27,34 +27,34 @@ public class WindowListService
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
private static partial bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
private static partial bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[LibraryImport("user32.dll", EntryPoint = "GetWindowTextW", SetLastError = true)]
private static partial int GetWindowText(IntPtr hWnd, [Out] char[] lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowTextLength(IntPtr hWnd);
[LibraryImport("user32.dll", EntryPoint = "GetWindowTextLengthW", SetLastError = true)]
private static partial int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[LibraryImport("user32.dll", EntryPoint = "SendMessageW")]
private static partial IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetClassLongPtr", CharSet = CharSet.Auto)]
private static extern IntPtr GetClassLongPtr64(IntPtr hWnd, int nIndex);
[LibraryImport("user32.dll", EntryPoint = "GetClassLongPtrW")]
private static partial IntPtr GetClassLongPtr64(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "GetClassLong", CharSet = CharSet.Auto)]
private static extern uint GetClassLong32(IntPtr hWnd, int nIndex);
[LibraryImport("user32.dll", EntryPoint = "GetClassLongW")]
private static partial uint GetClassLong32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[LibraryImport("user32.dll", SetLastError = true)]
private static partial uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
[LibraryImport("shell32.dll", EntryPoint = "ExtractIconW", StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex);
private IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)
{
@@ -85,13 +85,14 @@ public class WindowListService
foreach (var hWnd in handles)
{
// Fetch window text title cleanly
int length = GetWindowTextLength(hWnd);
var builder = new StringBuilder(length + 1);
GetWindowText(hWnd, builder, builder.Capacity);
string title = builder.ToString();
var length = GetWindowTextLength(hWnd);
var titleBuffer = new char[length + 1];
var actualLength = GetWindowText(hWnd, titleBuffer, titleBuffer.Length);
var title = actualLength > 0 ? new string(titleBuffer, 0, actualLength) : string.Empty;
// Fetch numeric process identifier
GetWindowThreadProcessId(hWnd, out uint processId);
GetWindowThreadProcessId(hWnd, out var processId);
// Fetch and map the icon image synchronously/asynchronously into WinUI structures
SoftwareBitmapSource iconSource = await GetWinUi3IconAsync(hWnd);
@@ -110,7 +111,7 @@ public class WindowListService
private Icon GetIconFromHwnd(IntPtr hWnd)
{
IntPtr hIcon = SendMessage(hWnd, WM_GETICON, ICON_BIG, IntPtr.Zero);
var hIcon = SendMessage(hWnd, WM_GETICON, ICON_BIG, IntPtr.Zero);
if (hIcon == IntPtr.Zero)
hIcon = SendMessage(hWnd, WM_GETICON, ICON_SMALL2, IntPtr.Zero);