0

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?

Zoneh
  • 192
  • 5
  • 19
  • 1
    Possible duplicate of http://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro – Sean Airey Jun 18 '14 at 14:11

1 Answers1

0

In the end I have decided I'm gonna use a simple messaging library since i could have WinForms on the DLL side and WPF on the other side, so I could use windows messages. http://thecodeking.github.io/XDMessaging.Net/ is the library I used.

Another, more advanced way of sharing data is http://blogs.msdn.com/b/dmetzgar/archive/2012/10/16/an-updated-custom-wcf-transport-channel-example.aspx I wanted to use this but in the end I decided that I'm not gonna use WCF since I'm not sending alot of data, just messages that control the app.

Zoneh
  • 192
  • 5
  • 19