WahJava
2005-05-25 17:23:58 UTC
Hi hackers,
I'm investigating on how C++ objects can be accessed and invoked by
the external code (e.g. a C code, or a assembly language routine, or
some other language routines). I'm using "Microsoft 32-bit C/C++
Optimizing Compiler v. 13.10.3052". How C++ class is actually
laid out in memory ?
My half correct guess is representation as a structure is
represented. e.g.
class Msg
{
char* msg;
public:
Msg(const char*);
void print();
~Msg();
};
might be represented in C as:
struct MsgStruct
{
char* msg;
void (*construct)(struct MsgStruct*, const char*);
void (*print)(struct MsgStruct*);
void (*destruct)(struct MsgStruct*);
};
But the function pointers declared in above MsgStruct structure have to
be invoked using "thiscall" calling convention (documented in MSDN,
where "this" pointer is passed in ECX register), and "thiscall"
convention can't be explicitly. So a tweak will be needed as below:
/* Invoke method on Msg object not MsgStruct stucture */
void invoke_print_method(void* p)
{
Msg* m = (Msg*)p; /* Cast a Msg object from parameter */
void (Msg::*fn)() = &Msg::print;
unsigned** px = (unsigned**)(&fn);
__asm {
lea eax, [fn] ; Get the value of pointer, i.e. address of print()
mov ecx, [m] ; Now, set this pointer
call [eax] ; Invoke the function, since EAX contains address
; of print() method
}
}
But some of my thoughts contradicts what I've actually derived
above. That's why I've not used If we've to represent C++ member
methods as the function
pointers in C structure, then this means we've to duplicate function
pointers for each object which also leads to memory wastage. And this
means, size of C++ object is increased. But size of C++ object remains
4 bytes, whereas size of structure instance is 16 bytes (4 bytes data,
12 bytes for 3 function pointers).
Suppose I want to expose a C++ object to some C code, although that C
code can cast my C++ object to a pointer and can change its data, but
what about member methods. And is there any standard that controls
this behavior ? Or every compiler does in its own way ? Then how
member methods can be invoked ? Is there any table of function
pointers which I can locate and then invoke the function pointers ?
And by the way, how COM does it ?
Thanx in advance,
Ashish Shukla alias Wah Java !!
Wah Java !!
-----------------------------------
tsorF treboR - peels I erofeb og ot seliM
I'm investigating on how C++ objects can be accessed and invoked by
the external code (e.g. a C code, or a assembly language routine, or
some other language routines). I'm using "Microsoft 32-bit C/C++
Optimizing Compiler v. 13.10.3052". How C++ class is actually
laid out in memory ?
My half correct guess is representation as a structure is
represented. e.g.
class Msg
{
char* msg;
public:
Msg(const char*);
void print();
~Msg();
};
might be represented in C as:
struct MsgStruct
{
char* msg;
void (*construct)(struct MsgStruct*, const char*);
void (*print)(struct MsgStruct*);
void (*destruct)(struct MsgStruct*);
};
But the function pointers declared in above MsgStruct structure have to
be invoked using "thiscall" calling convention (documented in MSDN,
where "this" pointer is passed in ECX register), and "thiscall"
convention can't be explicitly. So a tweak will be needed as below:
/* Invoke method on Msg object not MsgStruct stucture */
void invoke_print_method(void* p)
{
Msg* m = (Msg*)p; /* Cast a Msg object from parameter */
void (Msg::*fn)() = &Msg::print;
unsigned** px = (unsigned**)(&fn);
__asm {
lea eax, [fn] ; Get the value of pointer, i.e. address of print()
mov ecx, [m] ; Now, set this pointer
call [eax] ; Invoke the function, since EAX contains address
; of print() method
}
}
But some of my thoughts contradicts what I've actually derived
above. That's why I've not used If we've to represent C++ member
methods as the function
pointers in C structure, then this means we've to duplicate function
pointers for each object which also leads to memory wastage. And this
means, size of C++ object is increased. But size of C++ object remains
4 bytes, whereas size of structure instance is 16 bytes (4 bytes data,
12 bytes for 3 function pointers).
Suppose I want to expose a C++ object to some C code, although that C
code can cast my C++ object to a pointer and can change its data, but
what about member methods. And is there any standard that controls
this behavior ? Or every compiler does in its own way ? Then how
member methods can be invoked ? Is there any table of function
pointers which I can locate and then invoke the function pointers ?
And by the way, how COM does it ?
Thanx in advance,
Ashish Shukla alias Wah Java !!
Wah Java !!
-----------------------------------
tsorF treboR - peels I erofeb og ot seliM