Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何包装UTF-8编码的C++;std::C#中带Swig的字符串?_C++_Swig - Fatal编程技术网

C++ 如何包装UTF-8编码的C++;std::C#中带Swig的字符串?

C++ 如何包装UTF-8编码的C++;std::C#中带Swig的字符串?,c++,swig,C++,Swig,我的问题与几乎相同,只是链接的问题处理char*,而我在代码中使用的是std::string。像链接问题一样,我也使用C#作为我的目标语言 我有一个用C++编写的类: class MyClass { public: const std::string get_value() const; // returns utf8-string void set_value(const std::string &value); // sets utf8-string private:

我的问题与几乎相同,只是链接的问题处理char*,而我在代码中使用的是std::string。像链接问题一样,我也使用C#作为我的目标语言

我有一个用C++编写的类:

class MyClass
{
public:
    const std::string get_value() const; // returns utf8-string
    void set_value(const std::string &value); // sets utf8-string
private:
    // ...
};
这个get由C#中的SWIG包装,如下所示:

public class MyClass
{
    public string get_value();
    public void set_value(string value);
}
SWIG为我做了所有的事情,除了在调用MyClass期间它不会进行utf8到utf16字符串转换。如果字符串可以用ASCII表示,那么我的字符串可以通过,但是如果我尝试通过“set_value”和“get_value”在往返过程中传递一个带有非ASCII字符的字符串,我最终会得到无法理解的字符

如何在C语言中进行UTF-8编码的C++字符串的SWIP封装?n、 我使用的是std::string,不是std::wstring,也不是char*

上有一个部分解决方案,但它处理char*而不是std::string,并使用(可配置的)固定长度缓冲区。

在链接代码项目文章中的帮助下(阅读:genius!),我终于能够回答这个问题

在C#库中需要这个类(来自David Jeske的代码)

然后,在Swig的“std_string.i”中,第24行替换该行:

%typemap(imtype) string "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string "string"
%typemap(imtype) const string & "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string & "string"
这一行:

%typemap(imtype) string "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string "string"
%typemap(imtype) const string & "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string & "string"
在第61行中,替换该行:

%typemap(imtype) string "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string "string"
%typemap(imtype) const string & "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string & "string"
这一行:

%typemap(imtype) string "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string "string"
%typemap(imtype) const string & "string"
%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]", outattributes="[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))]") string & "string"

瞧,一切都在运转。阅读链接文章,了解其工作原理。

为了清楚起见,SWIG的输出语言是什么?答案完全取决于目标语言。谢谢@DanielKO和Flexo,你说得对,我真傻,省略了这个细节,我会更新OP。目标语言是C#。这是一个很好的解决方案,直到您尝试在适用于Windows 8.1或Windows 10的Windows应用商店通用应用程序中使用它。这需要一些我认为不会通过认证的东西,如果它们在Windows运行时应用程序中工作的话。(1) 它需要使用“允许不安全代码”选项,(2)需要使用CustomMarshaler类和ICustomMarshaler接口,这在Windows运行时中被省略。