How to check if the window handle value stored in an IntPtr type variable is valid in C#
In C#, you can use the IntPtr type to represent a window handle. To check if the window handle stored in an IntPtr variable is valid, you can use the IsWindow function from the user32.dll library. Here's an example:
using System;
using System.Runtime.InteropServices;
class Program
{
// Import the IsWindow function from user32.dll
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindow(IntPtr hWnd);
static void Main()
{
// Example IntPtr variable representing a window handle
IntPtr windowHandle = /* your window handle value here */;
// Check if the window handle is valid
if (IsWindow(windowHandle))
{
Console.WriteLine("The window handle is valid.");
}
else
{
Console.WriteLine("The window handle is not valid.");
}
}
}
Replace /* your window handle value here */ with the actual IntPtr variable containing the window handle you want to check.
The IsWindow function returns true if the specified window handle is valid and false otherwise. Keep in mind that this method only checks if the window handle is valid at the moment the function is called. It does not guarantee that the window will still be valid after the function returns, as the window could be destroyed by another part of the program or system.
'[====== Development ======] > C#' 카테고리의 다른 글
[WPF] Toast Message Nuget (0) | 2024.02.13 |
---|---|
C#에서 구조체 변수의 Column Attribute 값을 읽는 방법 (0) | 2024.01.24 |
grpc - client timeout 설정 (1) | 2024.01.03 |
C# - REST API Server에서 URL 자동 Mapping 기능 (1) | 2023.12.06 |
Enum Type Extentions (4) | 2023.12.05 |