0

Basically let's say I have a C# MP3 player running, and I want to change the song or just call some function from the command line.

Assuming the MP3 player is named mp3.exe, I want to change the song from outside the app while the app itself is still running. So I was thinking, make another app called mp3call.exe, so I can run

mp3call -play "<song_here>"

and mp3call.exe would then call some method Play from within mp3, like Play(<song_here>).

Is this manageable? And if so, how?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • https://msdn.microsoft.com/en-us/library/aa730857(v=vs.80).aspx – Hans Passant May 29 '17 at 18:04
  • hmmm you can pass arguments to the app , build messaging between the two applications using some mechanism, expose an API from the application as a shared library, .... – niceman May 29 '17 at 18:04
  • @HansPassant your link is old – niceman May 29 '17 at 18:05
  • 1
    @niceman we might be old but at least we know that stuff works ... – rene May 29 '17 at 18:06
  • @rene all I wanted to say is that [this](https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx) link is a better link to the same principle : WCF – niceman May 29 '17 at 18:08
  • You should have asked about the problem you are trying to solve, rather than the solution you have in mind (which isn't). Yes, it is possible to expose an automation interface to an application. Many ways to do that. – IInspectable May 29 '17 at 21:16
  • If you have control over the other program, you have lots of options. See marked duplicates for some of them. If you don't, your options a much more limited, and you may not be able to do what you want. If it's possible at all, the best approach will use "UI automation". You can search on those terms to learn more. – Peter Duniho May 30 '17 at 00:05

1 Answers1

1

I have researched a little about the topic since it seems interesting. I think you could do this by using Win32. I have made a very very simple sample. Two WPF applications, first named WpfSender and second named WpfListener. WpfSender will send a message to WpfListener process.

WpfSender only has one button that send the message once it is clicked. WpfListener is only an empty window that will show a message box when receiving the message from WpfSender.

enter image description here

Here is the code behind for WpfSender

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;


namespace WpfSender
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var process = Process.GetProcessesByName("WpfListener").FirstOrDefault();
            if (process == null)
            {
                MessageBox.Show("Listener not running");
            }
            else
            {
                SendMessage(process.MainWindowHandle, RF_TESTMESSAGE, IntPtr.Zero, IntPtr.Zero);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private const int RF_TESTMESSAGE = 0xA123;
    }
}

You use Win32 api for sending messages across windows applications

Here is the code for WpfListener

using System;
using System.Windows;
using System.Windows.Interop;


namespace WpfListener
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            source.AddHook(WndProc);
        }

        private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {

            if (msg == RF_TESTMESSAGE)
            {
                MessageBox.Show("I receive a msg here a I can call the method");
                handled = true;
            }
            return IntPtr.Zero;
        }

        private const int RF_TESTMESSAGE = 0xA123;
    }
}

I don not write here the XAML since it is very simple. Again this is a very simple sample that shows you how to achieve cross application message sending. The limit is your imagination. You can declare many int constants each one representing an action, an then in a switch statement you can call selected action.

I have to say that I follow two articles that I found in my research:

For knowing how to handle WndProc in Wpf

For knowing how to send messages using win32 api

Hope this helps!

taquion
  • 2,667
  • 2
  • 18
  • 29