How to get string from a wchar pointer

Hi all, I am using this function to send some string to my window from another thread.

lpm = create_unicode_buffer(some_string)
SendNotifyMessage(hwnd, MyMessages.THREAD_MSG, 0, addressof(lpm) )

How do I receive string from this pointer at the receiving end ? This is what I have tried so far. But output is some unknown characters.

output = cast(lpm, c_wchar_p).value

What exactly is a “unicode buffer”? What encoding? What is SendNotifyMessage using?

You will need to decode the bytes using whatever encoding is being used at the other end.

Thanks for the reply. create_unicode buffer returns just a wchar array. It’s a ctypes functon. I don’t know much about the encoding method of it. All I have is a pointer to wchar array. It is possible to know the length too. In any other langs, I can use pointer indexing to get the data.

Ah, sorry, I thought this was something from elsewhere! My bad.

(It would be very helpful to include your imports so that we know which parts of your code come from the standard library and which aren’t. I still have no idea what SendNotifyMessage is, so I’m assuming that that’s something custom.)

So, having checked the docs for create_unicode_buffer… this gives back an array. And you’re getting a pointer to that. We then have to assume that SendNotifyMessage carries that across to the receiving end and gives back a pointer. But I’m not 100% sure of what byte encoding is being sent around the place. When I tested it, I was able to get back a valid bytesrting by using c_char_p (not c_wchar_p), but without SendNotifyMessage in the middle, I have no idea how meaningful that is.

It’s worth noting that bytes are the only things that can be sent around, so it might be easier to lean into that and simply send a byte string and decode it on the other side. That way, you’re the one in control of the encoding (and UTF-8 is usually a good choice), which will make life a lot easier.

1 Like

Try decoding as utf16, that is the native Win32 encoding.
You may need to try utf16-be and utf16-le.

1 Like

Thanks for the suggestions. Let me try the byte encoding.

Thanks for the reply. Got the point.