Failed to get Windows Handle (HWND) after installing certain software.

Oh, Jongseok 0 Reputation points
2025-02-17T08:04:34.8+00:00

Hi,

I configured Visual Studio to look for the Process I created with the following configuration.

  1. Create a Process using ::CreateProcess, a WINAPI
    1. bResult = ::CreateProcess( nullptr, // LPCTSTR lpApplicationName, tszServerPath, // LPTSTR lpCommandLine nullptr, // LPSECURITY_ATTRIBUTES lpProcessAttributes nullptr, // LPSECURITY_ATTRIBUTES lpThreadAttributes FALSE, // BOOL bInheritHandles, NULL, // DWORD dwCreationFlags, nullptr, // LPVOID lpEnvironment, nullptr, // LPCTSTR lpCurrentDirectory, &si, // LPSTARTUPINFO lpStartupInfo, &piLib32Server // LPPROCESS_INFORMATION lpProcessInformation ) != FALSE;
  2. Find the HWND of the Process through the Class that inherits using the Process ID we got.
    1. ::EnumWindows(EnumProc, (LPARAM)this); // Create an Iterator Class and set it to iterate
    2. if (GetWindowLong(hwnd,GWL_STYLE) & WS_VISIBLE) { DWORD pidwin; GetWindowThreadProcessId(hwnd, &pidwin); if (pidwin==m_pid)
      	return TRUE;
      
      } // m_pid is the ID of the Process created in 1 and iterates over it to find the Window. return FALSE;

The code was working fine, but now when I install some programs, the window with the same id is not fetching. :(

But installing the 2015-2022 redistribution package will resolve the issue.

Do you know of any cases like this? I would expect the problem to be in a specific dll file or something. :)

If you know of any, or can find Process in another way, please let me know.

Thank you. :)

Developer technologies | Visual Studio | Other
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 49,666 Reputation points
    2025-02-17T12:25:57.1033333+00:00

    The documentation for the CreateProcess function says "Note that the function returns before the process has finished initialization. If a required DLL cannot be located or fails to initialize, the process is terminated. To get the termination status of a process, call GetExitCodeProcess." So it is possible that the process you have created has already terminated and the process ID returned by CreateProcess is no longer valid at the time you enumerate windows.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.