[RESOLVED] SendMessage What is the correct call to use ?
I am trying to send text to a textbox on another program (which is also mine)
I can get the hwnd of that Textbox reliably.
However I am confused as to the syntax to use in the SendMessage call.
There are a few variations 'out there', but I guess they boil down to these two -
SendMessage hWndTextBox, WM_SETTEXT, 0&, ByVal sMsg
SendMessage hWndTextBox, WM_SETTEXT, Len(sMsg), sMsg
Penagate in his posting
www.vbforums.com/showthread.php?t=336836
uses SendMessage hWndTextBox, WM_SETTEXT, 0&, ByVal CStr(txtMsg.Text)
In this posting
http://www.vbforums.com/showthread.php?t=354746
one of the approaches is suggested, and then the originator (who raised the question) mentions that he had to switch to the other approach to avoid an error
I would appreciate it if someone could tell me which is the correct approach to use.
Re: SendMessage What is the correct call to use ?
PS I just came across this example -
vb Code:
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private Const WM_SETTEXT = &HC
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Form_Load()
Dim lhWnd As Long
lhWnd = FindWindow("Notepad", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0, "Edit", vbNullString)
SendMessageString lhWnd, WM_SETTEXT, 0, "Hi people!"
End Sub
Is there an advantage in using SendMessageString ?
Re: SendMessage What is the correct call to use ?
No. In fact that declaration is wrong as wParam should be a Long and not Integer.
If you look up WM_SETTEXT you will see the wParam is not used.
If you use SendMessageA you should send the string as you have done above.
If you use SendMessageW you should send it like this:
Code:
Private Declare Function SendMessageW Lib "user32" ( _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
' Usage:
SendMessageW(lhWnd, WM_SETTEXT, 0, StrPtr("Hi People!"))
It is advantageous to use the -W form of the function if both applications are natively Unicode (as VB apps are).
Re: SendMessage What is the correct call to use ?
I will be using this only for passing text between my vb pgms, so I will use the example that you just sent ( SendMessageW )
I haven''t seen the StrPtr before, but will give it a go.
Thanks for quick response,
Rob from Cloudy Melbourne
Re: SendMessage What is the correct call to use ?
Actually it's pretty cloudy here too at the moment. :(
On Unicode:
VB stores strings as Unicode internally.
When you call a DLL function, VB converts any strings (types declared as String, or Any) into ASCII. This is fairly expensive and wasteful. To avoid this automatic converstion, we can pass a pointer directly to the string buffer (as above) using StrPtr().
It is always better to use the Unicode form of a function where possible. However, Win 95/98 don't support them (natively) - so you're restricting your application to NT and above.
Re: SendMessage What is the correct call to use ?
We could always argue if it's best to use the W or A versions of any API that requires a string, but I'll leave that discussion to another day... Instead I wanted to go back to your original question.
Quote:
Originally Posted by RobCrombie
However I am confused as to the syntax to use in the SendMessage call.
There are a few variations 'out there', but I guess they boil down to these two -
VB Code:
SendMessage hWndTextBox, WM_SETTEXT, 0&, ByVal sMsg
SendMessage hWndTextBox, WM_SETTEXT, Len(sMsg), sMsg
'[...]
Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Is there an advantage in using SendMessageString ?
Let's discuss the SendMessageString first (besides the fact that the declaration is slightly wrong since it uses an Integer where a Long should be, as already mentioned by Penagate). That is actually not an existing function, the SendMessage functions that exist is SendMessageA and SendMessageW, but VB allows you to put an alias name for an API function. If you look closely the declaration of SendMessageString is actually just an alias for SendMessageA, you could give the function any name you want:
vb Code:
Private Declare Function SetTheText Lib "user32" Alias "SendMessageA" (...
When it comes to the WM_SETTEXT message the wParam parameter is not used and should be set to 0, so passing Len(sMsg) as above is technically wrong, but the value is ignored. It's probably because of a confusion between WM_SETTEXT and WM_GETTEXT that value is there, because with WM_GETTEXT you must pass the length of the buffer which would recieve the text.
As Penagate already has shown, if you want to use the W (wide) version of the function you must declare all the parameters as Long, and pass the pointer of the string instead of the string itself.
Re: SendMessage What is the correct call to use ?
Thanks for shedding more light on that.
I incorrectly thought the name on the right was the Alias, and did not pay much attention to them.
I will be a bit better informed in the future.
Re: [RESOLVED] SendMessage What is the correct call to use ?
Yeah, 'Alias' would be more correctly named 'Aliases' or something as the grammar is a bit misleading.
Re: [RESOLVED] SendMessage What is the correct call to use ?