Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1714
  • 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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