Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetWindowText with char[]

I am quite new to Windows programming. I am trying to retrieve the name of a window.

char NewName[128];
GetWindowText(hwnd, NewName, 128);

I need to use a char[] but it gives me the error of wrong type.
From what I read, LPWSTR is a kind of char*.

How can I use a char[] with GetWindowText ?

Thanks a lot !

like image 645
MaT Avatar asked Dec 19 '12 09:12

MaT


3 Answers

You are probably compiling a Unicode project, so you can either:

  • Explicitly call the ANSI version of the function (GetWindowTextA), or
  • Use wchar_t instead of char (LPWSTR is a pointer to wchar_t)
like image 109
Jonathan Potter Avatar answered Oct 04 '22 03:10

Jonathan Potter


For modern Windows programming (that means, after the year 2000 when Microsoft introduced the Layer for Unicode for Windows 9x), you're far better off using "Unicode", which in C++ in Windows means using wchar_t.

That is, use wchar_t instead of char, and use std::wstring instead of std::string.

Remember to define UNICODE before including <windows.h>. It's also a good idea to define NOMINMAX and STRICT. Although nowadays the latter is defined by default.

like image 27
Cheers and hth. - Alf Avatar answered Oct 04 '22 04:10

Cheers and hth. - Alf


When calling Windows APIs without specifying an explicit version by appending either A (ANSI) or W (wide char) you should always use TCHAR. TCHAR will map to the correct type depending on whether UNICODE is #defined or not.

like image 29
IInspectable Avatar answered Oct 04 '22 03:10

IInspectable