In my application i am using SendKeys.SendWait to send text to screen:
SendKeys.SendWait("password");
The text is on English but when the keyboard set to other language the text that SendKeys.SendWait type is set in other language and not in English
Any suggestions how to make sure that the text will set only in English ?
I did a quick test using SendKeys.Send to send text to a couple of input fields. It sends the same text regardless of whether I have the keyboard in English or another language, so I'm not sure why you see a different result. Example:
SendKeys.Send("username");
SendKeys.Send("{TAB}");
SendKeys.Send("påsswørd");
SendKeys.SendWait("{ENTER}");
One possibility is that you could change the keyboard to English temporarily before calling SendKeys, then set it back to whatever it was before. There is an excellent example of the technique in this answer.
Another option is to use Win32 API functions to send messages to the window. The problem will be how to find the right windows to send the text to though. I'm not sure it could be done reliably. Here's an example (not tested):
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, string lParam);
// Windows message constants
const int WM_SETTEXT = 0x000C;
public void DoLogin(string username, string password)
{
// Get handle for current active window
IntPtr hWndMain = GetForegroundWindow();
if (!hWndMain.Equals(IntPtr.Zero))
{
IntPtr hWnd;
// Here you would need to find the username text input window
if ((hWnd = FindWindowEx(hWndMain, IntPtr.Zero, "UserName", "")) != IntPtr.Zero)
// Send the username text to the active window
SendMessage(hWnd, WM_SETTEXT, 0, username);
// Here you would need to find the password text input window
if ((hWnd = FindWindowEx(hWndMain, IntPtr.Zero, "Password", "")) != IntPtr.Zero)
// Send the password text
SendMessage(hWnd, WM_SETTEXT, 0, password);
// Send ENTER key to invoke login
SendKeys.SendWait("{ENTER}");
}
}
Related
I basically use this:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool SetWindowTextW(IntPtr hWnd, string lpString);
SetWindowTextW(HWnd, "лфорфпылвоарпфлыьтвмлафывафыва")
to set the title of a window but the title ends up as:
ð╗Ðäð¥ÐÇÐäð┐Ðïð╗ð▓ð¥ð░ÐÇð┐Ðäð╗ÐïÐîÐéð▓ð╝ð╗ð░ÐäÐïð▓ð░ÐäÐïð▓ð░
I think that this has something to do with incorrect encoding.
Interestingly it seems to work if I type the string into a TextBox and send the property textbox.Text to the same function.
I get a similar string from an API so just typing it in and saving the output is not possible.
(I know the text in the code above is just random characters but the result is a similar mess with actual words)
You can directly use SendMessage to send a WM_SETTEXT message to a foreign Window:
From the Remarks section of SetWindowText:
If the target window is owned by the current process, SetWindowText
causes a WM_SETTEXT message to be sent to the specified window or
control. If the control is a list box control created with the
WS_CAPTION style, however, SetWindowText sets the text for the
control, not for the list box entries.
To set the text of a control in another process, send the WM_SETTEXT
message directly instead of calling SetWindowText.
Charset = CharSet.Auto is used to correctly marshal the string. The target operating system requirements are determined automatically (C# would mark it as ANSI otherwise).
See also: Charsets and marshaling.
[DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
const uint WM_SETTEXT = 0X000C;
IntPtr hWnd = [TheWindowHandle];
IntPtr russianPtr = Marshal.StringToHGlobalUni("лфорфпылвоарпфлыьтвмлафывафыва");
SendMessage(hWnd, WM_SETTEXT, IntPtr.Zero, russianPtr);
Marshal.FreeHGlobal(russianPtr);
Try this, with the L for literal prefixing.
SetWindowTextW(HWnd, L"лфорфпылвоарпфлыьтвмлафывафыва")
I try to simulate button click using sendmessage.
Here is the declaration:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, String lParam);
Here is the code:
// get window handle through change password link
IntPtr objHandle = new IntPtr();
objHandle =getWindowObjHandleByText(ChangePasswordLink,(IntPtr)hwnd);
// click the change password
SendMessage((int)objHandle,0x00F5, 0, null);
The sendmessage works perfect on windows 7 machine but Sendmessae doesnt work on windows10.
Kindly advice.
I am using the following SendMessage function to send/paste text to a different application.
But in that function I have to give the name of the window from the other application.
How can I change this to get the current active window and paste in the code there?
Code:
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public const int WM_PASTE = 0x0302;
IntPtr windowHandle = FindWindow("NOTEPAD", null);
IntPtr editHandle = FindWindowEx(windowHandle, IntPtr.Zero, "EDIT", null);
string textToSendToFile = "Input here your text";
Clipboard.SetText("Test");
SendMessage((int)editHandle, WM_PASTE, 0, textToSendToFile);
I also got this but I do not really know how to combine this with the code above...
[DllImportAttribute("user32.dll", EntryPoint = "GetForegroundWindow")]
public static extern IntPtr GetForegroundWindow();
[DllImportAttribute("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId([InAttribute()] IntPtr hWnd, IntPtr lpdwProcessId);
IntPtr hWndForegroundWindow = GetForegroundWindow();
uint activeThreadID = GetWindowThreadProcessId(hWndForegroundWindow, IntPtr.Zero);
The WM_PASTE message does not use the parameters. It's just an instruction to the recipient to take the contents of the clipboard and paste them. So if you wish the recipient to do anything, you'll need to populate the clipboard first.
If you don't wish to pollute the clipboard, and you should not since it belongs to the user, then you can send an EM_REPLACESEL message passing the text in lParam.
If you want to find the window which the user is currently working on, use GetForegroundWindow.
However, rather than faking low level messages, best of all would be to use the automation API.
Our user panel runs all of our software as services. For whatever reason though, the Mumble voice software creates a popup when you run it with an administrator password set in the command line. Someone posted an alternative by using a bat file that runs two processes as a work around, but is there any way to just suppress the popup message using .NET? I have written a lot of launcher type apps to fix things like this, but I have no idea how I could suppress this message.
Here is what the .bat file looks like from the workaround.
set /p VAR= < superadmin.txt
start murmur2.exe -supw %var%
ping 0.0.0.0 -n 3 > NUL
tskill murmur2
murmur.exe
You can try to monitor open windows and close popup via winapi when it shows up
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
public const int WM_COMMAND = 0x0112;
public const int WM_CLOSE = 0xF060;
int handle = FindWindow(lpClassName, lpWindowName);
SendMessage(handle, WM_COMMAND, WM_CLOSE, 0);
Using Winspector I've found out the ID of the child textbox I want to change is 114. Why isn't this code changing the text of the TextBox?
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
const int WM_SETTEXT = 0x000c;
private void SetTextt(IntPtr hWnd, string text)
{
IntPtr boxHwnd = GetDlgItem(hWnd, 114);
SendMessage(boxHwnd, WM_SETTEXT, 0, text);
}
The following is what I've used successfully for that purpose w/ my GetLastError error checking removed/disabled:
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
public const uint WM_SETTEXT = 0x000C;
private void InteropSetText(IntPtr iptrHWndDialog, int iControlID, string strTextToSet)
{
IntPtr iptrHWndControl = GetDlgItem(iptrHWndDialog, iControlID);
HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, strTextToSet);
}
I've tested this code and it works, so if it fails for you, you need to be sure that you are using the right window handle (the handle of the Dialog box itself) and the right control ID. Also try something simple like editing the Find dialog in Notepad.
I can't comment yet in the post regarding using (char *) but it's not necessary. See the second C# overload in p/Invoke SendMessage. You can pass String or StringBuilder directly into SendMessage.
I additionally note that you say that your control ID is 114. Are you certain WinSpector gave you that value in base 10? Because you are feeding it to GetDlgItem as a base 10 number. I use Spy++ for this and it returns control IDs in base 16. In that case you would use:
IntPtr boxHwnd = GetDlgItem(hWnd, 0x0114);
Please convert your control id (obtained from spy ++) from Hexdecimal Number to Decimal Number and pass that value to the GetDlgItem function.With this
you will get the handle of Text box.This worked for me.
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
const int WM_SETTEXT = 0x000c;
private void SetTextt(IntPtr hWnd, string text)
{
IntPtr boxHwnd = GetDlgItem(hWnd, 114);
SendMessage(boxHwnd, WM_SETTEXT, 0, text);
}
Are you sure you are passing text right? SendMessage last param should be a pointer to char* containing text you want to set.
Look at my "crude hack" of setting text in
How to get selected cells from TDBGrid in Delphi 5
this is done in Delphi 5, where PChar is char* alias, and I simply cast it as int (Integer in Delphi).
You must make sure that "text" is allocated in the external app's memory space. You will not be able to allocate text in the caller app and pass it to another app as each of them will have their own private memory space.