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

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

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在下面警告的那样,线程创建和窗口打开的日期可能不同。

  1. using System;
  2. using System.Runtime.InteropServices;
  3. public class WindowCreationDateFinder
  4. {
  5. [DllImport("user32.dll")]
  6. private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
  7. // Import the GetThreadTimes function from the kernel32.dll
  8. [DllImport("kernel32.dll")]
  9. private static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
  10. out long lpExitTime, out long lpKernelTime, out long lpUserTime);
  11. [DllImport("kernel32.dll")]
  12. private static extern uint GetLastError();
  13. [DllImport("kernel32.dll")]
  14. private static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
  15. [DllImport("kernel32.dll")]
  16. private static extern bool CloseHandle(IntPtr hObject);
  17. [Flags]
  18. private enum ThreadAccess : int
  19. {
  20. QUERY_INFORMATION = 0x0040
  21. }
  22. public static DateTime GetWindowCreationDate(uint windowHandle)
  23. {
  24. var threadID = GetWindowThreadProcessId(new IntPtr(windowHandle), out _);
  25. if (threadID == 0)
  26. ThrowWinApiError("GetWindowThreadProcessId");
  27. IntPtr threadHandle = OpenThread(ThreadAccess.QUERY_INFORMATION, false, threadID);
  28. if (threadHandle == IntPtr.Zero)
  29. ThrowWinApiError("OpenThread");
  30. try
  31. {
  32. var b = GetThreadTimes(threadHandle, out long start, out long end, out long rawKernelTime, out long rawUserTime);
  33. if (!b)
  34. ThrowWinApiError("GetThreadTimes");
  35. return DateTime.FromFileTime(start);
  36. }
  37. finally
  38. {
  39. CloseHandle(threadHandle);
  40. }
  41. }
  42. private static void ThrowWinApiError(string funcCalled)
  43. {
  44. var m = $"{funcCalled} error: {GetLastError()}. See: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-";
  45. throw new Exception(m);
  46. }
  47. }
英文:

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.

  1. using System;
  2. using System.Runtime.InteropServices;
  3. public class WindowCreationDateFinder
  4. {
  5. [DllImport("user32.dll")]
  6. private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
  7. // Import the GetThreadTimes function from the kernel32.dll
  8. [DllImport("kernel32.dll")]
  9. private static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
  10. out long lpExitTime, out long lpKernelTime, out long lpUserTime);
  11. [DllImport("kernel32.dll")]
  12. private static extern uint GetLastError();
  13. [DllImport("kernel32.dll")]
  14. private static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
  15. [DllImport("kernel32.dll")]
  16. private static extern bool CloseHandle(IntPtr hObject);
  17. [Flags]
  18. private enum ThreadAccess : int
  19. {
  20. QUERY_INFORMATION = 0x0040
  21. }
  22. public static DateTime GetWindowCreationDate(uint windowHandle)
  23. {
  24. var threadID = GetWindowThreadProcessId(new IntPtr(windowHandle), out _);
  25. if (threadID == 0)
  26. ThrowWinApiError("GetWindowThreadProcessId");
  27. IntPtr threadHandle = OpenThread(ThreadAccess.QUERY_INFORMATION, false, threadID);
  28. if (threadHandle == IntPtr.Zero)
  29. ThrowWinApiError("OpenThread");
  30. try
  31. {
  32. var b = GetThreadTimes(threadHandle, out long start, out long end, out long rawKernelTime, out long rawUserTime);
  33. if (!b)
  34. ThrowWinApiError("GetThreadTimes");
  35. return DateTime.FromFileTime(start);
  36. }
  37. finally
  38. {
  39. CloseHandle(threadHandle);
  40. }
  41. }
  42. private static void ThrowWinApiError(string funcCalled)
  43. {
  44. var m = $"{funcCalled} error: {GetLastError()}. See: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-";
  45. throw new Exception(m);
  46. }
  47. }

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:

确定