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
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