Output LRESULT to console - c++

I'm trying to output text from Notepad window to console and it's always 0.
What I'm doing wrong?
int main()
{
HWND hwnd = (HWND)0x0031019C; // Window Handler of Notepad
char szBuf[4096];
HWND hwndEdit;
LRESULT result;
hwndEdit = FindWindowEx(hwnd, NULL, L"Edit", NULL); // Class for edit box
result = SendMessage(hwndEdit, WM_GETTEXT, sizeof(szBuf) / sizeof(szBuf[0]), (LPARAM)szBuf);
cout<<"Contents: \n"<<result;
cin.get();
return 0;
}
I tried print_f, but it outputs unreadable characters:
printf( "Contents: %s\n", result, szBuf );

It looks to me like you probably have a little bit of a mismatch happening.
Based on the L"Edit", you seem to be doing a Unicode build (otherwise, you'd get an error message about not being able to convert an wchar_t const[5] to LPCSTR, and the code wouldn't compile.
If you do a Unicode build, however, WM_GETTEXT is going to write Unicode data to your buffer, so you need to prepare for and use Unicode instead of narrow characters for your buffer.
For convenience, I've modified it a little to find Notepad instead of using a hard-coded Window handle.
#include <windows.h>
#include <stdio.h>
#define elements(b) (sizeof(b)/sizeof(b[0]))
int main() {
HWND hwnd; // Window Handler of Notepad
wchar_t buf[4096]={0};
HWND hwndEdit;
LRESULT result;
hwnd=FindWindowEx(NULL, NULL, L"Notepad", NULL);
hwndEdit=FindWindowEx(hwnd, NULL, L"Edit", NULL); // Class for edit box
result = SendMessage(hwndEdit, WM_GETTEXT, elements(buf), (LPARAM)buf);
printf("%S", buf);
return 0;
}
I built with:
cl /DUNICODE whatever.cpp user32.lib
Then I did a quick test that printed out exactly the text I'd typed into Notepad. To verify the result, I then edited the text in notepad, ran it again, and it printed out the modified text.

Related

winapi - a standard way to retrieve text from running text editor

Is there a standard message that can be sent to a text editor window or a certain WinApi call, that will retrieve the contents of the currently edited text?
For example, to retrieve the current contents of a Notepad window. (assumed that the most up to date text wasn't yet written to a file)
I've tried retrieving the text via SendMessage using WM_GETTEXT, WM_GETTEXTLENGTH but I was able to retrieve the title text only.
In general no there is no standard message for this.
But Windows' Notepad has an "Edit" child which responds to WM_GETTEXT and WM_GETTEXTLENGTH - messages normally used to retrieve text from input controls.
Here's a PoC demonstrating the idea:
#include <iostream>
#include <vector>
#include <string.h>
#include <Windows.h>
BOOL CALLBACK enumProc(HWND hwnd, LPARAM) {
std::vector<char> buf(100);
GetClassNameA(hwnd, buf.data(), 100);
if (strcmp(buf.data(), "Notepad")) return TRUE;
hwnd = FindWindowEx(hwnd, NULL, "Edit", NULL);
if (!hwnd) return TRUE;
int textLength = SendMessageA(hwnd, WM_GETTEXTLENGTH, 0, 0) + 1;
if (textLength <= 0) return TRUE;
buf.resize(textLength);
SendMessage(hwnd, WM_GETTEXT, textLength, (LPARAM)buf.data());
std::cout << buf.data() << "\n";
return TRUE;
}
int main() {
EnumWindows(&enumProc, 0);
}
Works on Windows 10:

WM_GETTEXT not showing correct text

i am having issues with making an injectable notepad text viewer. I think I may have accessed the hwndEdit HWND wrongly. Currently when i run the program it shows
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
as the text
#include <stdio.h>
#include <iostream>
#include <string>
#include <Windows.h>
int main() {
AllocConsole();
SetConsoleTitleA("Notepad Viewer");
FILE *c;
freopen_s(&c, "CONOUT$", "w", stdout);
freopen_s(&c, "CONIN$", "r", stdin);
DWORD hwndEditAddress = 0x41E1B4;
HWND hwndEdit = *(HWND*)&hwndEditAddress;
for (;;) {
TCHAR text[256];
SendMessage(hwndEdit, WM_GETTEXT, sizeof(text) / sizeof(text[0]), LPARAM(text));
std::cout << "Current Text: " << text;
std::string input;
std::getline(std::cin, input);
}
}
╠ corresponds to the (extended) ASCII code 204, which is 0xCC in hex. A sequence of 0xCC bytes is used by the Visual C++ CRT to mark uninitialized memory. So, your output is basically a dump of some uninitialized memory region.
In your code, you call SendMessge passing the hwndEdit handle; but the logic you used to initialize this handle is unclear and smells of bug:
DWORD hwndEditAddress = 0x41E1B4;
HWND hwndEdit = *(HWND*)&hwndEditAddress;
This code is strange
DWORD hwndEditAddress = 0x41E1B4;
HWND hwndEdit = *(HWND*)&hwndEditAddress;
You may as well write
HWND hwndEdit = (HWND)0x41E1B4;
Which has the exact same effect.
What happens next is that this value is not the value of a window handle. Therefore the attempt to read the text fails, and text is never modified. Finally, you print an uninitialized character array, which is undefined behaviour.
You need to revisit your logic that obtains the window handle.

PostMessage not working for WM_PASTE , mfc

I have an application which has a button on Click of button am trying to paste the text already available to Notepad. My application gathers the text first and puts it on the clipboard(this is working perfectly fine), I am facing problem with the Paste part. Here is the code, Please let me know where am i going wrong.
CWnd *pCwnd = FindWindow(NULL, _T("Untitled - Notepad"));
HWND handle = pCwnd->GetSafeHwnd();
pCwnd->PostMessageA(WM_PASTE,0,0);
I am using Notepad to test it so the name is ("Untitled - Notepad").
Please help me. Thanks in advance.
I don't use MFC, but you can probably translate to what you need. The issue is you need to send the message to the edit control, not the main window.
#include <Windows.h>
#include <string>
#include <cstdlib>
int main()
{
const std::string data("This is some text from the clipboard.");
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, data.size() + 1);
std::memcpy(GlobalLock(hMem), data.c_str(), data.size() + 1);
GlobalUnlock(hMem);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
HWND mainWindow = FindWindow(NULL, "Untitled - Notepad");
HWND editWindow = FindWindowEx(mainWindow, NULL, "edit", NULL);
PostMessage(editWindow, WM_PASTE, 0, 0);
return 0;
}

Strange GetWindowText(); error

I am trying to get the text of a textedit control in a dialog in my Win32 C++ Application.
I am using the following block of code to get that, and also to test it.
HWND hCarRegNo = GetDlgItem( hDlg, IDC_REGNUMBER );
if( hCarRegNo )
{
LPWSTR carRegNo = NULL;
GetWindowText(hCarRegNo, carRegNo, 20);
MessageBox(hDlg, carRegNo, _T("Test"), MB_OK);
}
The MessageBox output is an empty string.
Where is my mistake?
Not allocating any memory for carRegNo. Try this
WCHAR carRegNo[20];
GetWindowText(hCarRegNo, carRegNo, 20);
After some more research, I have solved the problem: instead of LPWSTR, I had to use TCHAR carRegNo[256] to make this work.
HWND hCarRegNo = GetDlgItem( hDlg, IDC_REGNUMBER );
if( hCarRegNo )
{
TCHAR carRegNo[256] = L"";
GetWindowText(hCarRegNo, carRegNo, 256);
MessageBox(hDlg, carRegNo, _T("Test"), MB_OK);
}

How to find the class name & title of a program in c++?

The question is how to find the class name from running programs and title of those programs. I know there already exist some scanning tools like WinDowse or spy++ from visual studio, but what I am asking you is how to make programs like those in our own source code, what function to use, is there some open source program that can help? Code appreciated, link's also :)
Use EnumWindows to enumerate all top-level windows and get their handle.
Pass the handle to GetWindowText and GetClassName to get the window title and window class respectively.
Example:
EnumWindows(EnumProc, 0);
...
BOOL CALLBACK EnumProc(HWND hWnd, LPARAM lParam) {
TCHAR title[256];
TCHAR className[256];
GetWindowText(hWnd, title, 256);
MessageBox(NULL, title, NULL, MB_OK);
GetClassName(hWnd, className, 256);
MessageBox(NULL, className, NULL, MB_OK);
return TRUE;
}