I have a dll that has to talk to a program not written in C#. I have established that connection with this code:
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);
}
}
So the callbacks work perfectly. I have a problem that I dont know how to solve.
I want to use this as an interface for my WPF app. So when a program calls the dll, the dll should start the WPF app and wait for that app to close and send the information, then I would call the callback.
The thing is I dont want to start and stop the WPF app everytime I need info from it. I want to have a button "send to callback". The problem is that this project is a Class Library, using DllExport and the WPF project won't compile as a class library.
Is there a way to do this, so I can control the whole WPF app with the dll? So I would control start, stop, pass callback values so I could call them from WPF forms, or just send info back to the TestCallback
function when I press a button in WPF.
How would that be possible?