Thread overview
Can't call GetWindowTextW - Error:undefined identifier
Jun 17, 2015
Dan
Jun 17, 2015
Dan
Jun 17, 2015
Dan
Jun 17, 2015
John Chapman
Jun 17, 2015
Dan
Jun 17, 2015
John Chapman
Jun 17, 2015
Dan
Jun 17, 2015
John Chapman
June 17, 2015
hi

I'm using those imports:

import core.runtime;
import core.sys.windows.windows;

when I call GetWindowTextW DMD compiler complains! (error:undefined identifier)

any solution?
June 17, 2015
I'm new to Dlang and I have no Idea whats wrong with this code!

wchar[260] buffer;
HWND hWindow = GetForegroundWindow();
GetWindowTextW(hWindow, buffer, sizeof(title)); <-- Problem here
June 17, 2015
> GetWindowTextW(hWindow, buffer, sizeof(title)); <-- Problem here

please Ignore the sizeof(title) parameter, I copied that from c++ equivalent code :D

June 17, 2015
On Wednesday, 17 June 2015 at 20:40:02 UTC, Dan wrote:
> I'm new to Dlang and I have no Idea whats wrong with this code!
>
> wchar[260] buffer;
> HWND hWindow = GetForegroundWindow();
> GetWindowTextW(hWindow, buffer, sizeof(title)); <-- Problem here

The compiler is complaining it can't find an identifier named GetWindowTextW, so you'll have to declare it yourself.

Try this:

  extern(Windows)
  int GetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount);

And call it like so:

  wchar[MAX_PATH] buffer;
  int length = GetWindowTextW(GetForegroundWindow(), buffer.ptr, buffer.length);
June 17, 2015
thank you John it worked :)

do I always need do the same for all windows API?



June 17, 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();
  }
June 17, 2015
On Wednesday, 17 June 2015 at 21:00:55 UTC, Dan wrote:
> thank you John it worked :)
>
> do I always need do the same for all windows API?

For most Win32 API functions, yes. Although there are some more complete headers on Github (for example, https://github.com/rikkimax/WindowsAPI).
June 17, 2015
thank you so much John :)