Python:将文本编码为 win32api.SendMessage() 的 WPARAM
Python: Encode text into WPARAM for win32api.SendMessage()
我正在尝试通过我的 Python 应用程序(即发送应用程序)呼叫 win32api.SendMessage()
。
接收应用程序的 API 指出消息的格式为:::SendMessage(<app_name>, <msg_name>, (WPARAM) <value>
)
然而,value
实际上是一个 3 到 4 个字符(无空格)的字符串。
我的问题
使用 win32api.SendMessage
的正确方法是什么,尤其是 value
?
我可以简单地输入字符串吗,如:
win32api.SendMessage(<app_name>, <msg_name>, "ABC")
?
或者我是否需要将字符串转换为 WPARAM 类型(如果需要,我该怎么做)?
我一直在 Linux Python 进行开发,对 Windows 和 C++ 的经验很少。非常感谢任何帮助。
提前致谢!
P.s。根据评论,接收应用程序实际上是 AmiBroker,API 文档中给出的实际消息格式是:
::SendMessage( g_hAmiBrokerWnd, WM_USER_STREAMING_UPDATE, (WPARAM) Ticker, (LPARAM) &recentInfoStructureForGivenTicker );
我前面说的'string'是'Ticker',作者说是string (char*)
。我最初没有包含它,因为我认为实际的消息格式并不重要。
研究:我从 this that WPARAM is essentially an integer type, and 读到 win32api
。在我阅读的众多文章中; none 他们帮助回答了我上面的问题(或者至少我认为是这样)。
如果您要发送自定义消息,则可以在 WPARAM 中发送数据。如果您要发送标准 window 消息,那么 WPARAM 应该真正设置为适合所发送消息的值。
请注意 WPARAM 是一个 32 位整数,因此如果您不能将您的字符串放入 32 位(又名 4 字节)那么不,您不能这样做。请注意,如果您要发送 ASCII,这意味着您只能传递 4 个字符(每个字节一个)。我不知道 python,但我想您可以对 4 个字节进行位移并将它们添加或或运算为单个 32 位整数以作为 WPARAM 发送,也许是这样的?
伪代码如下
Int32 wparam = 0
wparam = wparam | ((Int32)chr[0] << (32 - (8 * 1)))
wparam = wparam | ((Int32)chr[1] << (32 - (8 * 2)))
wparam = wparam | ((Int32)chr[2] << (32 - (8 * 3)))
wparam = wparam | ((Int32)chr[3] << (32 - (8 * 4)))
请参阅 Micorsoft 网站上的 SendMessage 函数。
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage
查看有关发送自定义消息的大量其他问题。
Sending a Custom windows message...custom data marshalling
[Github]: mhammond/pywin32 - Python for Windows (pywin32) Extensions 是 Python 对 WINAPI 的包装,因此被设计为 Python友好.
[ActiveState.Docs]: win32api.SendMessage (best doc I could find), is a wrapper over [MS.Docs]: SendMessage function.
lParam(最后一个)参数是一个LONG_PTR,这意味着它持有一个可以指向的内存地址任何事情。通常这是用来传递字符串等数据的。
因为我不知道你想发什么信息,我花了一些时间才找到[MS.Docs]: EM_REPLACESEL message。
code0.py:
#!/usr/bin/env python3
import sys
import win32api
import win32gui
import win32con
is_py2 = sys.version_info.major < 3
if is_py2:
_input = input
input = raw_input
def main():
np_wnd = win32gui.FindWindow(None, "Untitled - Notepad")
if not np_wnd:
print("Cound not get Notepad window")
return
np_edit_wnd = win32gui.GetWindow(np_wnd, win32con.GW_CHILD)
if not np_edit_wnd:
print("Cound not get Notepad child window")
return
heading = "After pressing ENTER, "
#'''
input("{:s}all text in Notepad will be selected ... ".format(heading))
# HERE's when the 1st screenshot was taken
win32api.SendMessage(np_edit_wnd, win32con.EM_SETSEL, 0, -1)
replaced_text0 = "Replaced\nmultiline text."
input("{:s}Notepad text will be set (via EM_REPLACESEL) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text0))
win32api.SendMessage(np_edit_wnd, win32con.EM_REPLACESEL, 0, replaced_text0) # Regular string
# HERE's when the 2nd screenshot was taken. It was at the end of the program (at that time), but some stuff was added
replaced_text1 = "Other\nreplaced\n\nnmultiline text."
input("\n{:s}Notepad text will be set (via WM_SETTEXT) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text1))
win32api.SendMessage(np_edit_wnd, win32con.WM_SETTEXT, 0, replaced_text1)
if not is_py2:
return
#'''
print("\nFor Python 2, also get the text back from Notepad")
buf_size = 255
buf = win32gui.PyMakeBuffer(buf_size)
text_len = win32api.SendMessage(np_edit_wnd, win32con.WM_GETTEXT, buf_size, buf)
print(" Original text length: {:d}\n Retrieved text length: {:d}\n Text: \"\"\"\n{:s}\n \"\"\"".format(len(replaced_text1), text_len, buf[:text_len]))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("\nDone.")
结果:
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q056331657]> "e:\Work\Dev\VEnvs\py_064_02.07.15_test0\Scripts\python.exe" code0.py
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
After pressing ENTER, all text in Notepad will be selected ...
After pressing ENTER, Notepad text will be set (via EM_REPLACESEL) to:
"""
Replaced
multiline text.
""" ...
After pressing ENTER, Notepad text will be set (via WM_SETTEXT) to:
"""
Other
replaced
nmultiline text.
""" ...
For Python 2, also get the text from Notepad
Original text length: 32
Retrieved text length: 32
Text: """
Other
replaced
nmultiline text.
"""
Done.
正如所见,它适用于正常的 Python 字符串。
注意:我的 Win 用户拥有 "super" 管理权限。对于普通用户,某些功能可能无法按预期工作。
您还可以查看 来处理 WM_CHAR 之类的消息,更重要的是:如何处理 child windows.
@EDIT0:
已添加:
- WM_SETTEXT
- WM_GETTEXT(仅 Python 2)- 展示如何从 发送消息
但是由于 WM_USER_STREAMING_UPDATE 超出了 WM_USER (btw,我没有看到它的任何文档),事情可能/不会工作(根据@IInspectable 的评论,以及 SendMessage 的文档),所以额外的工作(数据编组)将是必需的。
@EDIT1:
我已经注意到您正在尝试与 AmiBroker 合作(通过 Googleing WM_USER_STREAMING_UPDATE)。
但是,我找不到该消息的任何(官方)文档,它会揭示 WPARAM 和 LPARAM 参数的内容应该包含(如:[MS.Docs]: WM_SETTEXT message)。
您是在尝试编写插件(意味着您与 AmiBroker 处于同一进程中),还是只是想向其发送消息(就像我在示例中所做的那样:Python -> 记事本)?
我正在尝试通过我的 Python 应用程序(即发送应用程序)呼叫 win32api.SendMessage()
。
接收应用程序的 API 指出消息的格式为:::SendMessage(<app_name>, <msg_name>, (WPARAM) <value>
)
然而,value
实际上是一个 3 到 4 个字符(无空格)的字符串。
我的问题
使用 win32api.SendMessage
的正确方法是什么,尤其是 value
?
我可以简单地输入字符串吗,如:
win32api.SendMessage(<app_name>, <msg_name>, "ABC")
?
或者我是否需要将字符串转换为 WPARAM 类型(如果需要,我该怎么做)?
我一直在 Linux Python 进行开发,对 Windows 和 C++ 的经验很少。非常感谢任何帮助。
提前致谢!
P.s。根据评论,接收应用程序实际上是 AmiBroker,API 文档中给出的实际消息格式是:
::SendMessage( g_hAmiBrokerWnd, WM_USER_STREAMING_UPDATE, (WPARAM) Ticker, (LPARAM) &recentInfoStructureForGivenTicker );
我前面说的'string'是'Ticker',作者说是string (char*)
。我最初没有包含它,因为我认为实际的消息格式并不重要。
研究:我从 this that WPARAM is essentially an integer type, and win32api
。在我阅读的众多文章中; none 他们帮助回答了我上面的问题(或者至少我认为是这样)。
如果您要发送自定义消息,则可以在 WPARAM 中发送数据。如果您要发送标准 window 消息,那么 WPARAM 应该真正设置为适合所发送消息的值。
请注意 WPARAM 是一个 32 位整数,因此如果您不能将您的字符串放入 32 位(又名 4 字节)那么不,您不能这样做。请注意,如果您要发送 ASCII,这意味着您只能传递 4 个字符(每个字节一个)。我不知道 python,但我想您可以对 4 个字节进行位移并将它们添加或或运算为单个 32 位整数以作为 WPARAM 发送,也许是这样的?
伪代码如下
Int32 wparam = 0
wparam = wparam | ((Int32)chr[0] << (32 - (8 * 1)))
wparam = wparam | ((Int32)chr[1] << (32 - (8 * 2)))
wparam = wparam | ((Int32)chr[2] << (32 - (8 * 3)))
wparam = wparam | ((Int32)chr[3] << (32 - (8 * 4)))
请参阅 Micorsoft 网站上的 SendMessage 函数。
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage
查看有关发送自定义消息的大量其他问题。
Sending a Custom windows message...custom data marshalling
[Github]: mhammond/pywin32 - Python for Windows (pywin32) Extensions 是 Python 对 WINAPI 的包装,因此被设计为 Python友好.
[ActiveState.Docs]: win32api.SendMessage (best doc I could find), is a wrapper over [MS.Docs]: SendMessage function.
lParam(最后一个)参数是一个LONG_PTR,这意味着它持有一个可以指向的内存地址任何事情。通常这是用来传递字符串等数据的。
因为我不知道你想发什么信息,我花了一些时间才找到[MS.Docs]: EM_REPLACESEL message。
code0.py:
#!/usr/bin/env python3
import sys
import win32api
import win32gui
import win32con
is_py2 = sys.version_info.major < 3
if is_py2:
_input = input
input = raw_input
def main():
np_wnd = win32gui.FindWindow(None, "Untitled - Notepad")
if not np_wnd:
print("Cound not get Notepad window")
return
np_edit_wnd = win32gui.GetWindow(np_wnd, win32con.GW_CHILD)
if not np_edit_wnd:
print("Cound not get Notepad child window")
return
heading = "After pressing ENTER, "
#'''
input("{:s}all text in Notepad will be selected ... ".format(heading))
# HERE's when the 1st screenshot was taken
win32api.SendMessage(np_edit_wnd, win32con.EM_SETSEL, 0, -1)
replaced_text0 = "Replaced\nmultiline text."
input("{:s}Notepad text will be set (via EM_REPLACESEL) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text0))
win32api.SendMessage(np_edit_wnd, win32con.EM_REPLACESEL, 0, replaced_text0) # Regular string
# HERE's when the 2nd screenshot was taken. It was at the end of the program (at that time), but some stuff was added
replaced_text1 = "Other\nreplaced\n\nnmultiline text."
input("\n{:s}Notepad text will be set (via WM_SETTEXT) to: \n\"\"\"\n{:s}\n\"\"\" ... ".format(heading, replaced_text1))
win32api.SendMessage(np_edit_wnd, win32con.WM_SETTEXT, 0, replaced_text1)
if not is_py2:
return
#'''
print("\nFor Python 2, also get the text back from Notepad")
buf_size = 255
buf = win32gui.PyMakeBuffer(buf_size)
text_len = win32api.SendMessage(np_edit_wnd, win32con.WM_GETTEXT, buf_size, buf)
print(" Original text length: {:d}\n Retrieved text length: {:d}\n Text: \"\"\"\n{:s}\n \"\"\"".format(len(replaced_text1), text_len, buf[:text_len]))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("\nDone.")
结果:
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q056331657]> "e:\Work\Dev\VEnvs\py_064_02.07.15_test0\Scripts\python.exe" code0.py Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32 After pressing ENTER, all text in Notepad will be selected ... After pressing ENTER, Notepad text will be set (via EM_REPLACESEL) to: """ Replaced multiline text. """ ... After pressing ENTER, Notepad text will be set (via WM_SETTEXT) to: """ Other replaced nmultiline text. """ ... For Python 2, also get the text from Notepad Original text length: 32 Retrieved text length: 32 Text: """ Other replaced nmultiline text. """ Done.
正如所见,它适用于正常的 Python 字符串。
注意:我的 Win 用户拥有 "super" 管理权限。对于普通用户,某些功能可能无法按预期工作。
您还可以查看
@EDIT0:
已添加:
- WM_SETTEXT
- WM_GETTEXT(仅 Python 2)- 展示如何从 发送消息
但是由于 WM_USER_STREAMING_UPDATE 超出了 WM_USER (btw,我没有看到它的任何文档),事情可能/不会工作(根据@IInspectable 的评论,以及 SendMessage 的文档),所以额外的工作(数据编组)将是必需的。
@EDIT1:
我已经注意到您正在尝试与 AmiBroker 合作(通过 Googleing WM_USER_STREAMING_UPDATE)。
但是,我找不到该消息的任何(官方)文档,它会揭示 WPARAM 和 LPARAM 参数的内容应该包含(如:[MS.Docs]: WM_SETTEXT message)。
您是在尝试编写插件(意味着您与 AmiBroker 处于同一进程中),还是只是想向其发送消息(就像我在示例中所做的那样:Python -> 记事本)?