Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-27
3
Medium Priority
?
1,610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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 1000 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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