专家帮助迫切需要InterOp(Managed C ++?)问题 [英] EXPERT help URGENTLY NEEDED on InterOp (Managed C++?) issue

查看:71
本文介绍了专家帮助迫切需要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 ***




" ET灰色" <是ne **** @ alpha-centauri.com>在消息中写道

news:dk ********** @ nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com ...



我想在C#项目中使用C ++ DLL。我实际上使用了许多高级C ++功能,如模板,部分/专用模板,仿函数和回调。我也在我的C ++ DLL API中广泛使用st / :: std :: vector,std :: vector和std :: map等STL容器。

但是,我可以简化API使用C#更容易使用。
下面是一个非常简单的概念证明。 C ++ DLL。如果有人能告诉我如何从C#中使用这个Dll,我会非常感激:

这是代码:

/ * C ++代码(Header )
(琐碎的概念证明DLL * /
#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dllexport)
#else
#define CCONV __declspec( dllimport)
#endif

typedef enum {
ONE,
TWO,
三,
} myEnum;
typedef struct mystruct_ {
int x;
float y;
char z [8];
} myStruct;

typedef int(* INT_FPTR) (const char *,const MyStruct *);

类CCONV MyClass {
public:
MyClass();
MyClass(const MyClass&);
MyClass& operator =(const MyClass&);
~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);

私人:
INT_FPTR cb;
};

非常感谢




抛出异常是什么意思?,barney是一个C ++成员函数,

你不能用C#创建本机C ++类的实例,所以你不能称之为

成员函数。

您唯一的选择是:

1.在托管程序集中创建托管C ++类(使用vs2005 C ++ / CLI)

包装您的非托管C ++类( es)。

2.将您的公共接口(暴露给C#)转换为托管C ++类,并使用C ++ / CLI(vs2005)编译



Willy。





Willy Denoyette [MVP]写道:

" ET灰色" <是ne **** @ alpha-centauri.com>在消息中写道
新闻:dk ********** @ nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com ...



我想在C#项目中使用C ++ DLL。我实际上使用了许多高级C ++功能,如模板,部分/专用模板,仿函数和回调。我也在我的C ++ DLL API中广泛使用st / :: std :: vector,std :: vector和std :: map等STL容器。

但是,我可以简化API使用C#更容易使用。
下面是一个非常简单的概念证明。 C ++ DLL。如果有人能告诉我如何从C#中使用这个Dll,我会非常感激:

这是代码:

/ * C ++代码(Header )
(琐碎的概念证明DLL * /
#ifdef TESTDLL_EXPORTS
#define CCONV __declspec(dllexport)
#else
#define CCONV __declspec( dllimport)
#endif

typedef enum {
ONE,
TWO,
三,
} myEnum;
typedef struct mystruct_ {
int x;
float y;
char z [8];
} myStruct;

typedef int(* INT_FPTR) (const char *,const MyStruct *);

类CCONV MyClass {
public:
MyClass();
MyClass(const MyClass&);
MyClass& operator =(const MyClass&);
~MyClass();

私人:
//这里有一些私有变量..
};
public:
DClass();
DClass(const DClass&);
DClass& operator =(const DClass& );
~DClass();

const cha r * foo(void);
int barney(int,myEnum,const myStruct *); //抛出异常
void register_callback(INT_FPTR);

私人:
INT_FPTR cb;
};

非常感谢

抛出异常是什么意思?,barney是一个C ++成员函数,




这个陈述有什么难以理解的? 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 actually
usng a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::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 extremely
grateful 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 exception
void register_callback( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks

解决方案

ET I am no expert, but VS2005 managed C++ supports templates and the STL
while emitting MSIL.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***



"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...

Hi,

I have a C++ DLL that I want to use from a C# project. I am actually usng
a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::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 extremely
grateful 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 exception
void 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 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).
2. convert your public interfaces (exposed to C#) to managed C++ classes and
compile with C++/CLI (vs2005).

Willy.




Willy Denoyette [MVP] wrote:

"E.T. Grey" <ne****@alpha-centauri.com> wrote in message
news:dk**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...

Hi,

I have a C++ DLL that I want to use from a C# project. I am actually usng
a lot of advanced C++ features like templates, partial/specialized
templates, functors and callbacks. I am also using STL containers like
std::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 extremely
grateful 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 exception
void register_callback( INT_FPTR ) ;

private:
INT_FPTR cb ;
};
Many Thanks
What do you mean by throws an exception?, "barney" is a C++ member function,



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 to
write managed C++ wrapper classes around my C++ classes - but there is
no useful examples given anywhere - most of the examples are to do with
calling 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 be
done or it has never been tried out in the real world - just be man
enough to say it instad of issuing the blanket line : "write managed C++
classes around your C++ classes". No one as yet (and that included MS
itself) afaik, has actually provided an example of using C++ classes
exported 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 it
done - does anyone actually even know?. I provided a simple no brainer
C++ DLL and expected at least some pseudocode to show how I can call the
2/3 functions from C# - apparently, no one really knows how to do this ....


这篇关于专家帮助迫切需要InterOp(Managed C ++?)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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