将字符串从 C# 发送到 C++

发布于 2025-01-08 07:05:05 字数 639 浏览 1 评论 0原文

我有一个 C++ 程序,它对 char search[500] 数组进行一些处理。这里需要回避的是 search[] 必须从 C# 程序中赋值。

考虑一下,我有一个 C# 程序,它从 textbox 获取用户的输入,并且必须将此 string 发送到 C#。

我已经能够将数据、函数和变量从 C++ 导出到 C#,但我不熟悉相反的过程是如何完成的。

[DllImport("Test.dll", EntryPoint = "fnmain", CallingConvention = CallingConvention.Cdecl , CharSet = CharSet.Ansi)]
private static extern int fnmain();//pass what in parameter?

C++

//search[] parameter has to be here. What type to be assigned to get from C# and further get search[] char array
    int main(char search[])
    {
    ..
    }

I have a C++ program which does some processing on a char search[500] array. The dodge here is that the search[] has to be assigned value from C# program.

Consider, that I have this C# program which gets user's input from textbox and have to send this string to C#.

I have been able to export the data, functions and variables from C++ to C#, but I am not familiar how the reverse is done.

[DllImport("Test.dll", EntryPoint = "fnmain", CallingConvention = CallingConvention.Cdecl , CharSet = CharSet.Ansi)]
private static extern int fnmain();//pass what in parameter?

C++

//search[] parameter has to be here. What type to be assigned to get from C# and further get search[] char array
    int main(char search[])
    {
    ..
    }

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

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

发布评论

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

评论(2

讽刺将军 2025-01-15 07:05:05

我认为您希望搜索成为一个字符串。我相信 CharSet.Ansi 是确保互操作将字符串转换为 char * 所需的全部。

I think you want search to be a string. I believe CharSet.Ansi is all you need to make sure the interop converts strings to char *.

清欢 2025-01-15 07:05:05

这听起来可能很愚蠢。您是否考虑过使用命令行参数调用 C++ 程序?如果初始化需要大量时间,您可以使用管道的概念或从文件读取/写入文件。如果您可以进行一些套接字编程,那将意味着真正的异步数据传递。希望这有帮助。

This may sound silly. Have you considered calling C++ program with command line arguments? If initialization takes a lot of time, you can use concepts of pipes or read/write from or to files. If you can do a little bit of socket programming, that would mean true asynchronous passing of data. Hope this helped.

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