GetWindowText() error 1400 - c++

I'm trying to get the current active window name with the code below :
HWND winHandle = GetActiveWindow();
wchar_t buffer[512] = L"";
int getT = GetWindowText(winHandle, (LPTSTR) buffer, 511);
When used on the window of the program, I get the window name correctly, otherwise, I'm getting error 1400. What could be the problem ?
Thanks

Error 1400 is ERROR_INVALID_WINDOW_HANDLE according to Microsoft's documentation. This means an invalid HWND is being passed to GetWindowText.
Working backwards, this means GetActiveWindow didn't return a valid handle, probably NULL instead. According to a comment on the documentation for GetActiveWindow this will happen when the active window doesn't belong to the current application or thread.

Related

Problem with reading value of TextEdit Box

Im trying to read the value of a TextEdit box in a windows dialog and display the results using a MessageBox, however the results being displayed is just “Error”, when I run the program, even though there are no exceptions or error message in the debug section in visual studio. What am i doing wrong? Here is the subject code:
LPWSTR path;
GetDlgItemText(hDlg, IDC_PROGRAM, path, sizeof(path));
MessageBox(hDlg, NULL, path, MB_OK);
You are passing an uninitialized pointer and an incorrect buffer size to GetDlgItemText().
You need to allocate memory for a buffer for GetDlgItemText() to write into, and specify the max size of that buffer, eg:
WHAR path[MAX_PATH] = {};
GetDlgItemText(hDlg, IDC_PROGRAM, path, MAX_PATH);
MessageBox(hDlg, NULL, path, MB_OK);

Get text using SendMessage() C++

I am trying to get text from a textbox in a specific window. For this I'm using SendMessage Api function, I dont know if this is the corect way:
SendMessage(hwnd, WM_GETTEXT, 0, 0);
But I dont know how to print the text. For the argument 3 and 4 in msdn site it says: Additional message-specific information. So i dont know if I need to pass something else beside 0. I tried this also:
SendMessage(hwnd, WM_GETTEXT, sizeof(text), LPARAM(text));
But it prints the name of the textbox, I need to retrieve the text inside the box?
How can I do this? Is SendMessage() the correct API function to use ?
Thank you.
edit:
I omit to say, I am enumerating the child windows from a window, and for me it looks like a textbox, where you have to type a name. I am retrieving the username of a Instant messaging window so I cant compare it to a string, is that a textbox ?
You should use GetWindowText. More information here.
Read the MSDN documentation again. It does NOT say "Additional message-specific information" for those parameters:
wParam The maximum number of
characters to be copied, including the
terminating null character.
ANSI applications may have the string
in the buffer reduced in size (to a
minimum of half that of the wParam
value) due to conversion from ANSI to
Unicode.
lParam A pointer to the buffer that
is to receive the text.
This code works for local only:
char *szText;
szText = (char *)GlobalAlloc(GPTR, 255);
SendMessage(hEditControl, WM_GETTEXT, 255, (LPARAM)szText);
MessageBox(hWnd, szText, "It's your message", MB_OK | MB_TOPMOST);
GlobalFree((HGLOBAL)szText);

Not able to get IME Input Context through C++ (ImmGetContext)

Hi I am trying to disable the IME on notepad using the following psuedo code:
MakeNotepadActiveWindow();//Notepad is already open and set to Japanese IME
HWND hwnd = GetTheHWNDForNotepad();
HIMC context = ImmGetContext(hwnd);
if(context == NULL)
printf("context is null %d ",GetLastError());
and the above code is always giving me the null context. GetLastError() gives 0;
Could somebody tell me how to get the InputContext
Just in case somebody else runs into similar problem
You can't get the input context for a window unless you own it.
So you need to call ImmGetInputContext(hwnd) by executing the code in that window's process.

First and last window don't show up

I'm creating a WinApi application for my programming course. The program is supposed to show an LED clock using a separate window for each 'block'. I have figured most of it out, except for one thing: when creating the two-dimensional array of windows, the first and last window never show up. Here's the piece of code from the InitInstance function:
for (int x=0;x<8;x++)
for (int y=0;y<7;y++) {
digitWnd[x][y] = CreateWindowEx((WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_STATICEDGE),
szWindowClass, szTitle, (WS_POPUP| WS_BORDER), NULL, NULL, NULL, NULL, dummyWnd, NULL, hInstance, NULL);
ShowWindow(digitWnd[x][y], nCmdShow);
UpdateWindow(digitWnd[x][y]);
}
The same loop bounds are used everytime I interact with the windows (set position and enable/disable). All the windows seem to be working fine, except for digitWnd[0][0] and digitWnd[7][6]... Any ideas as to what is happening?
Open Spy++ and check if the missing windows are really missing or just overlapped by other windows. It's possible that you have some small error in the position calculations code that puts them behind another window or outside of the screen.
To validate your creation mechanism I would check:
the array initialisation HWND digitWnd[8][7]
if the parent window dummyWnd is valid
the return value of CreateWindowEx() != NULL
Another point which comes to my mind is, that you create windows with dimension 0 - no width or height. So maybe it would be a good idea to set the size within CreateWindowEx(...)
Is this your first call to ShowWindow()? If so, according to MSDN, "nCmdShow: [in] Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow". This could mean that you can fix your program by simply calling ShowWindow() twice. Give it a try and see if it works. Other than that, you'll probably have to provide more of the code for us to look at.

C++ LogFont Embed Into XPS

I am attempting to marry a couple of APIs together to facilitate XPS Printing. As True Type Fonts are sometimes restricted on how they may be used it is suggested that you query the OS (Windows) for the license associated with a font. The proscribed method I've found for doing that looks like this:
HDC hDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
// logfont is a valid instance of LOGFONTW
HGDIOBJ hfont = ::CreateFontIndirect(&logfont);
if (!SelectObject(hDC, hfont))
return;
ULONG privstatus = 0;
LONG ttStatus;
ttStatus = TTGetEmbeddingType(hDC, &privstatus);
At this point ttStatus should be E_NONE if TTGetEmbeddingType succeeded and privstatus should be one of {EMBED_PREVIEWPRINT, EMBED_EDITABLE, EMBED_INSTALLABLE, EMBED_NOEMBEDDING}. I had this example working Friday. Today when I run my executable TTGetEmbeddingType returns 0x0A (E_NOTATRUETYPEFONT) instead of E_NONE. Wat? Am I missing something fundamental about the OS's ability to determine whether a font can be embedded?
The error message text you quoted ("The environment is incorrect") belongs to the ERROR_BAD_ENVIRONMENT system error code, which has a numeric value of 10 (0x0A). However, TTGetEmbeddingType() does not return a system error code. The TTGetEmbeddingType() documentation states:
If successful, returns E_NONE.
This function reads the embedding privileges stored in the font and transfers the privileges to pulPrivStatus.
Otherwise, returns an error code described in Embedding-Function Error Messages.
If you look at the actual definitions in T2embapi.h, a return value of 0x000A is E_NOTATRUETYPEFONT
The specified font is not a TrueType font.