英文:
How to get window open/creation date from window handle?
问题
我有2个以IE模式打开的MS Edge窗口。我需要找出哪个是后打开的。我有两者的窗口句柄。我应该如何做到这一点?GetWindowInfo函数不返回打开的日期。这可行吗?ChatGPT提出了一些代码,但它没有工作
英文:
I have 2 MS Edge windows open in IE mode. I have to find out the one opened later. I have window handles for both. How can I do this? GetWindowInfo function does not return the open date. Is this possible? ChatGPT proposed some code but it didn't work
答案1
得分: 1
以下是一个简单的C#类,用于读取打开窗口的线程的创建日期,其句柄由其句柄指定。正如Damien在下面警告的那样,线程创建和窗口打开的日期可能不同。
using System;
using System.Runtime.InteropServices;
public class WindowCreationDateFinder
{
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
// Import the GetThreadTimes function from the kernel32.dll
[DllImport("kernel32.dll")]
private static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
[DllImport("kernel32.dll")]
private static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
[Flags]
private enum ThreadAccess : int
{
QUERY_INFORMATION = 0x0040
}
public static DateTime GetWindowCreationDate(uint windowHandle)
{
var threadID = GetWindowThreadProcessId(new IntPtr(windowHandle), out _);
if (threadID == 0)
ThrowWinApiError("GetWindowThreadProcessId");
IntPtr threadHandle = OpenThread(ThreadAccess.QUERY_INFORMATION, false, threadID);
if (threadHandle == IntPtr.Zero)
ThrowWinApiError("OpenThread");
try
{
var b = GetThreadTimes(threadHandle, out long start, out long end, out long rawKernelTime, out long rawUserTime);
if (!b)
ThrowWinApiError("GetThreadTimes");
return DateTime.FromFileTime(start);
}
finally
{
CloseHandle(threadHandle);
}
}
private static void ThrowWinApiError(string funcCalled)
{
var m = $"{funcCalled} error: {GetLastError()}. See: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-";
throw new Exception(m);
}
}
英文:
Here is a simple C# class to read creation date of a thread that opened a window specified by its handle. As Damien has warned below, thread creation and window open dates can differ.
using System;
using System.Runtime.InteropServices;
public class WindowCreationDateFinder
{
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
// Import the GetThreadTimes function from the kernel32.dll
[DllImport("kernel32.dll")]
private static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
[DllImport("kernel32.dll")]
private static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);
[Flags]
private enum ThreadAccess : int
{
QUERY_INFORMATION = 0x0040
}
public static DateTime GetWindowCreationDate(uint windowHandle)
{
var threadID = GetWindowThreadProcessId(new IntPtr(windowHandle), out _);
if (threadID == 0)
ThrowWinApiError("GetWindowThreadProcessId");
IntPtr threadHandle = OpenThread(ThreadAccess.QUERY_INFORMATION, false, threadID);
if (threadHandle == IntPtr.Zero)
ThrowWinApiError("OpenThread");
try
{
var b = GetThreadTimes(threadHandle, out long start, out long end, out long rawKernelTime, out long rawUserTime);
if (!b)
ThrowWinApiError("GetThreadTimes");
return DateTime.FromFileTime(start);
}
finally
{
CloseHandle(threadHandle);
}
}
private static void ThrowWinApiError(string funcCalled)
{
var m = $"{funcCalled} error: {GetLastError()}. See: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-";
throw new Exception(m);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论