Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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
.net 转换托管C++/跨程序集边界将CLI类型转换为非托管类型 P>已经编写了一些C++/CLI来包装现有的非托管C++库,出现了从托管类型转换为非托管类型的问题。一个简单的例子是将std::string转换为System::string,但该原则适用于许多类型,即array->vector。在编写了一些转换函数之后,我决定将它们打包到一个程序集中,以便可以重用它们。考虑到全局C++/CLI函数在当前程序集之外不可见,我最终得到了这样的结果 public ref class ClassJustToContainStaticFunctions { public: static std::string convert( System::String^ s ) { msclr::interop::marshal_context context; return( context.marshal_as<std::string>( s ) ); } };_.net_Stl_C++ Cli_Interop_Mixed Mode - Fatal编程技术网

.net 转换托管C++/跨程序集边界将CLI类型转换为非托管类型 P>已经编写了一些C++/CLI来包装现有的非托管C++库,出现了从托管类型转换为非托管类型的问题。一个简单的例子是将std::string转换为System::string,但该原则适用于许多类型,即array->vector。在编写了一些转换函数之后,我决定将它们打包到一个程序集中,以便可以重用它们。考虑到全局C++/CLI函数在当前程序集之外不可见,我最终得到了这样的结果 public ref class ClassJustToContainStaticFunctions { public: static std::string convert( System::String^ s ) { msclr::interop::marshal_context context; return( context.marshal_as<std::string>( s ) ); } };

.net 转换托管C++/跨程序集边界将CLI类型转换为非托管类型 P>已经编写了一些C++/CLI来包装现有的非托管C++库,出现了从托管类型转换为非托管类型的问题。一个简单的例子是将std::string转换为System::string,但该原则适用于许多类型,即array->vector。在编写了一些转换函数之后,我决定将它们打包到一个程序集中,以便可以重用它们。考虑到全局C++/CLI函数在当前程序集之外不可见,我最终得到了这样的结果 public ref class ClassJustToContainStaticFunctions { public: static std::string convert( System::String^ s ) { msclr::interop::marshal_context context; return( context.marshal_as<std::string>( s ) ); } };,.net,stl,c++-cli,interop,mixed-mode,.net,Stl,C++ Cli,Interop,Mixed Mode,我在其他情况下也没有遇到任何问题。但是,make_public不适用于像std::string这样的模板类。请参阅此处了解一些信息 例如,我发现有一些尝试,但它们看起来都很丑陋 所以,在所有这些之后,我的问题是我是否遗漏了一些明显的东西?在我看来,从托管类型转换为非托管类型,特别是对于容器类(如STL.NET Unmanged STL)将是一个常见问题,但经过多次搜索,我在该主题上没有找到太多内容。因为这些函数仅在混合模式编程中需要,将它们包装到一些头文件/静态库中,而不是程序集中。通过这种方式

我在其他情况下也没有遇到任何问题。但是,make_public不适用于像std::string这样的模板类。请参阅此处了解一些信息

例如,我发现有一些尝试,但它们看起来都很丑陋


所以,在所有这些之后,我的问题是我是否遗漏了一些明显的东西?在我看来,从托管类型转换为非托管类型,特别是对于容器类(如STL.NET Unmanged STL)将是一个常见问题,但经过多次搜索,我在该主题上没有找到太多内容。

因为这些函数仅在混合模式编程中需要,将它们包装到一些头文件/静态库中,而不是程序集中。通过这种方式,您可以在每个程序中重复使用它们,但您并不依赖于它们的导出。

对于您的问题,这不是一个特定的答案,但是,对于这些转换函数,我没有任何像您这样的问题:

        static void StringToStdString ( String ^ s, std::string& os ) 
        {
            using namespace Runtime::InteropServices; 
            const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
            os = chars;
            Marshal::FreeHGlobal(IntPtr((void*)chars));
        }

        static const char * StringToCharPtr ( String ^ s) 
        {
            using namespace Runtime::InteropServices; 
            const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
            return chars;
        }

        static String^ StdStringToString(const std::string& _is)
        {
            return gcnew String(_is.c_str());
        }

在DLL中传递C++标准库类总是坏消息。如果可能的话,避免它。

微软的C++支持库,OP的代码包,已经处理了所有这些转换。@本:哦,对不起,修复了。这是真的,库已经提供了一些简单的转换。然而,我的问题适用于我想要执行的一系列更复杂的转换。我以字符串大小写为例;它们实际上不需要在程序集中。我喜欢在标头中使用内联函数——这就是微软C++支持库的作用。我尝试过静态库方法,我必须承认我不认为静态.NET程序集是可能的。无论如何,链接器似乎确实生成了一个.lib文件。然后我尝试将其添加到链接器输入中,得到了未解析的符号。我还尝试在.lib上使用,这给了我一个C1113使用失败的错误。我真的找不到任何关于如何在静态库中链接到C++/CLI代码的好文档。相关问题:我已经阅读了4121249问题,这是我提到的“非常难看”的解决方法之一。无论如何,这可能是真的很久以前,仍然是如果你使用一个混合的标志,例如dubug v的优化,为不同的DLL的,但不是真的与我的问题在这篇文章,无论如何谢谢。
        static void StringToStdString ( String ^ s, std::string& os ) 
        {
            using namespace Runtime::InteropServices; 
            const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
            os = chars;
            Marshal::FreeHGlobal(IntPtr((void*)chars));
        }

        static const char * StringToCharPtr ( String ^ s) 
        {
            using namespace Runtime::InteropServices; 
            const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
            return chars;
        }

        static String^ StdStringToString(const std::string& _is)
        {
            return gcnew String(_is.c_str());
        }