How to translate this vb-code to vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    How to translate this vb-code to vb.net

    How can I translate this to vb.net?
    I have tried a lot, and basicly I'm stuck at passing the txt variable to sendmessage, since this needs to be an intptr apparently..

    Code:
    Public Function WindowText(window_hwnd As Long) As String
    Dim txtlen As Long
    Dim txt As String
    
        WindowText = ""
        If window_hwnd = 0 Then Exit Function
        
        txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
        If txtlen = 0 Then Exit Function
        
        txtlen = txtlen + 1
        txt = Space$(txtlen)
        txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
        WindowText = Left$(txt, txtlen)
        
        If WindowText = "MOHAA Console" Then
            WindowText = ""
        ElseIf WindowText = "copy" Then
            WindowText = ""
        ElseIf WindowText = "quit" Then
            WindowText = ""
        ElseIf WindowText = "clear" Then
            WindowText = ""
        End If
    findstring$ = WindowText
    FindText TargetPosition + 1
        
    End Function
    thanx
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What exactly is that function doing? Maybe there is a completely different way of doing it?
    It looks like it is just getting the title of a window from a window handle?

    Comment

    • Gangreen
      New Member
      • Feb 2008
      • 98

      #3
      The function will get text in an other window's textbox using it's windowhandle and put it in a variable...

      Public Function WindowText(wind ow_hwnd As Long) As String
      Dim txtlen As Long
      Dim txt As String

      WindowText = ""
      'if the given window handle is zero, the function will exit.
      If window_hwnd = 0 Then Exit Function

      'the length of the text in the windowhandle will be gotten here
      txtlen = SendMessage(win dow_hwnd, WM_GETTEXTLENGT H, 0, 0)
      'If the textlength is zero, meaning there is no text, the function exits.
      If txtlen = 0 Then Exit Function

      txtlen = txtlen + 1
      'This declares some kind of buffer for a string I believe...
      'I'm not sure since I have never programmed in vb, only in .net

      txt = Space$(txtlen)

      'This line is what I'm unable to translate. It will get the text in the window
      '(which is some kind of multiline textbox) and put it in the txtlen variable.
      'Arguments passed to sendmessage: the handle, the command to get text,
      'the textlength, and a reference (?) to the buffer. I do not know how to
      'pass this last parameter to sendmessage in vb.net

      txtlen = SendMessage(win dow_hwnd, WM_GETTEXT, txtlen, ByVal txt)

      'I'm not sure what this does...

      WindowText = Left$(txt, txtlen)

      'don't mind the rest of the function
      If WindowText = "MOHAA Console" Then
      WindowText = ""
      ElseIf WindowText = "copy" Then
      WindowText = ""
      ElseIf WindowText = "quit" Then
      WindowText = ""
      ElseIf WindowText = "clear" Then
      WindowText = ""
      End If
      findstring$ = WindowText
      FindText TargetPosition + 1

      End Function

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        Ok, I figured it out myself, Thanx anyway

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Did you use:
          txtlen = SendMessage(win dow_hwnd, WM_GETTEXT, txtlen, ByRef txt)
          ??
          I would have passed the variable in by reference and then cast it as an IntPtr. (Note: I don't know if that works, but would be what I tried).

          How did you solve it?

          Comment

          • Gangreen
            New Member
            • Feb 2008
            • 98

            #6
            Originally posted by Plater
            Did you use:
            txtlen = SendMessage(win dow_hwnd, WM_GETTEXT, txtlen, ByRef txt)
            ??
            I would have passed the variable in by reference and then cast it as an IntPtr. (Note: I don't know if that works, but would be what I tried).

            How did you solve it?
            Dim txt As StringBuilder = New StringBuilder(l ength)
            SendMessage(rea dbox, WM_GETTEXT, length, txt)

            using a stringbuilder as buffer, it did the trick

            Comment

            Working...