C++ Classes within DLLs?

J

John Alway

Hello,

Is it possible to instantiate and use a c++ class from within a DLL?

I ask this, because I attempted to create a class within a DLL, and
export its methods via wrapper C functions, and although it all
compiles find, it returns garbage. The following is only test code to
see what I can do. I want to eventually make this functionality
available to a Visual Basic program, which keeps me from exporting a
class directly (I assume VB can't use classes).

In pure C code, this works, but somehow I can't make a class work.

//In the DLL I have two files GamePad.h and GamePad.cpp:

******First GamePad.h:

class CGamePad {
int i;
public:
GamePad() { i = 15;}
void inc(){ i++;}
void dec(){ i--;}
int get(){return i;}
void set(int _i){ i = _i;}
};


******Next GamePad.cpp

#include <windows.h>
#include <iostream>
#include "GamePad.h"
CGamePad *p_pad=0;
CGamePad g_oPad;

using namespace std;

#define DllExport extern "C" __declspec (dllexport)



DllExport void inc()
{
g_oPad.inc();

}

DllExport void dec()
{
g_oPad.dec();

}

DllExport int get()
{
i = g_oPad.get();
return i;
}

DllExport void set(int i)
{
g_oPad.set(i);
}


I call and use these functions in my C++ program, by linking in the
DLL, and the functions return garbage.

What is the reason for this, and is there a way I can employ C++
classes within a DLL, and export them through C functions?

Thanks for any feedback!


....John
 
C

constructor

John Alway said:
Hello,

Is it possible to instantiate and use a c++ class from within a DLL?

I ask this, because I attempted to create a class within a DLL, and
export its methods via wrapper C functions, and although it all
compiles find, it returns garbage. The following is only test code to
see what I can do. I want to eventually make this functionality
available to a Visual Basic program, which keeps me from exporting a
class directly (I assume VB can't use classes).

In pure C code, this works, but somehow I can't make a class work.

//In the DLL I have two files GamePad.h and GamePad.cpp:

******First GamePad.h:
// just use
class __declspec(dllexport) CGamePad{
// member functions and data members
} ;
// but it cann't be dynamic load, eg you can't use loadlibrary() function to
load the dll, just staticly load the library in your program
 
J

John Harrison

John Alway said:
Hello,

Is it possible to instantiate and use a c++ class from within a DLL?

This is a platform specific question, and therefore off topic on this group,
which deals with standard C++ only. Suggest you try

It clearly is possible because I was reviewing some code just the other day
that did exactly that. But for the details you should ask on a platform or
compiler specific group.

john
 
I

Ivan Vecerina

John Alway said:
Is it possible to instantiate and use a c++ class from within a DLL?
Yes -- but note that DLLs are platform-specific and
outside the scope of this NG.
I call and use these functions in my C++ program, by linking in the
DLL, and the functions return garbage.

What is the reason for this, and is there a way I can employ C++
classes within a DLL, and export them through C functions?

I happen use similar techniques, and it works well for me.

To locate the cause of the problem you encounter, more information
is needed about the "garbage" you are seeing.
Could it be that the problem is only that the constructor of
your global C++ variable is not called?
If you call the "set" function, what happens then?
Did you insert some logging (e.g. DebugString) calls to
verify that your functions are actually called?

Once you have investigated this, I would suggest re-posting
your question, if needed, on a platform-specific forum.
Such as microsoft.public.vc.language

hth -Ivan
 
J

John Alway

Ivan Vecerina said:
Yes -- but note that DLLs are platform-specific and
outside the scope of this NG.
I happen use similar techniques, and it works well for me.
To locate the cause of the problem you encounter, more information
is needed about the "garbage" you are seeing.
Could it be that the problem is only that the constructor of
your global C++ variable is not called?

Bingo! I'm embarrassed to say it was a typo! My constructor
should be CGamePad(), not GamePad(). So, it works.

Thanks much!

If you call the "set" function, what happens then?
Did you insert some logging (e.g. DebugString) calls to
verify that your functions are actually called?
Once you have investigated this, I would suggest re-posting
your question, if needed, on a platform-specific forum.
Such as microsoft.public.vc.language

Will do.

Regards,
...John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top