Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C和C应用程序之间进行通信_C#_C_Communication - Fatal编程技术网

C# 如何在C和C应用程序之间进行通信

C# 如何在C和C应用程序之间进行通信,c#,c,communication,C#,C,Communication,在C和C进程之间进行通信的最佳方式是什么。我需要从C#进程发送包含命令和参数等的消息。到C进程。然后C进程必须能够发送回复 我在C#进程中启动C进程 实现这一目标的最佳方式是什么?我尝试使用stdin和stdout,但效果不太好(由于某些原因,C进程的stdin被垃圾发送了一些字符串(x(U+266C)Q)(U+266C是UTF8)您真的需要作为单独的进程吗?如果您拥有这两个代码,为什么不通过导入C库方法进行互操作调用: class Program { [DllImport("yourl

在C和C进程之间进行通信的最佳方式是什么。我需要从C#进程发送包含命令和参数等的消息。到C进程。然后C进程必须能够发送回复

我在C#进程中启动C进程


实现这一目标的最佳方式是什么?我尝试使用stdin和stdout,但效果不太好(由于某些原因,C进程的stdin被垃圾发送了一些字符串(x(U+266C)Q)(U+266C是UTF8)

您真的需要作为单独的进程吗?如果您拥有这两个代码,为什么不通过导入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

您的进程是否需要并行运行,或者您启动了一个外部进程并需要获得它的结果?如果您只是启动一个子进程,那么,正如注释中所述,您不会对传递给子应用程序的数据执行UTF16->ASCII转换


如果您需要并行运行进程并在它们之间交换消息,请查看我们的产品,它是专为此类任务而设计的。

听起来您似乎无法访问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();
}

似乎你有一个编码问题,请看这个问题:最终我们可以使用C代码在C#中使用的库。谢谢!谢谢。结果我们可以完全忽略C程序,使用C程序中使用的库。但是谢谢你的回答,这非常有帮助。