• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2206
  • Last Modified:

GetMenu(), Getsubmenu(). GetMenuID()

Hello All,

I am trying to access third party software through VB.NET and I am using FindWindow() function for that. I am not able to get the menu function which were available in VB6 in Vb.net Please let me know to get this function I am requirred to import some library or something. or if I am required to add some reference. I have tried searching Web for that but I am not getting any appropriate answer.

Please help me out with this.

Thank you
0
dualtech
Asked:
dualtech
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are you saying you had code that worked in VB6 that doesn't in VB.Net?  For interop, you need to change old VB6 Long types to Integer (or IntPtr) types.
0
 
dualtechAuthor Commented:
Yes I knw I mean to say that the GetMenu function used to work with the Vb6 but in Vb.net they are not working. I knw I need to use Intptr instead of Long.  lemme show you what I am trying to do so you can have idea and I am also adding the error which I am getting in there.

 mMenuHandler = Menu.GetMainMenu(nWnd)
        mSubMenu = MenuItem.FindHandle(mMenuHandler)

Errors:
Error      1      Class 'System.Windows.Forms.MainMenu' cannot be indexed because it has no default property.      C:\Users\adamr\Documents\Visual Studio 2008\Projects\FscTest\FscTest\Form1.vb      57      24      FscTest
Error      2      Expression is not an array or a method, and cannot have an argument list.      C:\Users\adamr\Documents\Visual Studio 2008\Projects\FscTest\FscTest\Form1.vb      58      20      FscTest
0
 
dualtechAuthor Commented:
I was trying to use direcrt;y GetMenu() dunction up there but It was not working. So I tried this one.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Those functions are for manipulation the built-in .Net menu controls...not external applications.

The VB6 approach would have been to use APIs no?  You still need to use those same APIs but change up the data types.
0
 
dualtechAuthor Commented:
I have changed the datatype to Intptr. but in vb6 i had made .BAS file with the definition of the GetMenu and Find WIndow functions. I am not sure that the same module file will work for vb.net or not. Please let me know for vb.net what king of file we need to add for the definition of the function.

Thank You
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can add the declarations to a Module or you an add them to the Form Class itself:

    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function GetMenuItemID Lib "user32" Alias "GetMenuItemID" (ByVal hMenu As Long, ByVal nPos As Long) As Long

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

*If you add them to a Module then change Private to Public.
0
 
nffvrxqgrcfqvvcCommented:
You could study the example maybe it would help you down the road
Class1.DoMenuStuff().
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System.Runtime.ConstrainedExecution
Imports Microsoft.Win32.SafeHandles

Public Class Class1

    <DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Private Shared Function GetMenu(ByVal hWnd As IntPtr) As SafeMenuHandle
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Private Shared Function GetSubMenu(ByVal hMenu As SafeMenuHandle, ByVal nPos As Integer) As SafeMenuHandle
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Private Shared Function GetMenuItemID(ByVal hMenu As SafeMenuHandle, ByVal nPos As Integer) As Integer
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Private Shared Function GetMenuItemCount(ByVal hMenu As SafeMenuHandle) As Integer
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    Private Shared hMenu As SafeMenuHandle          'HMENU
    Private Shared hSubMenu As SafeMenuHandle       'HMENU
    
    Public Shared Sub DoMenuStuff()

        Dim hWnd As IntPtr = IntPtr.Zero
 
        hWnd = FindWindow("Notepad", Nothing)
        If hWnd.ToInt32 = 0 Then
            Console.WriteLine("Invalid hWnd")
            Return
        End If

        hMenu = GetMenu(hWnd)
        If hMenu.IsInvalid Then
            Console.WriteLine("Invalid hMenu")
            Return
        End If
        Console.WriteLine("hMenu Count {0}", GetMenuItemCount(hMenu))

        hSubMenu = GetSubMenu(hMenu, 0)
        If hSubMenu.IsInvalid Then
            Console.WriteLine("Invalid hMenu")
            Return
        End If
        Console.WriteLine("hSubMenu Count {0}", GetMenuItemCount(hSubMenu))
        Console.WriteLine("hSubMenu ID {0}", GetMenuItemID(hSubMenu, 0))

        hMenu.Dispose()
        hSubMenu.Dispose()

    End Sub

End Class

<SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode:=True)> _
<SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)> _
Public Class SafeMenuHandle
    Inherits SafeHandleZeroOrMinusOneIsInvalid
    Private Sub New()
        MyBase.New(True)
    End Sub
    <ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)> _
    Protected Overloads Overrides Function ReleaseHandle() As Boolean
        ' Should be used for cleanup DestroyMenu() not needed
        ' for other processes.
        Console.WriteLine("Disposed  {0}", handle)
    End Function
End Class

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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