WM_GETTEXT 用法
我正在尝试获取应用程序中文本字段的状态。但我无法让它发挥作用。我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
指针概念
文件<< *输出;将打印字符串数组文件的第一个元素
<<输出;打印整个字符串
Pointers concepts
file << *output; will print the first element of the string array
file << output; print the entire string
C# 代码:
对我来说工作正常!
C# code:
Working properly to me!
索菲亚的回答是正确的。但是,现在 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
内容。这就是它存在的原因,这样您就不必经历将指针转换为LPARAM
或WPARAM
的繁琐操作。它更加类型安全,如果您的类型不匹配,尤其是对于 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 awchar_t*
and see what results you get in the buffer.P.S. It is bad form to allocate memory with
malloc
and release it withdelete
. You should either usemalloc
/free
pairs ornew
/delete
pairs. Even safer way to allocate achar
buffer is to usestd::string
or usestd::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 theSendMessage
stuff. That's why it is there so you don't have to go through the rigmarole of casting a pointer to aLPARAM
orWPARAM
. 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 andwchar_t
/char
.