SetText of a text field in an external application. Win32 API

Using the Winspector I found that the text field ID for the children I want to change is 114. Why doesn't this code change 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); } 
+4
source share
4 answers

The following is what I successfully used for this purpose with error checking GetLastError 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 tested this code and it works, so if it doesn’t work for you, you should be sure to use the handle to the right window (handle to the dialog window itself) and the right identifier of the control. Also try something simple, for example, edit the Search dialog in Notepad.

I still can not comment on the usage message (char *), but this is not necessary. See Second C # Overload in p / Invoke SendMessage . You can pass a String or StringBuilder directly to SendMessage.

Also, note that you are saying that your management identifier is 114. Are you sure WinSpector gave you this value in base 10? Because you submit it to GetDlgItem as base number 10. I use Spy ++ for this, and it returns the control identifiers in base 16. In this case, you will use:

 IntPtr boxHwnd = GetDlgItem(hWnd, 0x0114); 
+7
source

Are you sure you are conveying the text correctly? The last SendMessage parameter should be a pointer to a char * containing the text you want to set.
Look at my “rough hack” in setting up text in How to get selected cells from TDBGrid in Delphi 5
this is done in Delphi 5, where PChar is a char * alias, and I just use it as an int (Integer in Delphi).

0
source

You must make sure that the "text" is allocated in the external memory space of the application. You will not be able to select text in the recipient application and transfer it to another application, since each of them will have its own free space.

0
source

Please convert your control identifier (derived from spy ++) from a hexadecimal number to a decimal number and pass this value to the GetDlgItem function. With this
you will get a text field handle. It 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); } 
0
source

All Articles