dualtech
asked on
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
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
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.
ASKER
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(mMenuH andler)
Errors:
Error 1 Class 'System.Windows.Forms.Main Menu' cannot be indexed because it has no default property. C:\Users\adamr\Documents\V isual Studio 2008\Projects\FscTest\FscT est\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\V isual Studio 2008\Projects\FscTest\FscT est\Form1. vb 58 20 FscTest
mMenuHandler = Menu.GetMainMenu(nWnd)
mSubMenu = MenuItem.FindHandle(mMenuH
Errors:
Error 1 Class 'System.Windows.Forms.Main
Error 2 Expression is not an array or a method, and cannot have an argument list. C:\Users\adamr\Documents\V
ASKER
I was trying to use direcrt;y GetMenu() dunction up there but It was not working. So I tried this one.
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.
The VB6 approach would have been to use APIs no? You still need to use those same APIs but change up the data types.
ASKER
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
Thank You
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could study the example maybe it would help you down the road
Class1.DoMenuStuff().
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