C# 从DLL控制WPF应用程序(进程间通信)
我有一个dll,它必须与非C语言编写的程序通信。我已经建立了与本规范的联系:C# 从DLL控制WPF应用程序(进程间通信),c#,.net,wpf,dll,C#,.net,Wpf,Dll,我有一个dll,它必须与非C语言编写的程序通信。我已经建立了与本规范的联系: namespace ClassLibrary1 { public class Class1 { [DllExport("teststring", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.BS
namespace ClassLibrary1
{
public class Class1
{
[DllExport("teststring", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
public static String TestString(string test)
{
return "Test" + test;
}
public static CallbackProc _callback;
[DllExport("SetCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static void SetCallback(CallbackProc pCallback)
{
_callback = pCallback;
//var app = new App();
//var win = new MainWindow();
//app.Run(win);
MessageBox.Show("C#: SetCallback Completed");
}
[DllExport("TestCallback", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static void TestCallback(string passedString)
{
string displayValue = passedString;
string returnValue = String.Empty;
TodayButton.MainWindow app = new TodayButton.MainWindow();
app.InitializeComponent();
MessageBox.Show("C#: About to call the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
_callback(displayValue, ref returnValue);
MessageBox.Show("C#: Back from the Callback. displayValue=" + displayValue + ", returnValue=" + returnValue);
}
public delegate void CallbackProc( [MarshalAs(UnmanagedType.BStr)] String PassedValue, [MarshalAs(UnmanagedType.BStr)] ref String ReturnValue);
}
}
因此回调可以完美地工作。我有一个我不知道如何解决的问题。我想将其用作WPF应用程序的接口。因此,当程序调用dll时,dll应该启动WPF应用程序,等待该应用程序关闭并发送信息,然后我将调用回调 问题是我不想每次需要WPF应用程序的信息时都启动和停止它。我想要一个按钮“发送到回调”。问题是这个项目是一个类库,使用DllExport,WPF项目不会编译为类库 有没有办法做到这一点,这样我就可以用dll控制整个WPF应用程序?因此,我可以控制启动、停止、传递回调值,以便从WPF表单调用它们,或者在按下WPF中的按钮时将信息发送回
TestCallback
函数
这怎么可能呢?最后,我决定使用一个简单的消息传递库,因为我可以在DLL端使用WinForms,在另一端使用WPF,所以我可以使用windows消息。这是我用过的图书馆 另一种更高级的数据共享方式是,我想使用它,但最后我决定不使用WCF,因为我不发送大量数据,只发送控制应用程序的消息。可能重复