需要 Windows API InsertMenuItem 帮助

发布于 2024-11-28 04:29:51 字数 1453 浏览 0 评论 0原文

我想将新菜单插入其他进程。但我收到一个错误:

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

按钮代码:

    Mmenuhandle = GetMenu(mainhandle) 
    Mmenucount = GetMenuItemCount(Mmenuhandle)
    Smenuhandle = GetSubMenu(Mmenuhandle, 0)
    Smenucount = GetMenuItemCount(Smenuhandle)  
    With mii  
        .cbSize = Len(mii)  
        .fMask = MIIM_STATE Or MIIM_ID Or MIIM_STRING Or MIIM_FTYPE  
        .fType = MFT_STRING  
        .fState = MFS_ENABLED  
        .wID = MENUID  
        .dwTypeData = "My Menu"  
        .cch = Len(.dwTypeData)  
    End With  
    InsertMenuItem(Smenuhandle, Smenucount + 1, True, mii) ' ERROR here  
    DrawMenuBar(mainhandle)  

声明 InsertMenuItem

Private Declare Function InsertMenuItem Lib "user32" Alias "InsertMenuItemA" _
    (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Boolean, ByVal lpmii As MENUITEMINFO) As Integer

声明 MENUITEMINFO

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
    Public a As Integer  
End Structure

如何修复此错误?

I want to insert a new menu into other process. But I get an error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

code for button:

    Mmenuhandle = GetMenu(mainhandle) 
    Mmenucount = GetMenuItemCount(Mmenuhandle)
    Smenuhandle = GetSubMenu(Mmenuhandle, 0)
    Smenucount = GetMenuItemCount(Smenuhandle)  
    With mii  
        .cbSize = Len(mii)  
        .fMask = MIIM_STATE Or MIIM_ID Or MIIM_STRING Or MIIM_FTYPE  
        .fType = MFT_STRING  
        .fState = MFS_ENABLED  
        .wID = MENUID  
        .dwTypeData = "My Menu"  
        .cch = Len(.dwTypeData)  
    End With  
    InsertMenuItem(Smenuhandle, Smenucount + 1, True, mii) ' ERROR here  
    DrawMenuBar(mainhandle)  

declare for InsertMenuItem:

Private Declare Function InsertMenuItem Lib "user32" Alias "InsertMenuItemA" _
    (ByVal hMenu As Integer, ByVal uItem As Integer, ByVal fByPosition As Boolean, ByVal lpmii As MENUITEMINFO) As Integer

declare for MENUITEMINFO:

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
    Public a As Integer  
End Structure

How do I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病女 2024-12-05 04:29:51

P/Invoke 代码不正确...它看起来是从 VB 6 源代码复制的,并且等效名称的数据类型在 VB 6 中与在 VB.NET 中具有非常不同的语义含义。

此外,句柄/指针是使用固定整数类型声明的,这在 64 位环境中无法正常工作。这些类型的值应始终使用专门为此目的设计的 IntPtr 类型来声明。

并且,在 VB.NET 中,需要通过 ByRef 传递指向结构的指针。您无法传递它们 ByVal

您需要使用 System.Runtime 中找到的工具.InteropServices 命名空间 和 .NET 编组器可以帮助您。

这也是为什么您永远不应该在不了解其含义和用途的情况下复制和粘贴在网上找到的代码的另一个原因。

声明应该如下所示:

Imports System.Runtime.InteropServices

Public NotInheritable Class NativeMethods

   Public Const MIIM_STATE As Integer = &H1
   Public Const MIIM_ID As Integer = &H2
   Public Const MIIM_STRING As Integer = &H40
   Public Const MIIM_BITMAP As Integer = &H80
   Public Const MIIM_FTYPE As Integer = &H100

   Public Const MFT_STRING As Integer = &H0

   Public Const MFS_ENABLED As Integer = &H0

   <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 IntPtr
      Public hbmpChecked As IntPtr
      Public hbmpUnchecked As IntPtr
      Public dwItemData As IntPtr
      <MarshalAs(UnmanagedType.LPTStr)> Public dwTypeData As String
      Public cch As Integer
      Public hbmpItem As IntPtr
   End Structure

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
   Public Shared Function GetMenu(ByVal hWnd As IntPtr) As IntPtr
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function GetMenuItemCount(ByVal hMenu As IntPtr) As Integer
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
   Public Shared Function GetSubMenu(ByVal hMenu As IntPtr, ByVal nPos As Integer) As IntPtr
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function InsertMenuItem(ByVal hMenu As IntPtr,
                                         ByVal uItem As Integer,
                                         <MarshalAs(UnmanagedType.Bool)> fByPosition As Boolean,
                                         ByRef lpmii As MENUITEMINFO) _
                                    As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function DrawMenuBar(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function

End Class

然后您可以使用这样的函数(重新编写代码以匹配):

  ' Get a handle to the menu assigned to a window (in this case, your form)
  Dim hMenu As IntPtr = NativeMethods.GetMenu(Me.Handle)

  ' Get a count of the total items in that menu
  Dim menuItemCount As Integer = NativeMethods.GetMenuItemCount(hMenu)

  ' Get a handle to the sub-menu at index 0
  Dim hSubMenu As IntPtr = NativeMethods.GetSubMenu(hMenu, 0)

  ' Get a count of the total items in that sub-menu
  Dim subMenuItemCount As Integer = NativeMethods.GetMenuItemCount(hSubMenu)

  ' Create and fill in a MENUITEMINFO structure, describing the menu item to add
  Dim mii As New NativeMethods.MENUITEMINFO
  With mii
     .cbSize = Marshal.SizeOf(mii)   ' prefer Marshal.SizeOf over the VB 6 Len() function
     .fMask = NativeMethods.MIIM_FTYPE Or NativeMethods.MIIM_STATE Or NativeMethods.MIIM_ID Or NativeMethods.MIIM_STRING
     .fType = NativeMethods.MFT_STRING
     .fState = NativeMethods.MFS_ENABLED
     .wID = 0                        ' your custom menu item ID here
     .hSubMenu = IntPtr.Zero
     .hbmpChecked = IntPtr.Zero
     .hbmpUnchecked = IntPtr.Zero
     .dwItemData = IntPtr.Zero
     .dwTypeData = "My Menu Item"    ' the name of your custom menu item
  End With

  ' Insert the menu item described by the above structure
  ' (notice that we're passing the structure by reference in the P/Invoke definition!)
  NativeMethods.InsertMenuItem(hSubMenu, subMenuItemCount + 1, True, mii)

  ' Force an update of the window's menu bar (again, in this case, your form)
  NativeMethods.DrawMenuBar(Me.Handle)

一切都按预期工作,至少在同一进程中。请注意,P/Invoke 是一个相当困难的主题,您不仅需要对 VB.NET 还需要对 Win32 API 有相当透彻的了解才能使其正常工作。复制和粘贴您在网上找到的代码本质上是有风险的。大多数时候,这是行不通的。其余时间,这可能存在安全风险。不幸的是,您需要的不仅仅是 Stack Overflow 上的答案来向您解释这一切是如何工作的。

编辑:实际上,上面的代码跨进程也可以正常工作。不需要特别努力。我尝试在记事本的运行实例中修改菜单,一切正常。并不是说我建议在没有非常理由的情况下这样做......

The P/Invoke code is incorrect... It looks to be copied from a VB 6 source, and the data types of equivalent names have very different semantic meanings in VB 6 than they do in VB.NET.

In addition, handles/pointers are declared using fixed integer types, which will not work properly in 64-bit environments. These types of values should always be declared using the IntPtr type specifically designed for this purpose.

And, pointers to structures need to be passed ByRef in VB.NET. You can't pass them ByVal.

You need to use the tools found in the System.Runtime.InteropServices namespace and the .NET marshaller to help you out.

This is yet another reason why you should never just copy and paste code that you find online without understanding what it means and what it does.

The declarations should look like this:

Imports System.Runtime.InteropServices

Public NotInheritable Class NativeMethods

   Public Const MIIM_STATE As Integer = &H1
   Public Const MIIM_ID As Integer = &H2
   Public Const MIIM_STRING As Integer = &H40
   Public Const MIIM_BITMAP As Integer = &H80
   Public Const MIIM_FTYPE As Integer = &H100

   Public Const MFT_STRING As Integer = &H0

   Public Const MFS_ENABLED As Integer = &H0

   <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 IntPtr
      Public hbmpChecked As IntPtr
      Public hbmpUnchecked As IntPtr
      Public dwItemData As IntPtr
      <MarshalAs(UnmanagedType.LPTStr)> Public dwTypeData As String
      Public cch As Integer
      Public hbmpItem As IntPtr
   End Structure

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
   Public Shared Function GetMenu(ByVal hWnd As IntPtr) As IntPtr
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function GetMenuItemCount(ByVal hMenu As IntPtr) As Integer
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=False)> _
   Public Shared Function GetSubMenu(ByVal hMenu As IntPtr, ByVal nPos As Integer) As IntPtr
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function InsertMenuItem(ByVal hMenu As IntPtr,
                                         ByVal uItem As Integer,
                                         <MarshalAs(UnmanagedType.Bool)> fByPosition As Boolean,
                                         ByRef lpmii As MENUITEMINFO) _
                                    As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function

   <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
   Public Shared Function DrawMenuBar(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
   End Function

End Class

Then you can use the function like this (re-write your code to match):

  ' Get a handle to the menu assigned to a window (in this case, your form)
  Dim hMenu As IntPtr = NativeMethods.GetMenu(Me.Handle)

  ' Get a count of the total items in that menu
  Dim menuItemCount As Integer = NativeMethods.GetMenuItemCount(hMenu)

  ' Get a handle to the sub-menu at index 0
  Dim hSubMenu As IntPtr = NativeMethods.GetSubMenu(hMenu, 0)

  ' Get a count of the total items in that sub-menu
  Dim subMenuItemCount As Integer = NativeMethods.GetMenuItemCount(hSubMenu)

  ' Create and fill in a MENUITEMINFO structure, describing the menu item to add
  Dim mii As New NativeMethods.MENUITEMINFO
  With mii
     .cbSize = Marshal.SizeOf(mii)   ' prefer Marshal.SizeOf over the VB 6 Len() function
     .fMask = NativeMethods.MIIM_FTYPE Or NativeMethods.MIIM_STATE Or NativeMethods.MIIM_ID Or NativeMethods.MIIM_STRING
     .fType = NativeMethods.MFT_STRING
     .fState = NativeMethods.MFS_ENABLED
     .wID = 0                        ' your custom menu item ID here
     .hSubMenu = IntPtr.Zero
     .hbmpChecked = IntPtr.Zero
     .hbmpUnchecked = IntPtr.Zero
     .dwItemData = IntPtr.Zero
     .dwTypeData = "My Menu Item"    ' the name of your custom menu item
  End With

  ' Insert the menu item described by the above structure
  ' (notice that we're passing the structure by reference in the P/Invoke definition!)
  NativeMethods.InsertMenuItem(hSubMenu, subMenuItemCount + 1, True, mii)

  ' Force an update of the window's menu bar (again, in this case, your form)
  NativeMethods.DrawMenuBar(Me.Handle)

Everything works as expected, at least within the same process. Note that P/Invoke is a fairly difficult topic, and you'll need to have a fairly thorough understanding of not only VB.NET but also the Win32 API to get it to work correctly. Copying and pasting code that you find online is an inherently risky proposition. Most of the time, it won't work. The rest of the time, it's a possible security risk. Unfortunately, you'll need more than an answer on Stack Overflow to explain to you how it all works.

Edit: Actually, the above code works just fine across processes, too. No special effort required. I tried monkeying with the menus in a running instance of Notepad, and everything worked fine. Not that I recommend doing this without a very good reason...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文