2 Replies - 933 Views - Last Post: 08 February 2013 - 04:03 PM Rate Topic: -----

#1 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 215
  • View blog
  • Posts: 726
  • Joined: 01-February 13

Can`t get text using GetMenuItemInfo

Posted 07 February 2013 - 03:04 PM

Hi Everyone,
I need to get the menu text from an external window. I can get the Enabled/Disabled state and the Checked/Unchecked state without a problem but, when it comes to the text all i can get is something like this (並睥䌉牴⭬). I have been all over the net looking for answers and have tried everything i can think of with no success. The way i set the string buffer size may be the problem but,i am not sure. It works with the GetMenuString API but, that is obsolete. It seems to get the text length ok but, not the text. This is an example i made so anyone can test it by opening up notepad and running this. I am hoping i am just missing something stupid. Does anyone have any idea of what it could be ?

Module1.vb
Imports System.Runtime.InteropServices

Module Module1
    Public Const MFT_STRING = &H0
    Public Const MIIM_TYPE = &H10

    <DllImport("user32.dll")> Public Function GetMenu(ByVal hWnd As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll")> Public Function GetSubMenu(ByVal hMenu As IntPtr, ByVal nPos As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Public Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    Public Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As IntPtr, ByVal unit As Integer, ByVal byPosition As Boolean, ByRef lpMenuItemInfo As MENUITEMINFO) As Int32

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure MENUITEMINFO
        Public cbSize As Integer
        Public fMask As Integer
        Public fType As Integer
        Public fState As Integer
        Public wID As Integer
        Public hSubMenu As Integer
        Public hbmpChecked As Integer
        Public hbmpUnchecked As Integer
        Public dwItemData As Integer
        Public dwTypeData As String
        Public cch As Integer
    End Structure
End Module



Form1.vb
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hWnd As IntPtr = FindWindow(Nothing, "untitled - notepad")
        Dim hSubMenu As IntPtr = GetSubMenu(GetMenu(hWnd), 0)

        Dim menuinfo As New MENUITEMINFO
        menuinfo.cbSize = Len(menuinfo)
        menuinfo.fMask = MIIM_TYPE
        menuinfo.fType = MFT_STRING
        menuinfo.cch = 0
        menuinfo.dwTypeData = ""

        '1st call to get the text length into (cch)
        GetMenuItemInfo(hSubMenu, 0, True, menuinfo)
        MessageBox.Show(menuinfo.cch.ToString)

        'Set the length of the buffer to cch + 1
        menuinfo.dwTypeData = Space(menuinfo.cch + 1)

        '2nd call to get the text into (dwTypeData)
        GetMenuItemInfo(hSubMenu, 0, True, menuinfo)

        MessageBox.Show(menuinfo.dwTypeData)
    End Sub

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Can`t get text using GetMenuItemInfo

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 215
  • View blog
  • Posts: 726
  • Joined: 01-February 13

Re: Can`t get text using GetMenuItemInfo

Posted 07 February 2013 - 10:07 PM

I`m back,
Well after another long day of reading and testing i found my problem and it was a simple mistake. I was using GetMenuItemInfoA which uses a 1-byte character ANSI format which is normally used for older windows 95 and 98 platforms when i should have been using the GetMenuItemInfoW which uses a 2-byte character Unicode format used for newer windows platforms.

Changed this line in Module1.vb :
    Public Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoW" (ByVal hMenu As IntPtr, ByVal unit As Integer, ByVal byPosition As Boolean, ByRef lpMenuItemInfo As MENUITEMINFO) As Int32



Now i have a another problem. The string being returned to (menuinfo.dwTypeData) is one character short. I tried setting the buffer size to (cch + 2) but, i get the same string. I think maybe it is something to do with passing the managed and unmanaged variables back and forth but, i`m not sure. Does anyone have any idea what i am overlooking now?

I didn`t know if i should open another thread for the new problem because it is kinda the same problem.

So Close but, yet so far away. :wacko:
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 215
  • View blog
  • Posts: 726
  • Joined: 01-February 13

Re: Can`t get text using GetMenuItemInfo

Posted 08 February 2013 - 04:03 PM

I`m back,
I got some help from Viorel_ that belongs to the msdn Visual Basic Forum. Once again it was just something simple that i was overlooking. I new i had to increment cch by 1 but the way i was doing it was just expanding the size of the dwTypeData string buffer that holds the text. It was not actually telling the structure to get the 1 extra character to put into the dwTypeData string buffer. So i wanted to let everyone know that it is resolved.

Change this part of the code in Form1.vb :
        'Set the length of the buffer to cch + 1
        menuinfo.cch += 1
        menuinfo.dwTypeData = Space(menuinfo.cch)



Sometimes it is the simple things that you overlook all day long that are the problem. :detective:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1