digitalmars.D.learn - Can't call GetWindowTextW - Error:undefined identifier
- Dan (7/7) Jun 17 2015 hi
- Dan (4/4) Jun 17 2015 I'm new to Dlang and I have no Idea whats wrong with this code!
- Dan (2/3) Jun 17 2015 please Ignore the sizeof(title) parameter, I copied that from c++
- John Chapman (10/14) Jun 17 2015 The compiler is complaining it can't find an identifier named
- Dan (2/2) Jun 17 2015 thank you John it worked :)
- John Chapman (4/6) Jun 17 2015 For most Win32 API functions, yes. Although there are some more
- Dan (1/1) Jun 17 2015 thank you so much John :)
- John Chapman (17/20) Jun 17 2015 Don't know why I used MAX_PATH there. You should probably
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?
Jun 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
Jun 17 2015
GetWindowTextW(hWindow, buffer, sizeof(title)); <-- Problem hereplease Ignore the sizeof(title) parameter, I copied that from c++ equivalent code :D
Jun 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 hereThe 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);
Jun 17 2015
thank you John it worked :) do I always need do the same for all windows API?
Jun 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).
Jun 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(); }
Jun 17 2015