C ++类的C接口 [英] C interface to C++ classes

查看:115
本文介绍了C ++类的C接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组C ++类,我想为它们提供一个C API - 并且

编译成一个C DLL,用于一个只能用于
$的应用程序b $ ba C接口DLL。


有关如何继续的任何建议/指示。谷歌搜索没有任何结果

直接解决问题的任何事情(parashift信息很有用,

但不具体处理,与isse有关)

I have a set of C++ classes for which I want to provide a C API - and
compile into a C DLL, for use in an application that can only work with
a C interface DLL.

Any suggestions/pointers on how to proceed. Googling is not bringng
anything up that directly addresses the issue (parashift info is useful,
but does not deal specifically, with the isse)

推荐答案

>我有一组C ++类,我想为它提供一个C API - 并且
> I have a set of C++ classes for which I want to provide a C API - and
编译成一个C DLL,用于只能与一个C接口DLL一起使用的应用程序。

关于如何进行的任何建议/指示。谷歌搜索不是直接解决问题的任何事情(转发信息是有用的,
但没有具体处理,与isse)
compile into a C DLL, for use in an application that can only work with
a C interface DLL.

Any suggestions/pointers on how to proceed. Googling is not bringng
anything up that directly addresses the issue (parashift info is useful,
but does not deal specifically, with the isse)




你有没有使用LabVIEW? :-)

我做了类似的事情,我创建了一个带有C函数的DLL,它们将b / b
转发给C ++对象。而不是使用''new''我提供了一个

函数创建了一个对象,然后提供了指针作为输出

参数。

对于每个函数,你必须提供指针。


类似于:


int fun(object * myObject,int arg1,int arg2)

{

myObject-> fun(arg1,arg2);

}


如果你想要的就是拥有一对一的函数映射,你可能会创建一个接受类定义的脚本,然后为它生成包装器

。 br />

当然这只有在你的类使用简单数据类型传递

数据时才有效,因为你不能使用std :: string和其他来传递数据

你的app和dll。你也必须小心结构,因为一些编程语言不能创建所有类型的结构。


我知道这不是你想要的听到,但除非您的数据和

你的课程都是微不足道的,你必须手工完成很多事情。

你还需要知道具体的事情吗?


-


亲切的问候,

布鲁诺。
BR ******************** **@hotmail.com

仅删除_nos_pam



You are not using LabVIEW by any chance? :-)
I have done something similar, and I created a dll with C functions that
forwarded the arguments to C++ objects. instead of using ''new'' i provided a
function that created an object and then supplied the pointer as an output
parameter.
for each function you have to supply that pointer.

something like:

int fun(object* myObject, int arg1, int arg2)
{
myObject->fun(arg1, arg2);
}

If all you want is to have a 1 on 1 mapping of functions, you could probably
create a script that takes the class definitions, and then generates wrappers
for it.

Of course this only works if your classes use simple datatypes for passing
data because you cannot use std::string and others for passing data between
your app and dll. you also have to be careful with structures, since some
programming languages cannot create all types of structure.

I know that this is not what you wanted to hear, but unless your data and
your classes are trivial, You have to do a lot of things by hand.
Are there specific things that you still need to know?

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"


2006年4月18日星期二12 :55:21 +0100,位字节< fl ** @ flop.com>写道:
On Tue, 18 Apr 2006 12:55:21 +0100, Bit byte <fl**@flop.com> wrote:
我有一组C ++类,我想为它提供一个C API - 并且编译成一个C DLL,用于只能运行的应用程序使用
C接口DLL。

有关如何继续的任何建议/指示。谷歌搜索不是直接解决问题的任何事情(转发信息是有用的,
但没有具体处理,与isse)
I have a set of C++ classes for which I want to provide a C API - and
compile into a C DLL, for use in an application that can only work with
a C interface DLL.

Any suggestions/pointers on how to proceed. Googling is not bringng
anything up that directly addresses the issue (parashift info is useful,
but does not deal specifically, with the isse)




你必须创建一组映射到C ++对应的C函数,

并且你必须以某种方式传递this指针。像

这样的东西应该可行:


class X

{

public:


X();


void f();

};


// C接口


#ifdef __cplusplus

extern" C" {

#endif


//类型安全,不透明指针。

typedef struct C_X_ * X_Handle;


//使用__declspec导出这些,例如


X_Handle CreateX();

void DestroyX(X_Handle x);


无效X_f(X_Handle x);


#ifdef __cplusplus

}

#endif


// C ++实现


X_Handle CreateX()

{

return reinterpret_cast< X_Handle>(新X);

}


无效DestroyX(X_Handle x)

{

删除reinterpret_cast< X *>(x);

}


void X_f(X_Handle x)

{

reinterpret_cast< X *>(x) - > f();

}


只需确保你只在X_Handle中存储X *,反之亦然。哦,

和你的C ++包装器函数不应该允许异常通过C函数传播出来。

;他们应该返回错误代码。


-

Doug Harrison

Visual C ++ MVP



You''ll have to create a set of C functions that map to C++ counterparts,
and you''ll have to pass the this pointer around in some way. Something like
this should work:

class X
{
public:

X();

void f();
};

// C interface

#ifdef __cplusplus
extern "C" {
#endif

// Type-safe, opaque pointer.
typedef struct C_X_* X_Handle;

// Export these using __declspec, for example

X_Handle CreateX();
void DestroyX(X_Handle x);

void X_f(X_Handle x);

#ifdef __cplusplus
}
#endif

// C++ implementation

X_Handle CreateX()
{
return reinterpret_cast<X_Handle>(new X);
}

void DestroyX(X_Handle x)
{
delete reinterpret_cast<X*>(x);
}

void X_f(X_Handle x)
{
reinterpret_cast<X*>(x)->f();
}

Just make sure you only ever store an X* in an X_Handle and vice versa. Oh,
and your C++ wrapper functions should not allow exceptions to propagate out
through C functions; they should return error codes instead.

--
Doug Harrison
Visual C++ MVP





Doug Harrison [MVP]写道:


Doug Harrison [MVP] wrote:
On Tue,2006年4月18日12:55:21 + 0100,位字节< fl ** @ flop.com>写道:

On Tue, 18 Apr 2006 12:55:21 +0100, Bit byte <fl**@flop.com> wrote:

我有一组C ++类,我想为它们提供一个C API - 并且编译成一个C DLL,用于一个只适用于C接口DLL的应用程序。

有关如何继续的任何建议/指示。谷歌搜索没有任何直接解决问题的任何事情(parashift信息很有用,
但没有具体处理,与isse)
I have a set of C++ classes for which I want to provide a C API - and
compile into a C DLL, for use in an application that can only work with
a C interface DLL.

Any suggestions/pointers on how to proceed. Googling is not bringng
anything up that directly addresses the issue (parashift info is useful,
but does not deal specifically, with the isse)



你''我必须创建一组映射到C ++对应的C函数,然后你必须以某种方式传递this指针。像
这样的东西应该有用:

X类
{
公开:

X();
void f();
};

// C界面

#ifdef __cplusplus
extern" C" {
#endif

//类型安全,不透明指针。
typedef struct C_X_ * X_Handle;

//使用__declspec导出这些,示例

X_Handle CreateX();
void DestroyX(X_Handle x);

void X_f(X_Handle x);

#ifdef __cplusplus
}
#endif

// C ++实现

X_Handle CreateX()
返回reinterpret_cast< X_Handle> (新X);


void DestroyX(X_Handle x)
{
删除reinterpret_cast< X *>(x);
}

void X_f(X_Handle x)
{
reinterpret_cast< X *>(x) - > f();
}
只需确保您只在X_Handle中存储X *,反之亦然。哦,
和你的C ++包装函数不应该允许异常通过C函数传播出去;他们应该返回错误代码。


You''ll have to create a set of C functions that map to C++ counterparts,
and you''ll have to pass the this pointer around in some way. Something like
this should work:

class X
{
public:

X();

void f();
};

// C interface

#ifdef __cplusplus
extern "C" {
#endif

// Type-safe, opaque pointer.
typedef struct C_X_* X_Handle;

// Export these using __declspec, for example

X_Handle CreateX();
void DestroyX(X_Handle x);

void X_f(X_Handle x);

#ifdef __cplusplus
}
#endif

// C++ implementation

X_Handle CreateX()
{
return reinterpret_cast<X_Handle>(new X);
}

void DestroyX(X_Handle x)
{
delete reinterpret_cast<X*>(x);
}

void X_f(X_Handle x)
{
reinterpret_cast<X*>(x)->f();
}

Just make sure you only ever store an X* in an X_Handle and vice versa. Oh,
and your C++ wrapper functions should not allow exceptions to propagate out
through C functions; they should return error codes instead.




嗨Doug,


谢谢你。这是我正在寻找的skinda东西。


Npow,是否有任何陷阱等等,如果是这样的话:


1)。虚拟方法 - 可以''导出类''类X(在你的

例子中给出),包含虚拟方法?


2)。在派生类上调用方法(可以'暴露clsss''类X

是从另一个C ++类派生出来的吗?) - 即类X:public Y,

或can你只将C接口公开给那些没有从另一个类继承

的类。


3)。你认为哪个ff是更好的方法:

(i)。创建一个C ++ DLL,然后创建一个使用C ++ DLL的C DLL

(ii)。使用DLL中导出的C API编译C ++类 - 即

一个Dll


4)。如何在C API中实现回调函数?

假设我们有ff:


void(CB_FUNC *)(const type1,cponst type2 *) ;


// C ++标题

class X {

public:

X();

virtual~X(); //< - 虚拟好吗?

registerCallback(CB_FUNC,void * data); //如何导出

从C API中使用?


};



Hi Doug,

Thanks for that. This is the skinda stuff I was looking for.

Npow, are there any gotchas etc w.r.t the ff:

1). virtual methods - can ''exported class '' class X (given in your
example), contain virtual methods?

2). invoking methods on derived classes (can ''exposed clsss'' class X
have been derived from another C++ class ? ) - i.e. class X : public Y,
or can you only expose a C interface to classes that are not inheriting
from another class.?

3). Which of the ff do you reckon is the better approach:
(i). Create a C++ DLL and then create a C DLL that uses the C++ DLL
(ii). Compile the C++ classes with an exported C API in the DLL - i.e.
one Dll

4). How can callback functions be implemented in the C API?
suppose we have the ff:

void (CB_FUNC*)(const type1, cponst type2*);

//C++ header
class X {
public:
X();
virtual ~X(); // <- is virtual ok ?

registerCallback(CB_FUNC, void* data); //how is this exported
an used from the C API ?

};


这篇关于C ++类的C接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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