本文介绍了专家帮助迫切需要InterOp(Managed C ++?)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个C ++ DLL,我想在C#项目中使用它。我实际上是很多高级C ++功能,如模板,部分/专用 模板,仿函数和回调。我也在我的C ++ DLL API中广泛使用STL容器,比如 std :: string,std :: vector和std :: map。 但是,我可以简化API,以便更容易使用C#。 下面,是一个非常简单的概念证明。 C ++ DLL。如果有人能告诉我如何使用C#中的Dll,我会非常感激。感激不尽。 这是代码: / * C ++代码(标题) (概念DLL的简单证明* / #ifdef TESTDLL_EXPORTS #define CCONV __declspec(dllexport) #else #define CCONV __declspec(dllimport) #endif typedef enum { ONE, TWO, THREE> } myEnum; typedef struct mystruct_ { int x; float y; char z [8] ; } myStruct; typedef int(* INT_FPTR)(const char *,const MyStruct *); class CCONV MyClass { public: MyClass(); MyClass(const MyClass&); MyClass& operator =(const MyClass&); ~MyClass(); private: //一些私人变量在这里.. }; class CCONV DClass:public MyClass { public: DClass(); DClass(const DClass&); DClass& operator =(const DClass&); ~DClass(); const char * foo(void); int barney(int,myEnum,const myStruct *); //抛出异常 void register_callback(INT_FPTR); private: INT_FPTR cb; $ b很多,谢谢 模板和STL 同时发出MSIL。 问候, Jeff ***通过开发人员发送的指南 http://www.developersdex.com *** 抛出异常是什么意思?,barney是一个C ++成员函数, 你不能用C#创建本机C ++类的实例,所以你不能称之为 成员函数。 您唯一的选择是: 1.在托管程序集中创建托管C ++类(使用vs2005 C ++ / CLI) 包装您的非托管C ++类( es)。 2.将您的公共接口(暴露给C#)转换为托管C ++类,并使用C ++ / CLI(vs2005)编译。 Willy。 这个陈述有什么难以理解的? C ++方法*可以* 抛出异常 - 也许是托管C ++ / C#(或者应该是C哑) 类不能? 你不能用C#创建本机C ++类的实例,所以你不能调用成员函数。你唯一的选择是: 1.创建一个托管C ++类一个托管程序集(使用vs2005 C ++ / CLI)包装你的非托管C ++类。 < snip> 我已经知道了。每个人(包括MS站点告诉我,我需要围绕我的C ++类编写托管C ++包装类 - 但是 没有任何有用的示例 - 大多数示例与从C#调用C函数的 有关。到目前为止,它一直都是谈话而没有例子 - 有人*实际上*曾经从C#调用C ++类?如果它不能完成或者从来没有在现实世界中尝试过 - 只需要男人 就足以说明发布毯子线了:编写托管C ++ $ C $ b类围绕你的C ++类。还没有人(包括MS 本身)afaik,实际上提供了一个使用C ++类的例子 从C ++ DLL导出 - 所以来吧:可以做或不做?。如果是的话 (每个人都告诉我它可以 - 但没有例子) - 地狱怎么样是不是已经完成了什么?有人真的知道了吗?我提供了一个简单的无需支持 C ++ DLL并且预期在至少有一些伪代码显示我如何从C#调用 2/3函数 - 显然,没有人真的知道如何做到这一点.... Hi, I have a C++ DLL that I want to use from a C# project. I am actuallyusng a lot of advanced C++ features like templates, partial/specializedtemplates, functors and callbacks. I am also using STL containers likestd::string, std::vector and std::map quite extensively in my C++ DLL API. However, I can simplify the API so as to make it easier to use from C#.Below, is a very simple "proof of concept" C++ DLL. I would be extremelygrateful if someone could show me how I can use this Dll from C#: Here is the code: /* C++ code (Header)(trivial proof of concept DLL */ #ifdef TESTDLL_EXPORTS#define CCONV __declspec(dllexport)#else#define CCONV __declspec(dllimport)#endif typedef enum {ONE ,TWO,THREE} myEnum; typedef struct mystruct_ {int x ;float y ;char z[8] ;} myStruct ; typedef int (*INT_FPTR)(const char*, const MyStruct*) ; class CCONV MyClass {public:MyClass();MyClass(const MyClass&);MyClass& operator = (const MyClass&);~MyClass(); private://some private variables here ..}; class CCONV DClass : public MyClass {public :DClass();DClass(const DClass&);DClass& operator = (const DClass&);~DClass(); const char* foo(void);int barney(int, myEnum, const myStruct*); //throws an exceptionvoid register_callback( INT_FPTR ) ; private:INT_FPTR cb ;};Many Thanks 解决方案 What do you mean by throws an exception?, "barney" is a C++ member function,you can''t create instances of native C++ classes from C#, so you can''t callmember functions.Your only options are:1. create a managed C++ class in a managed assembly (using vs2005 C++/CLI)that wrap your unmanaged C++ class(es).2. convert your public interfaces (exposed to C#) to managed C++ classes andcompile with C++/CLI (vs2005). Willy. What is so difficult to understand by this statement?. C++ methods *can*throw exceptions - maybe managed C++/C# (or should that be C dumb)classes can''t ? you can''t create instances of native C++ classes from C#, so you can''t call member functions. Your only options are: 1. create a managed C++ class in a managed assembly (using vs2005 C++/CLI) that wrap your unmanaged C++ class(es). <snip>I already knew that. Everybody (including the MS site tells me I need towrite managed C++ wrapper classes around my C++ classes - but there isno useful examples given anywhere - most of the examples are to do withcalling C functions from C#. So far its been all talk and no examples -has anyone *actually* EVER called C++ classes from C#? If it can''t bedone or it has never been tried out in the real world - just be manenough to say it instad of issuing the blanket line : "write managed C++classes around your C++ classes". No one as yet (and that included MSitself) afaik, has actually provided an example of using C++ classesexported from a C++ DLL - so come on : can it be done or NOT ?. If yes(everyone tells me it can - but has no examples) - HOW the hell is itdone - does anyone actually even know?. I provided a simple no brainerC++ DLL and expected at least some pseudocode to show how I can call the2/3 functions from C# - apparently, no one really knows how to do this .... 这篇关于专家帮助迫切需要InterOp(Managed C ++?)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 05:20