3

I'm trying to call win32api.SendMessage() via my Python application (i.e. the sending app).

The receiving app's API states that the format of the message is: ::SendMessage(<app_name>, <msg_name>, (WPARAM) <value>)

However, value is actually a string of between 3 to 4 characters (no spaces).

My Question

What is the correct way of using win32api.SendMessage, especially with regard to value?

Can I simply put the string, as in: win32api.SendMessage(<app_name>, <msg_name>, "ABC")?

Or do I need to convert the string to WPARAM type (and if so, how do I do that)?

I've been developing in Linux Python and have very little experience with Windows and C++. Would appreciate any assistance.

Thanks in advance!

P.s. In response to comments, the receiving app is actually AmiBroker and the actual message format given in the API documentation is: ::SendMessage( g_hAmiBrokerWnd, WM_USER_STREAMING_UPDATE, (WPARAM) Ticker, (LPARAM) &recentInfoStructureForGivenTicker ); The 'string' I mentioned earlier is 'Ticker', which the author says is a string (char*). I didn't include that initially as I thought the actual message format is not important.

RESEARCH: I read from this that WPARAM is essentially an integer type, and this led me to win32api. Among the many articles I read; none of them helped to answer my questions above (or at least so I think).

martineau
  • 119,623
  • 25
  • 170
  • 301
NoviceProg
  • 815
  • 1
  • 10
  • 22

2 Answers2

2

[Github]: mhammond/pywin32 - Python for Windows (pywin32) Extensions is a Python wrapper over WINAPIs, and therefore is designed to be Python friendly.

[ActiveState.Docs]: win32api.SendMessage (best doc I could find), is a wrapper over [MS.Docs]: SendMessage function.

The lParam (last) argument is a LONG_PTR, which means that it holds a memory address that can point to anything. Usually this is the one used to pass data like strings.

Since I don't know what message you want to send, I spent some time til I found [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.")

Outcome:

  • Initial state:

    Img0

  • Final state:

    Img1

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\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.

As seen, it works with a normal Python string.

Note: My Win user has "super" administrative privileges. For a normal user, some things might not work as expected.

You could also take a look at [SO]: Keyboard event not sent to window with pywin32 (@CristiFati's answer) for handling WM_CHAR like messages, and more important: how to handle child windows.

@EDIT0:

Added:

  • WM_SETTEXT
  • WM_GETTEXT (Python 2 only) - to show how to get strings back from SendMessage

But since WM_USER_STREAMING_UPDATE is beyond WM_USER (btw, I didn't see any documentation for it), things might / will not work (per @IInspectable's comment, and also SendMessage's documentation), so additional work (data marshaling) would be required.

@EDIT1:

I've already noticed that you're trying to work with AmiBroker (by Googleing WM_USER_STREAMING_UPDATE).
However, I couldn't find any (official) documentation for that message, that would reveal what the WPARAM and LPARAM arguments are supposed to contain (like: [MS.Docs]: WM_SETTEXT message).
Are you trying to write a plugin (meaning you're in the same process with AmiBroker), or you're simply trying to send messages to it (like I did in my example: Python -> Notepad)?

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thanks @CristiFati, I'll take some time to study what you posted. – NoviceProg May 28 '19 at 03:47
  • In Python 3, `win32api.SendMessage` calls wide-character `SendMessageW` (as opposed to `SendMessageA`). This assumes a string parameter is a native UTF-16LE `wchar_t *` string, which PyWin32 gets from Python's C-API function `PyUnicode_AsUnicode`. – Eryk Sun May 28 '19 at 05:51
  • @eryksun, I'm using Python 2.7, so does it mean `win32api.SendMessage` calls `SendMessageA`? Sorry if my question is very noob-ish, all these are shooting over my head. – NoviceProg May 28 '19 at 05:53
  • @NoviceProg, yes in 2.7 it calls `SendMessageA`, even if you pass a `unicode` string. The latter is problematic because, as confirmed in the debugger, it passes a `unicode` string as a UTF-16LE wide-character string to ANSI `SendMessageA`. If you have `unicode` strings, manually encode them as `'mbcs'`. If you need proper Unicode support, upgrade to Python 3, or use ctypes. – Eryk Sun May 28 '19 at 06:05
  • @NoviceProg, after reading your question again, I don't think you need to worry about Unicode support. Use a regular 2.x byte string (i.e. `str` type) for `Ticker`. You may need the `struct` module to pack the data for `recentInfoStructureForGivenTicker`. – Eryk Sun May 28 '19 at 06:17
  • @eryksun, thank you so much! I'll try what you suggested :) – NoviceProg May 28 '19 at 06:28
  • @eryksun: *PyUnicode\_AsUnicode* is atttempted only when *UNICODE* is defined (*Python 3*). For *Python 2*, *PyString\_AS\_STRING* is used. So in both versions normal string work (which is normal), but in *Python 3*, `b"..."` doesn't, and also in *Python 2* `u" ..."` doesn't as well. – CristiFati May 28 '19 at 06:36
  • @CristiFati, in Python 3, if we pass `bytes`, the wrapper can't know that it's supposed to be a string, as opposed to some other packed data, so that's understandable. The issue with `unicode` in Python 2 is not as easily forgiven. It should behave better by dynamically linking to `SendMessageW`. Many of Python's os functions, for example, call the wide-character versions of functions if passed a `unicode` string. – Eryk Sun May 28 '19 at 06:57
  • @eriksun: I'm not sure I get the problem (if there is one). My point is that *PyWin32* makes sure that *SendMessage\** function and and the default string will be in sync (*ASCII* / *Unicode*), so the above code will work. – CristiFati May 28 '19 at 07:14
  • Arguments to `SendMessage` are marshaled by the system for system messages only (`WM_NULL` up to `WM_USER`). The OP seems to want to send a custom message, so this isn't going to work out. – IInspectable May 28 '19 at 13:43
  • @CristiFati, if we pass a `unicode` string in Python 2, it calls ANSI `SendMessageA` with a UTF-16LE wide-character string. This is wrong. It should switch to calling `SendMessageW` in this case. – Eryk Sun May 30 '19 at 03:12
  • @IInspectable, yes and no, in my comment above I mentioned that the OP can simply pass a regular byte string (2.x. `str`) for `Ticker` since the application expects a `char *`. But there's more work to be done using struct or ctypes to pack the data in `recentInfoStructureForGivenTicker`. – Eryk Sun May 30 '19 at 03:14
  • @CristiFati, sorry for my late reply. To your question *"Are you trying to write a plugin (meaning you're in the same process with AmiBroker), or you're simply trying to send messages to it (like I did in my example: Python -> Notepad)?"*, I'm writing the Python app to send the `WM_USER_STREAMING_UPDATE` message to AB – NoviceProg May 31 '19 at 14:36
  • @NoviceProg: did this really answer your question? Cause you don't have to accept an answer just because it's there (or better than other answers). From what I am concerned, there are still open issues regarding your question. – CristiFati Jun 05 '19 at 21:51
  • @CristiFati, I managed to resolve my specific use-case via another way that *totally* avoids the use of Windows messaging. Thus, I didn't continue to pursue this, which, I'd admit, has gone pretty much over my head. But I believe someone who is determined to write an Amibroker DLL would find all your help benefit. Thus, I accepted your answer. – NoviceProg Jun 06 '19 at 18:11
1

If you are sending a custom message then it is perfectly ok to send data in the WPARAM. If you are sending a standard window message then WPARAM should really be set to a value that is correct for the message sent.

Note WPARAM is a 32 bit integer so if you cannot fit your string into 32 bits (AKA 4 bytes) then no you cannot do this. Note if you are sending ASCII this means you can only pass 4 characters (one per byte). I don't know python, but I would imagine you could bit shift the 4 bytes and ADD or OR them into a single 32 bit integer to send as the WPARAM, maybe something like this?

Pseudo code follows

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)))

See the SendMessage function on the Micorsoft website.

https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage

See a large number of other questions about sending a custom message.

Sending a Custom windows message...custom data marshalling

ceorron
  • 1,230
  • 1
  • 17
  • 28
  • Sending *char* arrays packed in *int*s is not the common way of sending strings (as a matter of fact I don't know any case when this is done, do you have a message example?). Instead, `char*` are used. – CristiFati May 27 '19 at 21:53
  • 1
    [`WPARAM`](https://learn.microsoft.com/en-us/windows/desktop/WinProg/windows-data-types#WPARAM) is not a 32-bit integer. It's a [`UINT_PTR`](https://learn.microsoft.com/en-us/windows/desktop/WinProg/windows-data-types#UINT_PTR), i.e. an unsigned integer that's the same size as a pointer. In the x64 ABI it's an `unsigned __int64`. But strings are not packed like this anyway. The kernel component of the window manager (win32k.sys) takes care of marshalling a null-terminated string (`wchar_t *`) message parameter to the process of the thread that owns the target window. – Eryk Sun May 28 '19 at 05:37
  • Ok WPARAM is a UINT_PTR. Which is only 32 bit on 32 bit platforms, my mistake. I don't have an example at all but it is permitted to send any data you like in the WPARAM so long as it is a custom message he is sending. char* is typically used but he is using python don't know if he could do that. If it was c++ then he could. – ceorron May 28 '19 at 22:42