-1

I need to write a program in C++ that queries the OS for running Windows, find a Window with a specific window title, and return the Native Window Handle value as a String.

So far, I've figured out everything but the last part. I'm not too familiar with C++, as I'm writing this for a JS project as a NodeJS C++ addon, so I'd appreciate any help.

Here's the program so far:

static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
  int length = GetWindowTextLength(hWnd);
  char* buffer = new char[length + 1];
  GetWindowText(hWnd, buffer, length + 1);
  std::string windowTitle(buffer);

  if (IsWindowVisible(hWnd) && length != 0) {
    std::cout << hWnd << ":  " << windowTitle << std::endl;
    if (windowTitle.compare("Find This Window") == 0) {
      // Here is the confusion. I've found the right HWND, but I don't know how to cast the HWND to a String
      return FALSE;
     }
  }

  return TRUE;
}

int::main() {
  EnumWindows(enumWindowCallback, NULL);
}

On this line: std::cout << hWnd << ": " << windowTitle << std::endl;, the hWnd returns the hexadecimal value that I want. For example, it prints out: 0000000000100566: Find this App

The value preceding the : is the value I want to return as a String, but I can't figure out how. I've tried casting it, but it doesn't work.

Again, this is probably a simple solution, but I can't find it on the internet and my limited knowledge of C++ is hindering me.

273K
  • 29,503
  • 10
  • 41
  • 64
lakers5824
  • 345
  • 1
  • 2
  • 10
  • 1
    An `HWND` is an opaque value - there's no way of knowing exactly what it represents, and it has no use outside of calls into WinAPI functions. What purpose do you think getting it as a string will serve? And it's a numeric value, so you can't *cast it* to a string. – Ken White Sep 30 '22 at 01:01
  • It's not casting that you want. In the indicated line, you are doing formatted I/O. Consider doing the same, but instead of cout use a std::ostringstream object that you instantiate - output to a string like thing rather than the console. – Avi Berger Sep 30 '22 at 01:04
  • Long story short, I need to provide the Native Window Handle hex value to an automated testing framework so I can attach to an existing application. The testrunner takes a string for this value. – lakers5824 Sep 30 '22 at 01:07
  • When I manually use Inspect.exe on a Window and provide the NativeWindowHandle value to the testrunner, it works. Also, when I copy and paste the value from `std::cout << hWnd` above to the test runner, it works. Now I just need a way to do it programmatically – lakers5824 Sep 30 '22 at 01:09
  • `GetWindowText` provides the title of the Window. I don't want that. I want the Native Window Handle value. Ie. Opening Inspect.exe on a window, the property `NativeWindowHandle` – lakers5824 Sep 30 '22 at 01:10
  • [See here](https://en.cppreference.com/w/cpp/io/basic_ostream) – Avi Berger Sep 30 '22 at 01:10
  • You have not answered on my question in the first comment. – 273K Sep 30 '22 at 01:13
  • No, I do not get a compiler warning on `GetWindowText`. Why? – lakers5824 Sep 30 '22 at 01:20
  • I suspected `GetWindowTextW` version was used and `buffer` should be `wchar_t* buffer` or `GetWindowTextA` should be used. I seemed to misread the quesion. – 273K Sep 30 '22 at 01:32
  • Use [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string). Besides, the `EnumWindows` call and manual filtering is pretty much just doing what [`FindWindow`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindoww) does. – IInspectable Sep 30 '22 at 06:46
  • Thank you @AviBerger, your suggestion was the only one that worked. Appreciate it! – lakers5824 Sep 30 '22 at 17:55

1 Answers1

0

Actually, the question is how to convert number to string.
At the beginning, as @KenWhite said, HWND is an opaque value.

#include <Windows.h>
#include <sstream>
void main()
{
    HWND i = FindWindow(NULL,NULL);
    char buffer[sizeof(HWND) * 2 + 1]{};
    _itoa_s((int)i, buffer, 16);

    std::ostringstream Convert;
    Convert << std::hex << i;
}
YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22