将字符串从 C# 发送到 C++
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您希望搜索成为一个
字符串
。我相信 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 *.这听起来可能很愚蠢。您是否考虑过使用命令行参数调用 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.