元帅C ++"串"在C#中的P / Invoke类 [英] Marshal C++ "string" class in C# P/Invoke

查看:186
本文介绍了元帅C ++"串"在C#中的P / Invoke类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在定义一个本地DLL中的函数如下:

I have a function in a native DLL defined as follows:

#include <string>
void SetPath(string path);

我试图把这个微软的P /调用互操作助理,但它闷死在字符串级(我认为这是从MFC?)。

I tried to put this in Microsoft's P/Invoke Interop Assistant, but it chokes on the "string" class (which I think is from MFC?).

我试图封送它作为各种不同类型的(C#字符串和char [],byte []的),但每次我要么得到一个NotSupportedException异常或本地大会异常(这取决于封送我试过)。

I have tried marshaling it as a variety of different types (C# String, char[], byte[]) but every time I either get a NotSupportedException or a Native Assembly Exception (depending on what marshaling I tried).

由于有人曾经做过本地/管理的互操作,其中本地字符串类用于?有什么办法元帅呢?我是不是有写我自己的封送拆?

As anyone ever done Native/Managed Interop where the native string class is used? Is there any way to Marshal this? Am I going to have to write my own Marshaler?

推荐答案

看起来你试图使用C ++标准库的string类。我怀疑,这将是很容易元帅。最好坚持使用一个char *元帅为StringBuilder的。这就是我通常做。你必须添加一个包装器,生成的C ++字符串为您服务。

Looks like you're trying to use the C++ standard library string class. I doubt that will be easy to Marshal. Better to stick with a char * and Marshal as StringBuilder. That's what I usually do. You'll have to add a wrapper that generates the C++ string for you.

这篇关于元帅C ++&QUOT;串&QUOT;在C#中的P / Invoke类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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