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

发布于 2024-09-25 05:31:33 字数 294 浏览 2 评论 0原文

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

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

实现这一目标的最佳方法是什么?我尝试使用标准输入和标准输出,但这效果不佳(由于某种原因,C 进程的标准输入被一些字符串 (x(U+266C)Q) 垃圾邮件(U+266C 是 UTF8 光束十六音符)

What would be the best way to communicate between a C and a C# process. I need to send messages containing commands and params and such from the C# process. to the C process. and the C process then has to be able to send reply's.

I start the C process in the C# process.

What would be the best way to achieve this? I tried using stdin and stdout but this doesn't work well (for some reason the stdin of the C process is spammed with some string (x(U+266C)Q) (U+266C is a UTF8 beamed sixteen note)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-10-02 05:31:33

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

Do you really need as separate processes? If you own both codes why don't you make interop calls by importing the c library methods:

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

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

and in your C library you export your method using a .def file:

LIBRARY "yourlibrary"
  EXPORTS
     YourMethod
转身泪倾城 2024-10-02 05:31:33

听起来您无权访问 C 程序源代码。我将使用 ProcessStartInfo 启动您的外部 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();
}

It sound like you dont have access to the C program source code. I would use ProcessStartInfo to launch your extern C program. But before you do, you redirect the Input/Output. See sample code below:

    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();
}
深者入戏 2024-10-02 05:31:33

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

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

Do your processes need to run in parallel or you start an external process and need to obtain it's results? If you just start a child process, then, as said in the comment, you don't perform UTF16->ASCII conversion of data being passed to the child application.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文