Can't call GetWindowTextW - Error:undefined identifier

John Chapman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 17 14:02:11 PDT 2015


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


More information about the Digitalmars-d-learn mailing list