WM_GETTEXT 用法

发布于 2025-01-03 08:53:29 字数 389 浏览 3 评论 0原文

我正在尝试获取应用程序中文本字段的状态。但我无法让它发挥作用。我使用 SendMessage 获取 WM_GETTEXT,并将内容保存到 char *

我将 char * 输出到文件中,但只得到“D”。这就是我现在所拥有的:

LRESULT result;
char * output = (char*)malloc(1024);

result = SendMessage(hwnd,WM_GETTEXT,1024,(LPARAM)output);

ofstream file("test.txt");
file <<  *output;
file.close();

delete [] output;

I'm trying to get the status of a text field in my application. But I don't get it to work. I'm using SendMessage to get WM_GETTEXT, I save the content to a char *.

I output the char * to a file, but I only get "D" back. This is what I have now:

LRESULT result;
char * output = (char*)malloc(1024);

result = SendMessage(hwnd,WM_GETTEXT,1024,(LPARAM)output);

ofstream file("test.txt");
file <<  *output;
file.close();

delete [] output;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2025-01-10 08:53:29

指针概念

文件<< *输出;将打印字符串数组文件的第一个元素

<<输出;打印整个字符串

Pointers concepts

file << *output; will print the first element of the string array

file << output; print the entire string

允世 2025-01-10 08:53:29

C# 代码:

public const uint WM_GETTEXT = 0xD;
const int bufferSize = 10000;
StringBuilder sb = new StringBuilder(bufferSize);
SendMessageGetText(handle, WM_GETTEXT, new UIntPtr(bufferSize), sb);

Console.WriteLine(sb.ToString());

对我来说工作正常!

C# code:

public const uint WM_GETTEXT = 0xD;
const int bufferSize = 10000;
StringBuilder sb = new StringBuilder(bufferSize);
SendMessageGetText(handle, WM_GETTEXT, new UIntPtr(bufferSize), sb);

Console.WriteLine(sb.ToString());

Working properly to me!

许一世地老天荒 2025-01-10 08:53:29

索菲亚的回答是正确的。但是,现在 Visual Studio 项目的默认设置是创建 Unicode 项目。如果您的项目是 Unicode 而不是 MBCS,您只会收到第一个字母。

您是否检查过从 WM_GETTEXT 返回的缓冲区以验证它是否包含整个字符串?

如果没有,请尝试将输出变量声明为 TCHAR* (通用)或 wchar_t* 并查看在缓冲区中得到什么结果。

PS 使用 malloc 分配内存并使用 delete 释放内存是不好的形式。您应该使用 malloc/free 对或 new/delete 对。分配 char 缓冲区的更安全方法是使用 std::string 或使用 std::wstring 来表示宽字符串。

PPS 尝试确保您的项目设置适用于多字节项目而不是 Unicode 项目。那么索菲亚答案中的一切都会起作用。

还有一件事...只需使用 GetWindowText() API 而不是 SendMessage 内容。这就是它存在的原因,这样您就不必经历将指针转换为 LPARAMWPARAM 的繁琐操作。它更加类型安全,如果您的类型不匹配,尤其是对于 Unicode/MBCS 和 wchar_t/char ,它会给您一个编译时错误(比运行时错误更好) 。

Sophia's answer is correct. However, the default now for a Visual Studio project is to create a Unicode project. You will only get the first letter if your project is Unicode and not MBCS.

Have you examined the buffer returned from WM_GETTEXT to verify it has the entire string?

If not, try declaring your output variable as TCHAR* (to be generic) or as a wchar_t* and see what results you get in the buffer.

P.S. It is bad form to allocate memory with malloc and release it with delete. You should either use malloc/free pairs or new/delete pairs. Even safer way to allocate a char buffer is to use std::string or use std::wstring for a wide string.

P.P.S. Try making sure your project settings are for a Multibyte project and not Unicode project. Then everything in Sophia's answer will work.

One more thing... Just use GetWindowText() API instead of the SendMessage stuff. That's why it is there so you don't have to go through the rigmarole of casting a pointer to a LPARAM or WPARAM. It's more typesafe and will give you a compile time error (better than runtime errors) if your types don't match up--especially with Unicode/MBCS and wchar_t/char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文