Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1928
  • Last Modified:

How can I use the Win32.SendMessage API in VB.Net

I'm converting some C# code to VB.NET and the portion I'm having a issue with are two functions which utilize what i understand are pointers to a variable.
From the searching I've done it I've learned that VB.NET does not support pointers like these.

So what would I need to do in order to utilize the Win32.SendMessage Function in VB.NET
I'm off to research the function itself but if someone has already written code which will help me understand the syntactic differances and save me some time it's worth the points to get it.

Here is the C# Code
#region Scrollbar positions functions
      /// <summary>
      /// Sends a win32 message to get the scrollbars' position.
      /// </summary>
      /// <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns>
      private unsafe Win32.POINT GetScrollPos()
      {
            Win32.POINT res = new Win32.POINT();
            IntPtr ptr = new IntPtr( &res);
            Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr);
            return res;
      }

      /// <summary>
      /// Sends a win32 message to set scrollbars position.
      /// </summary>
      /// <param name="point">a POINT conatining H/Vscrollbar scrollpos.</param>
      private unsafe void SetScrollPos(Win32.POINT point)
      {
            IntPtr ptr = new IntPtr(&point);
            Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ptr);
      }
#endregion


I've converted all but two lines in to VB
#Region "Scrollbar positions functions"
      ''' <summary>
      ''' Sends a win32 message to get the scrollbars' position.
      ''' </summary>
      ''' <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns>
      Private Function GetScrollPos() As Win32.POINT
          Dim res As New Win32.POINT()
          Dim ptr As New IntPtr(AddressOfres)    <-- This line generates an error
          Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr)
          Return res

      End Function

      ''' <summary>
      ''' Sends a win32 message to set scrollbars position.
      ''' </summary>
      ''' <param name="point">a POINT conatining H/Vscrollbar scrollpos.</param>
      Private Sub SetScrollPos(point As Win32.POINT)
          Dim ptr As New IntPtr(New Pointer(Of Win32.POINT)(point))  <-- This line  generates an error
          Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ptr)

      End Sub
#End Region
0
11ptMan
Asked:
11ptMan
2 Solutions
 
käµfm³d 👽Commented:
How can I use the Win32.SendMessage API in VB.Net

The simplest way is to use P\Invoke:  http://pinvoke.net/default.aspx/user32/SendMessage.html

There is a section labelled VB.NET Signature about half-way down the page. Import the System.Runtime.InteropServices namespace and copy/paste the function definition from that site. Then it's simply a matter of calling the function with the correct parameters, which are specified in the function signature  = )
0
 
nffvrxqgrcfqvvcCommented:
You can have more then one signature. The most common way SendMessage() is defined using IntPtr but depending on the message that you are going to be using you can change the last parameter to avoid conversion from structure to pointer. EM_GETSCROLLPOS lParam member expects a pointer to a POINT structure. In this case you know what lParam member is expecting so you can add another P/Invoke signature.
Imports System.Runtime.InteropServices

Public Class Form1

    Private Const EM_GETSCROLLPOS As UInteger = (&H400 + 221)
' Intptr (lParam)
    <DllImport("User32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
' Point (lParam)
    <DllImport("User32.dll", ExactSpelling:=False, SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As IntPtr, ByRef lParam As Point) As IntPtr
    End Function
     
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim pt As Point
        Debug.Print(SendMessage(RichTextBox1.Handle, EM_GETSCROLLPOS, IntPtr.Zero, pt))
        Debug.Print("X= {0}     Y= {1}", pt.X, pt.Y)

    End Sub
     
End Class

Open in new window

0
 
11ptManAuthor Commented:
I thought that I had closed this question but apparently not.

Both of your responses were helpful both provided clues and additional search criteria
and ultimately my continued search led me down several paths but the most efficient resolution I found was in the following artical
http://www.codeproject.com/KB/vb/Marshal.aspx 

After adding System.Runtime.InteropServices (Imports System.Runtime.InteropServices)
I was able to add the following lines to aquire the pointer I needed

 Dim gh As GCHandle = GCHandle.Alloc(res, GCHandleType.Pinned)
 Dim ptr As IntPtr = gh.AddrOfPinnedObject()
    gh.Free()

According to the article


The resulting code looks like this

#Region "Scrollbar positions functions"
        ''' <summary>
        ''' Sends a win32 message to get the scrollbars' position.
        ''' </summary>
        ''' <returns>a POINT structore containing horizontal and vertical scrollbar position.</returns>
        Private Function GetScrollPos() As Win32.POINT
            Dim res As New Win32.POINT()
            ' GC handle instance
            Dim gh As GCHandle = GCHandle.Alloc(res, GCHandleType.Pinned)
            ' get address of variable
            Dim ptr As IntPtr = gh.AddrOfPinnedObject()
            ' Release the Pin
            gh.Free()
            Win32.SendMessage(Handle, Win32.EM_GETSCROLLPOS, 0, ptr)
            Return res

        End Function

        ''' <summary>
        ''' Sends a win32 message to set scrollbars position.
        ''' </summary>
        ''' <param name="point">a POINT conatining H/Vscrollbar scrollpos.</param>
        Private Sub SetScrollPos(point As Win32.POINT)

            'Dim ptr As New IntPtr(New Pointer(Of Win32.POINT)(point))  <-- This line is generates an error
            ' GC handle instance
            Dim gh As GCHandle = GCHandle.Alloc(point, GCHandleType.Pinned)
            ' get address of variable
            Dim ptr As IntPtr = gh.AddrOfPinnedObject()
            ' Release the Pin
            gh.Free()
            Win32.SendMessage(Handle, Win32.EM_SETSCROLLPOS, 0, ptr)

        End Sub
#End Region
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now