Results 1 to 9 of 9

Thread: [RESOLVED] SendMessage What is the correct call to use ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Resolved [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.
    Rob C

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: SendMessage What is the correct call to use ?

    PS I just came across this example -

    vb Code:
    1. 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
    2. Private Const WM_SETTEXT = &HC
    3. 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
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5.  
    6. Private Sub Form_Load()
    7.     Dim lhWnd As Long
    8.     lhWnd = FindWindow("Notepad", vbNullString)
    9.     lhWnd = FindWindowEx(lhWnd, 0, "Edit", vbNullString)
    10.     SendMessageString lhWnd, WM_SETTEXT, 0, "Hi people!"
    11. End Sub

    Is there an advantage in using SendMessageString ?
    Rob C

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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).

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    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
    Rob C

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    1. SendMessage hWndTextBox, WM_SETTEXT, 0&, ByVal sMsg
    2. SendMessage hWndTextBox, WM_SETTEXT, Len(sMsg), sMsg
    3. '[...]
    4. 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:
    1. 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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    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.
    Rob C

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] SendMessage What is the correct call to use ?

    or, AliasFor...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width