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
dualtechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.