.NET Framework Bookmark and Share   
 index > Common Language Runtime > How to Load ,and usemenu from resources .dll
 

How to Load ,and usemenu from resources .dll

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

Could anyone help me totransferthe IntPter(I am not sure it is IntPter type) to Windows.Form.Menu type orthingslike thatthatI coulduse 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 byshoucal Wednesday, September 23, 2009 12:59 AM
  • Edited byshoucal Wednesday, September 23, 2009 2:08 AM
  •  
shoucal

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 byshoucal Wednesday, September 23, 2009 5:12 AM
  •  
Jialiang Ge [MSFT]
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.
nobugz
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
shoucal
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.
nobugz

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 byshoucal Wednesday, September 23, 2009 5:12 AM
  •  
Jialiang Ge [MSFT]



Thank you very much,Jialiang!
the same problem with Y.tks
shoucal

You can use google to search for other answers

Custom Search

More Threads

• Passing array of binary data from unmanaged to managed...
• Troubles with DowngradeFromWriterLock
• Run .NET executable over the network
• Changing credentials on the current thread
• structs as hastable keys?
• Does the CLS require support for static classes?
• GetVolumeInformation() - Serial Number returns 255???
• Decompile
• Return type as part of a method's signature
• Matching PIA to COM dll/exe?