#### Archived

This topic is now archived and is closed to further replies.

# C, C++, and DLL. I could use some help...

This topic is 5714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Generally here is what i'm trying to do. I want to write a DLL in C++ so that I can use object oriented code. I have declared a class as follows:

struct iTC2
{
virtual void GetPrg() = 0;
}

class cTCP_TC2 : public iTC2
{
//construct/deconstructor

public:
void GetPrg();
}

the cTCP_TC2 class is then defined in the file cTCP_TC2.cpp file, also in that file is an exported function called CreateTC2Driver( iTC2* &ptc2 ); The function simply sets the ptc2 = ((iTC2*)g_TCP_TC2); This is all in a DLL. now, i have c program i'm working on and i want the C program to load the DLL and call CreateTC2Driver. i understand that C does not support classes, but i was hoping to use the interface to get around that. so in the c sourse file i have a struct declared as follows:

struct iTC2
{
void (*m_fpGetPrg)(void);
};

in the C file i then use LoadLibrary and GetProcAddress to Get the exported CreateTC2Driver function. once i have that, i pass it a pointer to an iTC2 struct which works all fine and dandy. however, when i call tc2->m_fpGetPrg(); i get a general protection fault. i hope i described the situation well enough, any help would be apriciated. In short, the iTC2 as declared in the header for the C++ DLL is not the same as the iTC2 as it is declared in the C file for the program. C does not support virtual, so how do i make the two equivalent? thanks, mike. [edited by - Mike on June 29, 2002 11:06:57 PM] [edited by - Mike on June 29, 2002 11:08:00 PM]

##### Share on other sites
Your cTCP_TC2 class will store vtable pointer as the first DWORD. That pointer points to the table of virtual functions. The object doesn''t store the vtable itself (and this is how you''re trying to access it). Therefore, you need an additional dereference:

  struct iTC2_vtable{	void (*GetProg)();};struct iTC2{	iTC2_vtable *pvtbl;};

You would call GetProg like so:

tc2->pvtbl->GetProg();

And now the disclaimer: This is approximately how COM handles C/C++ interfacing, except it uses __stdcall as the calling convention. This code may be unportable. If you modify interface''s spec, be sure to modify _vtable struct as well. This code is untested, but should work.

##### Share on other sites
Thanks, that worked. Basically I was trying to create a simplified use of COM. Should i use __stdcall then to make it portable?

thanks again,
mike.

##### Share on other sites
quote:
Original post by Mike
Should i use __stdcall then to make it portable?

You might. __stdcall is also slightly faster than __cdecl, so you can use it for performance reasons as well.

##### Share on other sites
I''ve added a Release function to the class to delete the instance of the class that''s created by calling CreateTC2Device (using new). For some reason, the call to delete cause a general protection fault. any ideas?

thanks,
mike.

##### Share on other sites
Post some code, it may help.