www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't call GetWindowTextW - Error:undefined identifier

reply "Dan" <Dan example.com> writes:
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
parent reply "Dan" <Dan example.com> writes:
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
next sibling parent "Dan" <Dan example.com> writes:
 GetWindowTextW(hWindow, buffer, sizeof(title)); <-- Problem here
please Ignore the sizeof(title) parameter, I copied that from c++ equivalent code :D
Jun 17 2015
prev sibling parent reply "John Chapman" <johnch_atms hotmail.com> writes:
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);
Jun 17 2015
next sibling parent reply "Dan" <Dan example.com> writes:
thank you John it worked :)

do I always need do the same for all windows API?
Jun 17 2015
parent reply "John Chapman" <johnch_atms hotmail.com> writes:
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
parent "Dan" <Dan example.com> writes:
thank you so much John :)
Jun 17 2015
prev sibling parent "John Chapman" <johnch_atms hotmail.com> writes:
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