将 char 指针从 C# 传递到 c++功能
我陷入了 C# 实现方面,因为我对它还很陌生。问题是,我想从 C# 代码传递一个“指针”(具有内存),以便我的 C++ 应用程序可以将 pchListSoftwares 缓冲区复制到 pchInstalledSoftwares。我无法弄清楚如何从 C# 端传递指针。
本机 c++ 代码(MyNativeC++DLL.dll)
void GetInstalledSoftwares(char* pchInstalledSoftwares){
char* pchListSoftwares = NULL;
.....
.....
pchListSoftwares = (char*) malloc(255);
/* code to fill pchListSoftwares buffer*/
memcpy(pchInstalledSoftwares, pchListSoftwares, 255);
free(pchListSoftwares );
}
传递简单的“字符串”不起作用...
C# 实现
[DllImport("MyNativeC++DLL.dll")]
private static extern int GetInstalledSoftwares(string pchInstalledSoftwares);
static void Main(string[] args)
{
.........
.........
string b = "";
GetInstalledSoftwares(0, b);
MessageBox.Show(b.ToString());
}
非常感谢任何类型的帮助...
I am stuck in c# implementation side, as I am pretty new to it. The thing is, I want to pass a 'pointer'(having memory) from c# code so that My c++ application can copy pchListSoftwares buffer to pchInstalledSoftwares. I am not able to figure out how to pass pointer from c# side.
native c++ code(MyNativeC++DLL.dll)
void GetInstalledSoftwares(char* pchInstalledSoftwares){
char* pchListSoftwares = NULL;
.....
.....
pchListSoftwares = (char*) malloc(255);
/* code to fill pchListSoftwares buffer*/
memcpy(pchInstalledSoftwares, pchListSoftwares, 255);
free(pchListSoftwares );
}
Passing simple 'string' is not working...
C# implementation
[DllImport("MyNativeC++DLL.dll")]
private static extern int GetInstalledSoftwares(string pchInstalledSoftwares);
static void Main(string[] args)
{
.........
.........
string b = "";
GetInstalledSoftwares(0, b);
MessageBox.Show(b.ToString());
}
Any kind of help is greatly appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 StringBuilder
Try using a StringBuilder
我的错误...在对
GetInstalledSoftwares(0, b);
的调用中删除 0。My mistake... remove 0 in call to
GetInstalledSoftwares(0, b);
.尝试将原型行更改为:(
通过引用发送字符串)。
Try to change the prototype line to:
(Send the string by reference).