473,465 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Exposing private member functions to extern C function

I have a class that needs to be accesed by a C API. I need to expose
some private methods to the C API :
#ifdef __cplusplus
extern "C"
#endif

void peek(Object_Handle handle);
void poke(Object_Handle handle);

#ifdef __cplusplus
}
#endif
class ToBeFlattened
{
public:

//compiler barfs at the next two lines (mangled names?)
//I also cant use the extern C decoration ...
friend __stdcall void peek();
friend __stdcall void poke();

ToBeFlattened();
foo();
bar();

private:
void take_a_peek() const
void poke_all_you_want();
};
Sep 12 '07 #1
5 5032
Anonymous wrote:
I have a class that needs to be accesed by a C API. I need to expose
some private methods to the C API :
#ifdef __cplusplus
extern "C"
#endif

void peek(Object_Handle handle);
void poke(Object_Handle handle);

#ifdef __cplusplus
}
#endif
class ToBeFlattened
{
public:

//compiler barfs at the next two lines (mangled names?)
//I also cant use the extern C decoration ...
friend __stdcall void peek();
friend __stdcall void poke();
__stdcall is not a C++ keyword. You probably don't need it at all.
>
ToBeFlattened();
foo();
bar();

private:
void take_a_peek() const
void poke_all_you_want();
};
Sep 12 '07 #2
On Sep 12, 10:12 am, Anonymous <no.re...@here.comwrote:
I have a class that needs to be accesed by a C API. I need to expose
some private methods to the C API :

#ifdef __cplusplus
extern "C"
#endif

void peek(Object_Handle handle);
void poke(Object_Handle handle);

#ifdef __cplusplus}

#endif

class ToBeFlattened
{
public:

//compiler barfs at the next two lines (mangled names?)
//I also cant use the extern C decoration ...
friend __stdcall void peek();
friend __stdcall void poke();

ToBeFlattened();
foo();
bar();

private:
void take_a_peek() const
void poke_all_you_want();

};- Hide quoted text -

- Show quoted text -
You can only let C have access to global functions or static class
functions.
You can't let them have access to member functions - how would C
supply the
instance of the class object that is implicit in the calls to a member
function?
Sep 12 '07 #3
On Sep 12, 5:34 pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
On Sep 12, 10:12 am, Anonymous <no.re...@here.comwrote:
I have a class that needs to be accesed by a C API. I need to expose
some private methods to the C API :
#ifdef __cplusplus
extern "C"
That should be ``extern "C" {''.
#endif
void peek(Object_Handle handle);
void poke(Object_Handle handle);
What's Object_Handle?
#ifdef __cplusplus}
And this should be two lines (probably got mangled somewhere in
propagation).
#endif
class ToBeFlattened
{
public:
//compiler barfs at the next two lines (mangled names?)
//I also cant use the extern C decoration ...
friend __stdcall void peek();
friend __stdcall void poke();
There is absolutely no way a C program can ever call these
functions. For several reasons.
ToBeFlattened();
foo();
bar();
private:
void take_a_peek() const
void poke_all_you_want();
};
You can only let C have access to global functions or static class
functions.
C can only access functions declared ``static "C"''. Global
functions that aren't ``static "C"'' can't be used, nor can
static class functions (which can't be ``static "C"'').
You can't let them have access to member functions - how would
C supply the instance of the class object that is implicit in
the calls to a member function?
That's the most obvious problem. There's also the fact that the
calling conventions may be different between C and C++. The
result is that the "linkage" of a function is part of its type,
and if a function written in C is declared:

extern "C" void f( void (*)() ) ;

then it can only be passed functions declared and defined:

extern "C" void g() {}

In other words, given:

extern "C" {
typedef void (*PtrCFnc)() ;
}
typedef void (*PtrCppFnc)() ;

PtrCFnc and PtrCppFnc are two distinctly different types, with
no implicit conversion from one to the other.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 13 '07 #4
James Kanze wrote:
>
C can only access functions declared ``static "C"''. Global
functions that aren't ``static "C"'' can't be used, nor can
static class functions (which can't be ``static "C"'').
What's `static "C"' That's a new one on me.

I assume you intended to write `extern "C"'.

If you have to call a function with access to a class's private parts
from C, you have to declare it as extern "C" and make it a friend of the
class.

--
Ian Collins.
Sep 13 '07 #5
On Sep 13, 10:05 am, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
C can only access functions declared ``static "C"''. Global
functions that aren't ``static "C"'' can't be used, nor can
static class functions (which can't be ``static "C"'').
What's `static "C"' That's a new one on me.
A typo.
I assume you intended to write `extern "C"'.
Yes.
If you have to call a function with access to a class's
private parts from C, you have to declare it as extern "C" and
make it a friend of the class.
More often, I'll still write it as a member function, and then
write an ``extern "C"'' function which just calls the member
function. In most cases, the function whose pointer is being
passed takes a void* argument, which in fact will be a pointer
to the object, so the actual function being passed will be
something like:

extern "C"
void // Or whatever...
calledFromC( void * p )
{
static_cast< MyClass* >( p )->f() ;
}

The idiom is frequent enough that it would be worth writing a
template for it, except... function templates can't be ``extern
"C"'' either. (Maybe a macro?)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
5
by: He Shiming | last post by:
Hi, I have a question regarding memory consumption of class/object member functions. Say that I have: class A { char szAbc; char* getString(); }
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
4
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.