问题描述
基本上假设我有一个 C# MP3 播放器正在运行,我想更改歌曲或只是从命令行调用一些函数.
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.
假设 MP3 播放器名为 mp3.exe
,我想在应用程序本身仍在运行时从应用程序外部更改歌曲.所以我在想,制作另一个名为 mp3call.exe
的应用程序,这样我就可以运行
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>"
和 mp3call.exe
然后会从 mp3 中调用一些方法 Play
,比如 Play()
.
and mp3call.exe
would then call some method Play
from within mp3, like Play(<song_here>)
.
这可以管理吗?如果是这样,如何?
Is this manageable? And if so, how?
推荐答案
我对这个话题进行了一些研究,因为它看起来很有趣.我认为你可以通过使用 Win32 来做到这一点.我做了一个非常非常简单的示例.两个 WPF 应用程序,第一个名为 WpfSender,第二个名为 WpfListener.WpfSender 将向 WpfListener 进程发送消息.
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 只有一个按钮,一旦点击就发送消息.WpfListener 只是一个空窗口,当收到来自 WpfSender 的消息时会显示一个消息框.
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.
这是WpfSender背后的代码
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;
}
}
您使用 Win32 api 跨 Windows 应用程序发送消息
You use Win32 api for sending messages across windows applications
这是 WpfListener 的代码
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;
}
}
我不在这里写 XAML,因为它非常简单.同样,这是一个非常简单的示例,向您展示了如何实现跨应用程序消息发送.极限是你的想象力.您可以声明许多 int 常量,每个常量代表一个动作,然后在 switch 语句中您可以调用选定的动作.
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:
希望这会有所帮助!
这篇关于从另一个 C# 应用程序调用 C# 应用程序中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!