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);
source share