c++/cli 托管 非托管类型 转换 marshal_as(vs2008) marshal_to(vs2005)
2010-10-06 15:10
344 查看
#include <msclr/marshal.h>
using namespace msclr::interop;
msclr::interop::marshal_context^ context;
const wchar_t* __MarshaledReturnVal = context->marshal_as<const wchar_t*>(__ReturnVal);
I spent quite some time now doing research on C++ to .NET bridging
using C++/CLI and found your approach and code to be the cleanest from
all the examples I looked at. However, getting it to work required some
changes which made me wonder if you ever tried compiling above code
samples. Or maybe the way I did it was different than intended?
For anyone interested in my working solution, here my approach:
1. I used Visual Studio 2008 and created 3 projects:
- A C# class library called ManagedClasses which contains the Calculator class.
- A C++ class library with CLR support (/clr) called ILBridge which
contains the ILBridge_CppCliWrapper_Calculator and
NativeExport_CppCliWrapper_Calculator classes.
- A C++ console application without CLR support called NativeApp which uses the ILBridge lib.
Due to changes in the way the marshaling between objects is done in
Visual Studio 2008 I added an include for <msclr/marshal.h> and
changed the code in the ILBridge class from this line:
wchar_t* __MarshaledReturnVal = marshal_to<wchar_t*>(__ReturnVal);
to this code snippet:
msclr::interop::marshal_context^ context;
const wchar_t* __MarshaledReturnVal = context->marshal_as<const wchar_t*>(__ReturnVal);
delete context;
Also, I had to split up the ILBridge class into header and source
file, otherwise the NativeExport class would not compile, because the
compiler kept complaining that you cannot use marshal_as in a class that
is translated into native code.
Then I added
#using "../ManagedClasses/bin/Debug/ManagedClasses.dll"
to the ILBridge class header so that it finds the C# assembly.
Finally I took out the #ifdef THISDLL_EXPORTS from the NativeExport
class header, since unless you define this symbol it won't set the
correct dll export.
using namespace msclr::interop;
msclr::interop::marshal_context^ context;
const wchar_t* __MarshaledReturnVal = context->marshal_as<const wchar_t*>(__ReturnVal);
I spent quite some time now doing research on C++ to .NET bridging
using C++/CLI and found your approach and code to be the cleanest from
all the examples I looked at. However, getting it to work required some
changes which made me wonder if you ever tried compiling above code
samples. Or maybe the way I did it was different than intended?
For anyone interested in my working solution, here my approach:
1. I used Visual Studio 2008 and created 3 projects:
- A C# class library called ManagedClasses which contains the Calculator class.
- A C++ class library with CLR support (/clr) called ILBridge which
contains the ILBridge_CppCliWrapper_Calculator and
NativeExport_CppCliWrapper_Calculator classes.
- A C++ console application without CLR support called NativeApp which uses the ILBridge lib.
Due to changes in the way the marshaling between objects is done in
Visual Studio 2008 I added an include for <msclr/marshal.h> and
changed the code in the ILBridge class from this line:
wchar_t* __MarshaledReturnVal = marshal_to<wchar_t*>(__ReturnVal);
to this code snippet:
msclr::interop::marshal_context^ context;
const wchar_t* __MarshaledReturnVal = context->marshal_as<const wchar_t*>(__ReturnVal);
delete context;
Also, I had to split up the ILBridge class into header and source
file, otherwise the NativeExport class would not compile, because the
compiler kept complaining that you cannot use marshal_as in a class that
is translated into native code.
Then I added
#using "../ManagedClasses/bin/Debug/ManagedClasses.dll"
to the ILBridge class header so that it finds the C# assembly.
Finally I took out the #ifdef THISDLL_EXPORTS from the NativeExport
class header, since unless you define this symbol it won't set the
correct dll export.
相关文章推荐
- Item 3: 使用 is 或 as 去做类型转换(Prefer the is or as Operators to Casts)
- [翻译]转换VS2005项目到VS2008-开启LINQ(Converting VS 2005 Projects to VS 2008 - Enabling LINQ)
- 用as进行类型转换
- oracle 数值型函数,字符型函数,日期函数,转换函数,to_char,自动类型转换,日期类型转换
- C#类型转换、is、as、单问号、双问号解密
- 【原创】C#两种任意类型转换支持(示例:String convert to Type)
- 【转】将vs2008 项目转换VS2005解决方案
- CLR via 笔记4.2 类型转换 is 与 as 区别
- 〖Oracle 转载〗Oracle的数据类型转换 to_char
- Welcome-to-Swift-18类型转换(Type Casting)
- 9. 11. 30. 从asList到toArray转换 -Convert a List (ArrayList) to an Array with zero length array
- didSet属性观察、本地图片路径资源获取、UIColorBreakForOutOfRangeColorComponents、可选链、as?或as!类型转换
- VS2005、VS2008、VS2010、VS2012解决方案和工程项目文件转换方法
- 浅谈类型转换操作符is/as
- 向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE('{0}','YYYY-MM-DD'))
- 从VS2005项目转换为VS2008项目(C#版)
- Swift - as、as!、as?三种类型转换操作使用一览
- c# as is 类型转换
- python类型强制转换long to int的代码
- loadtxt()读取数据类型转换/string转换float/ValueError: could not convert string to float: