SendMessage, What's Wrong ?

kevin_lam

New member
Joined
Mar 9, 2005
Messages
1
Hi all,

I have been trying to use SendMessage to determine no. of characters in each line within a textbox.

API Declaration
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal wParam As Int32, ByVal lParam As Object) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As System.IntPtr, ByVal wMsg As Long, ByVal wParam As Int32, ByRef lParam As String) As Long
Private Const EM_GETLINE As Int32 = &HC4
Private Const EM_GETLINECOUNT As Int32 = &HBA
Private Const EM_LINEINDEX As Int32 = &HBB
Private Const EM_LINELENGTH As Int32 = &HC1

Code to determine length of each line

Dim lngCount As Long, lngLineIndex As Long, lngLength As Long
Dim i As Int32

Dim strBuffer As String
Get Line count
lngCount = SendMessage(txtContent1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)
txtCount.Text = ""
txtLineCount.Text = ""

With txtContent1
For i = 0 To lngCount - 1
Get line index
lngLineIndex = SendMessage(.Handle, EM_LINEINDEX, i, Nothing)

lngLineIndex ALWAYS return 0 !!

get line length
lngLength = SendMessage(.Handle, EM_LINELENGTH, lngLineIndex, Nothing)

lngLength ALWAYS return length of 1st line

resize buffer
strBuffer = Space(lngLength)
get line text

SendMessageStr(.Handle, EM_GETLINE, i, strBuffer)
Number each line
txtLineCount.Text &= "line " & i & " : " & strBuffer.Length & ", " & lngLength & ControlChars.CrLf
Next
End With

Can someone help me to take a look the code and see whats wrong ?

Thanks :o
 
In your SendMessage declarations, wMsg should be declared as Int32, not Long.

Also, I would be declaring lParam as Int32 and passing zero, rather than declaring it as Object and passing Nothing. What youre doing is probably fine, tho, but itd be something to try after changing wMsg to Int32 if things still dont work.
 
Tygur said:
In your SendMessage declarations, wMsg should be declared as Int32, not Long.

Also, I would be declaring lParam as Int32 and passing zero, rather than declaring it as Object and passing Nothing. What youre doing is probably fine, tho, but itd be something to try after changing wMsg to Int32 if things still dont work.

I just find it odd that its set up for ByVal and not ByRef. If youre going to be passing an arguement to be changed I would pass its reference, not its value. I know C# has the out modifier... Just my thoughts.
 
Back
Top