Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I know how to set another program's textbox's text using english letters, but how can I set text using cyrillic letters?

That's how I do it.

C#
SendMessage(hwnd, WM_SETTEXT, 0, "Привет");


I also tried to do:

C#
SendMessage(hwnd, WM_SETTEXT, 0, Encoding.Unicode.GetString(Encoding.ASCII.GetBytes("Привет")))


but that just sets text to "?????"...

EDIT:
Figured it out how to do it.

Needed to assign SendMessage like that:

C#
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int SendMessage(IntPtr hWnd, int msg, int Param, string str);


Instead of:

C#
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int msg, int Param, string str);
Posted
Updated 6-Jun-12 6:57am
v3
Comments
Sergey Alexandrovich Kryukov 6-Jun-12 12:56pm    
Why on Earth?
--SA

1 solution

You are trying to do dirty business, communication between different processes in the outlaw Windows-specific way. Processes are well isolated, and communication via messages is Windows perverted style which was rendered obsolete since the introduction of NT, but supported ever since.

What you are trying cannot work because you do it in non-Unicode way. First of all, the foreign application you are trying to send message to should be compiled to support Unicode, which may or may not be the case; if not, nothing will work. Secondly, you should use "wide" form of SendMessage and marshal the string accordingly. Try this:

C#
using System.Runtime.InteropServices;

//...

const string DllName = "User32.dll";

//...

[DllImport(DllName, EntryPoint="SendMessageW")] // SendMessageW instead of SendMessage
static extern void SendMessage(IntPtr windowHandle, UInt32 message, UInt32 wParam, [MarshalAs(UnmanagedType.LPWStr)] string message);


The numeric types should depend on the target platform. If I'm not much mistaken, all numeric parameters becomes Uint64 on 64-bit instruction-set architectures.

Please try — I did not test it. Anyway, you got the idea.

—SA
 
Share this answer
 
v2
Comments
Joshi, Rushikesh 8-Jun-12 9:47am    
+5, a well descriptive reply. Few years back I was getting same problem to readh external application List View and this was the problem. :)
Sergey Alexandrovich Kryukov 8-Jun-12 10:29am    
Thank you, Joshi.
--SA
dimitrievg 1-Apr-16 17:44pm    
Thanks! It also helped me during solving of analogous problem of cyrillic WM_CHAR. May be I am wrong but it works.
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
public static extern void SendMessageW(IntPtr hWnd, UInt32 msg, [MarshalAs(UnmanagedType.U1)] char wParam, int lParam);
Sergey Alexandrovich Kryukov 1-Apr-16 20:28pm    
Sure.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900