How to handle menu items of an app running in the background from an app in C #

I am working on an application that will have many other tools running in the background. I ran into a problem. During the launch of the background application, it needs to download a specific file (file β†’ load β†’ file name).

Let the front-end Fapp and the background app Bapp . Is it possible for Fapp to get a handle to the Bapp's menu Bapp's and call the load function. I can get a handle for the buttons, but I can't do the same for the menu items.

Now we achieve this with AutoIt, I am trying to achieve this in C # itself.

+4
source share
3 answers

After you get the window handle that you want to call its menu, you can use

  • HMENU GetMenu(HWND) windows api to get the menu
  • HMENU GetSubMenu(HMENU, int) to go to the file menu and open the menu again.
  • BOOL GetMenuItemInfo( ... ) to get menu information
  • and you can use PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM); ( linked post ) to click on this item.

all of these apis are what AutoIt calls (I think). This solution works if your Bapp is a regular Windows application with a regular Windows menu and not a fancy WPF application or ribbon. If so, then what you see as a menu is probably not a menu (technically anyway)

+1
source

Are you sure this is the right way to make two applications talk to each other?

If you do not have the source code for BApp, and it also does not have an API that you can use, then pretending to be an interactive user may be the only way to interact with it. Just keep in mind that this is fraught with problems, think what will happen when

  • BApp not yet launched
  • BApp has an open modal dialogue
  • BApp is in the middle of an operation (or hanging), and its menu is disabled.
  • BApp is updated to a new version and changes to its user interface.
  • The interactive user changes focus in the middle of the operation.

An alternative to this would be the same thing you do when you test the application using the user interface. This is because you are doing the same thing, automating the application, making calls that perform its functions, in this case, to check the results, as expected. Since this WPF message suggests that you are writing an application with MVVM, the best way (to avoid being fragile when changing the user interface) is to ignore the user interface (View) and call the layer below it, i.e. virtual machine (ViewModel).

It’s actually quite simple to add a self-contained WCF connection inside your BApp application so that it can be called from the outside.

  this._host = new ServiceHost(service); this._host.AddServiceEndpoint(typeof(IContract), new NetTcpBinding(), address); this._host.Open(); 

This would allow you to make both speak completely independently.

0
source

If your Bapp is able to somehow call the Win32 API, then this can be achieved by sending a custom WM_USER message to your Fapp - using SendMessage () . In your Fapp, you process this message and take appropriate action.

I don’t think that accessing the control and calling its handler is the right way.

0
source

All Articles