| |
| Posted by John Chapman in reply to Mark Moorhen | PermalinkReply |
|
John Chapman
Posted in reply to Mark Moorhen
| On Sunday, 4 November 2018 at 19:06:22 UTC, Mark Moorhen wrote:
> Another Windows challenge:
>
> I'm trying to get the title of the active window even if it is from an external application. This is what I've come up with so far:
>
> <D>
> import std.stdio;
> import core.sys.windows.windows;
>
> extern (Windows)
>
> void main()
> {
> HWND foreground = GetForegroundWindow();
> const(wchar) title;
>
> int length = SendMessage(foreground, WM_GETTEXTLENGTH, 0, 0);
> SendMessage(foreground, WM_GETTEXT, length, LPARAM(title)); //LPARAM is a Long Pointer
>
> writeln(length);
> writeln(title);
>
> }
> </D>
>
> Outputs :
>
> 27
> ´┐┐
>
> So the lengt of the foreground windows title should be 27 chars long, but the title is only 3 chars (and kinda funny ones too:-(
>
> Anyone ideas?
You need to allocate some memory to receive the string from WM_GETTEXT.
auto length = SendMessage(foreground, WM_GETTEXTLENGTH, 0, 0);
auto buffer = new wchar[length + 1]; // +1 for the trailing null character
SendMessage(foreground, WM_GETTEXT, buffer.length, cast(LPARAM)buffer.ptr);
auto title = cast(wstring)buffer[0 .. length];
writeln(title);
|