How to communicate between C and C # application

What would be the best way to communicate between process C and C #. I need to send messages containing commands and parameters, and such from a C # process. to process C. and then process C should send a response.

I am starting a C process in a C # process.

What would be the best way to achieve this? I tried using stdin and stdout, but it didn’t work (for some reason stdin of process C is flashing some line (x (U + 266C) Q) (U + 266C is UTF8 there were sixteen notes brightened )

+3
source share
3 answers

? , interop, c:

class Program
{
    [DllImport("yourlibrary.dll")]
    public static extern int YourMethod(int parameter);

    static void Main(string[] args)
    {
        Console.WriteLine(YourMethod(42));
    }
}

C .def:

LIBRARY "yourlibrary"
  EXPORTS
     YourMethod
+6

, C. ProcessStartInfo extern C. , /. . :

    private void start()
{
    Process p = new Process();
    StreamWriter sw;
    StreamReader sr;
    StreamReader err;
    ProcessStartInfo psI = new ProcessStartInfo("cmd");
    psI.UseShellExecute = false;
    psI.RedirectStandardInput = true;
    psI.RedirectStandardOutput = true;
    psI.RedirectStandardError = true;
    psI.CreateNoWindow = true;
    p.StartInfo = psI;
    p.Start();
    sw = p.StandardInput;
    sr = p.StandardOutput;
    sw.AutoFlush = true;
    if (tbComm.Text != "")
        sw.WriteLine(tbComm.Text);
    else
        //execute default command
        sw.WriteLine("dir \\");
    sw.Close();
    textBox1.Text = sr.ReadToEnd();
    textBox1.Text += err.ReadToEnd();
}
+3

? , , , UTF16- > ASCII , .

If you need to run processes in parallel and exchange messages between them, check out our MsgConnect product, which was specifically designed for such tasks.

0
source

Source: https://habr.com/ru/post/1767337/


All Articles