On Wednesday, 17 June 2015 at 20:50:27 UTC, John Chapman wrote:
  wchar[MAX_PATH] buffer;
int length = GetWindowTextW(GetForegroundWindow(), buffer.ptr, buffer.length);

Don't know why I used MAX_PATH there. You should probably dynamically allocate a buffer based on GetWindowTextLengthW.

  extern(Windows)
  int GetWindowTextLengthW(HWND hWnd);

Putting it all together:

  import core.stdc.stdlib : malloc, free;
  import std.utf;

  HWND hwnd = GetForegroundWindow();
  int length = GetWindowTextLengthW(hwnd);
  if (length > 0) {
    auto buffer = cast(wchar*)malloc((length + 1) * wchar.sizeof);
    scope(exit) free(buffer);

    length = GetWindowTextW(hwnd, buffer, length);
    if (length > 0)
      auto title = buffer[0 .. length].toUTF8();
  }

Reply via email to