Solved

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

Posted on 2011-09-27
3
1,375 Views
Last Modified: 2012-05-12
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
Comment
Question by:11ptMan
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 36711359
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
 
LVL 29

Assisted Solution

by:nffvrxqgrcfqvvc
nffvrxqgrcfqvvc earned 250 total points
ID: 36711860
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
 

Author Closing Comment

by:11ptMan
ID: 36897308
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question