@BrendanE - Here's what I have so far. It's not complete but it is 85% functional in that not all menu items are being detected at this time. Tested and working on WinXP with PB3.91F
I'm posting this so you can see what is working for me and maybe it will give you a clue as to what might be going wrong in your code.
This is my dll...
mnutxt.dllCode: Select all
ProcedureDLL MyMessageProc(code.l, wParam.l, lParam.l)
#MIIM_ID = $2
#MIIM_TYPE = $10
#MF_POPUP = $10
#MSGF_MENU = $2
#WM_MENUSELECT = $11F
winText$ = Space(256) ; init buffer for window text
mItem.MENUITEMINFO ; Structure for menu info
mItem\cbSize = SizeOf(MENUITEMINFO) ; required
mItem\fMask = #MIIM_TYPE | #MIIM_ID ; mask flag(s)
hMyWin = FindWindow_("WindowClass_0","Get_Menu_Info") ; our window for displaying results
*mymsg.MSG = lParam ; MSG structure holds info
menuMessage = *mymsg\message ; get menu message
If code = #MSGF_MENU And menuMessage = #WM_MENUSELECT ; only concerned with menu being selected
hMenu = *mymsg\lParam ; handle to menu
menuType = *mymsg\wParam>>16 & $FFFF ; hiword of wparam = menuitem with or without submenu?
menuIndex = *mymsg\wParam & $FFFF ; loword of wparam = menu index or menuitem ID
If menuType&#MF_POPUP <>0 ; determine if menuitem is a popup
subYN.l = #True ; item is a popup
Else
subYN.l = #False ; item is not a popup
EndIf
menuText$ = "" ; init string holder to null
mItem\dwTypeData = @menuText$ ; init null string into mItem
GetMenuItemInfo_(hMenu, menuIndex, subYN, @mItem) ; first call gets the string size
menuText$ = Space(mItem\cch+1) ; add 1 to string size
mItem\dwTypeData = @menuText$ ; put sized string into mItem
mItem\cch +1 ; add one to string size (SDK says so)
GetMenuItemInfo_(hMenu, menuIndex, subYN, @mItem) ; second call puts the text into mItem
GetWindowText_(*mymsg\hwnd, @winText$, 256) ; get text of calling window for display
hWinHndGadget = ChildWindowFromPoint_(hMyWin, 120, 10) ; locate TextGgadget to display window handle
SendMessage_(hWinHndGadget, #WM_SETTEXT, 0, Str(*mymsg\hwnd)) ; send window handle to my app gadget
hWinTxtGadget = ChildWindowFromPoint_(hMyWin, 120, 30) ; locate TextGadget to display window text
SendMessage_(hWinTxtGadget, #WM_SETTEXT, 0, winText$) ; send window text to my app gadget
hMnuIdGadget = ChildWindowFromPoint_(hMyWin, 120, 70) ; locate TextGadget to display Menu ID
SendMessage_(hMnuIdGadget, #WM_SETTEXT, 0, Str(mItem\wID)) ; send menu id to my app gadget
hMenuTxtGadget = ChildWindowFromPoint_(hMyWin, 120, 90) ; locate TextGadget to display Menu text
SendMessage_(hMenuTxtGadget, #WM_SETTEXT, 0, menuText$) ; send menu text to my app gadget
EndIf
ProcedureReturn CallNextHookEx_(@MyMessageProc(), code, wParam, lParam)
EndProcedure
And my main app. Not much to look at but then this is just a learn-to-hook project for me.
menuhook.pbCode: Select all
Enumeration
#Window_0
EndEnumeration
Enumeration
#Window_Handle_Label
#Window_Handle
#Window_Text_Label
#Window_Text
#Menu_ID_Label
#Menu_ID
#Menu_Text_Label
#Menu_Text
#Button_Hook
#Button_Quit
EndEnumeration
If OpenWindow(#Window_0, 0, 0, 550, 160, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "Get_Menu_Info")
If CreateGadgetList(WindowID(0))
TextGadget(#Window_Handle_Label, 10, 10, 100, 20, "Window Handle:")
TextGadget(#Window_Handle, 120, 10, 420, 20, "")
TextGadget(#Window_Text_Label, 10, 30, 100, 20, "Window Text:")
TextGadget(#Window_Text, 120, 30, 420, 40, "")
TextGadget(#Menu_ID_Label, 10, 70, 100, 20, "Menu Item ID:")
TextGadget(#Menu_ID, 120, 70, 420, 20, "")
TextGadget(#Menu_Text_Label, 10, 90, 100, 20, "Menu Item Text:")
TextGadget(#Menu_Text, 120, 90, 420, 20, "")
ButtonGadget(#Button_Hook, 10, 130, 150, 20, "Start hooking menus")
ButtonGadget(#Button_Quit, 470, 130, 70, 20, "Quit")
EndIf
SetWindowPos_(WindowID(#Window_0),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) ; keep window on top
EndIf
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadgetID()
Case #Button_Hook
If GetGadgetText(#Button_Hook) = "Start hooking menus"
hDLL = OpenLibrary(0, "mnutxt.dll")
If hDLL
hmyHook = SetWindowsHookEx_(#WH_MSGFILTER, GetProcAddress_(hDLL, "MyMessageProc"), hDLL, 0)
SetGadgetText(#Button_Hook, "Stop hooking menus")
Else
MessageRequester("Error", "Not able to open file: mnutxt.dll", #MB_ICONERROR)
EndIf
ElseIf EventGadgetID() = #Button_Hook And GetGadgetText(#Button_Hook) = "Stop hooking menus"
UnhookWindowsHookEx_(hmyHook)
CloseLibrary(0)
hDLL = #False
SetGadgetText(#Window_Handle, "")
SetGadgetText(#Window_Text, "")
SetGadgetText(#Menu_ID, "")
SetGadgetText(#Menu_Text, "")
SetGadgetText(#Button_Hook, "Start hooking menus")
EndIf
Case #Button_Quit
quit = #True
EndSelect
Case #PB_Event_CloseWindow
quit = #True
EndSelect
Until quit = #True
UnhookWindowsHookEx_(hmyHook)
CloseLibrary(0)
hDLL = #False
End