互送字符串从C#和C ++ [英] Interop sending string from C# to C++

查看:109
本文介绍了互送字符串从C#和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#中的字符串发送到函数在本地C ++ DLL

I want to send a string from C# to a function in a native C++ DLL.

下面是我的代码:
C#的一面:

Here is my code: The C# side:

[DllImport(@"Native3DHandler.dll", EntryPoint = "#22", 
    CharSet = CharSet.Unicode)]
private static extern void func1(byte[] path);

public void func2(string path)
{
   ASCIIEncoding encoding = new ASCIIEncoding();
   byte[] arr = encoding.GetBytes(path);
   func1(this.something, arr);
}



C ++的一面:

The C++ side:

void func1(char *path)
{
    //...
}

我得到在C ++侧是一个空字符串,每次,不管什么我送。
帮助?

What I get in the C++ side is an empty string, every time, no matter what I send. Help?

感谢。

推荐答案

做工精细我在VS2008没有额外的编组说明:

Works fine for me with no extra marshalling instructions in VS2008:

C#端:

[DllImport("Test.dll")]
public static extern void getString(StringBuilder theString, int bufferSize);

func()
{
  StringBuilder tstStr = new StringBuilder(BufSize);
  getString(tstStr, BufSize);
}



C ++端:

C++ side:

extern "C" __declspec(dllexport) void getString(char* str, int bufferSize)
{
  strcpy_s(str, bufferSize, "FOOBAR");
}

这篇关于互送字符串从C#和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆