Microsoft KB Archive/187918

= PRB: SendMessage Fails for Some Messages =

Article ID: 187918

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q187918



SYMPTOMS
The Sendmessage API function call will fail to give expected results for some messages when the API Text Viewer declaration is used. For example, the messages "WM_SETTEXT" and "LVM_SCROLL" will fail without returning any error values.



CAUSE
Some messages expect the lParam to be passed as ByVal, while the API Text Viewer declaration passes lParam as ByRef.



RESOLUTION
The declaration can be manually changed after copying the declaration.



STATUS
This behavior is by design.



MORE INFORMATION
When a message expects the lParam as string, then the Sendmessage declaration must be changed to pass lParam as ByVal. Additionally, some messages require integer values to be passed as ByVal. Because the requirements for messages vary, it is necessary to check the documentation for each message for details.

Steps to Reproduce Behavior
 Start a new project. Form1 is created by default. Add a CommandButton and a TextBox to Form1.  Paste the following code in the General Declaration section of Form1: Option Explicit Private Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _        ByVal wParam As Long, lParam As Any) As Long Const WM_SETTEXT = &HC Private lRet As Long

Private Sub Command1_Click lRet = SendMessage(Text1.hwnd, WM_SETTEXT, 0, "hello") End Sub

 Run the project and click Command1. Some garbage is placed in the textbox. However, SendMessage returns 1 indicating that SendMessage has succeeded.  Change the declaration of SendMessage so that the lParam argument is passed ByVal: Private Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _        ByVal wParam As Long, ByVal lParam As Any) As Long

</li> Run the project once more and click Command1. Note that Text1 now contains the text "hello" as expected.</li></ol>

Additional query words: kbDSupport kbDSD kbVBp500 kbVBp kbAPI kbSDKWin32 kbVBp600

Keywords: kbprb KB187918

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.