如何从窗口句柄获取窗口打开/创建日期?

huangapple go评论70阅读模式
英文:

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);
    }

}

huangapple
  • 本文由 发表于 2023年6月16日 14:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定