Solved

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

Posted on 2011-09-27
3
1,291 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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now