none
How to Load ,and usemenu from resources .dll RRS feed

  • Question

  • Hi All, I want to load the menu from a resource dll and use it .

    Could anyone help me  to transfer the IntPter(I am not sure it is IntPter type) to Windows.Form.Menu  type or things like that that I could use it in windows forms.       


     [DllImport("user32.dll ", SetLastError = true, CharSet = CharSet.Auto)]
    static extern IntPtr LoadMenu(IntPtr Hinstance, string LpMenuName);

    thanks
    the same problem with Y.tks
    • Edited by shoucal Wednesday, September 23, 2009 2:08 AM
    Tuesday, September 22, 2009 10:00 AM

Answers

  • Hello shoucal

    Hans is right that the native menu template resource is not compatibile with .NET menu objects. Visual Studio designer can parse the menu resource embedded in a resource dll simply because VS uses the native PE file interpreter to parse the resource. The interpreter is not related to .NET.

    Here are two solutions in my mind:

    Possible solution1.
    Stick to P/Invoking native APIs to load and display the native menu resource in the winform app. You already P/Invoked LoadMenu, next you may need to P/Invoke TrackPopupMenu if it's a shortcut menu, and finally DestroyMenu.

    Possible solution2.
    The menu resource was generated by a .rc file. I'm aware of some third party tools that can convert .rc file to .NET objects. For example, http://www.dudelabs.com/Home.aspx. This tool is able to parse the native resources in the rc (e.g. the menu resource) and convert them to a Winform class with MenuStrip, ToolStripMenuItem objects.
    If you do not want these tools, you may probably manually do the convertion.

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards,
    Jialiang Ge


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by shoucal Wednesday, September 23, 2009 5:12 AM
    Wednesday, September 23, 2009 4:15 AM
    Moderator

All replies

  • System.Windows.Forms.Menu has no support for this.  It is designed to generate a HMENU (IntPtr) from the menu created by the designer.  It cannot go the other way around.  Decompiling the binary resource back into a Menu and MenuItems is technically possible, you just won't get any help from .NET doing this.

    Raymond Chang blogged about the structure of a menu template.

    Hans Passant.
    Tuesday, September 22, 2009 4:22 PM
    Moderator
  • But when I draft the dll file into the visual studio 2008 ,the menus could see clearly.
    How visual studio 2008 open it ?


    the same problem with Y.tks
    Wednesday, September 23, 2009 3:17 AM
  • Not sure how you draft a DLL.  Visual Studio has a built-in decompiler for resources, I assume that's what you mean.  The C++ IDE has a resource editor.  Nothing you can use in your own program, unmanaged resources are pretty much retired in .NET programming.  Good riddance. 

    It is otherwise quite simple to reproduce an unmanaged menu, shouldn't take more than 15 minutes or so with the WF designer.

    Hans Passant.
    Wednesday, September 23, 2009 4:04 AM
    Moderator
  • Hello shoucal

    Hans is right that the native menu template resource is not compatibile with .NET menu objects. Visual Studio designer can parse the menu resource embedded in a resource dll simply because VS uses the native PE file interpreter to parse the resource. The interpreter is not related to .NET.

    Here are two solutions in my mind:

    Possible solution1.
    Stick to P/Invoking native APIs to load and display the native menu resource in the winform app. You already P/Invoked LoadMenu, next you may need to P/Invoke TrackPopupMenu if it's a shortcut menu, and finally DestroyMenu.

    Possible solution2.
    The menu resource was generated by a .rc file. I'm aware of some third party tools that can convert .rc file to .NET objects. For example, http://www.dudelabs.com/Home.aspx. This tool is able to parse the native resources in the rc (e.g. the menu resource) and convert them to a Winform class with MenuStrip, ToolStripMenuItem objects.
    If you do not want these tools, you may probably manually do the convertion.

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards,
    Jialiang Ge


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by shoucal Wednesday, September 23, 2009 5:12 AM
    Wednesday, September 23, 2009 4:15 AM
    Moderator



  • Thank you very much,Jialiang!
    the same problem with Y.tks
    Wednesday, September 23, 2009 5:12 AM
  • http://resourcelib.codeplex.com/
    Friday, October 9, 2009 7:35 PM