473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Classes within DLLs?

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
Jul 22 '05 #1
4 1443

"John Alway" <jw*****@hotmail.com> ????
news:db**************************@posting.google.c om...
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
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

Jul 22 '05 #2

"John Alway" <jw*****@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
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
news:comp.os.ms-windows.programmer.win32.

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
Jul 22 '05 #3
"John Alway" <jw*****@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #4
"Ivan Vecerina" <IN*****************************@vecerina.com> wrote in message news:<ck*********@newshispeed.ch>...
"John Alway" <jw*****@hotmail.com> wrote in message
news:db**************************@posting.google.c om...
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?
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
Jul 22 '05 #5

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

Similar topics

1
by: Mehrdad | last post by:
Hello, I have a set of C# classes and I am trying to instanciate and call them in a managed C++ project. I have been searching a lot but so far could not find any example or document on how to...
1
by: Geoff Jones | last post by:
Hi I'm starting to look at C# after programming in C++ and was wondering if anybody could help me with the following questions: (1) What is the difference between a Project and Solution In...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
8
by: Mark | last post by:
I have it in my head that I saw a marketing document from Microsoft with the total number of classes and methods in the .NET Framework. Something like 70,000 methods? I don't need precise numbers,...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
3
by: Brian Bischof | last post by:
I'm having troubles getting the debugging process to work consistenly for external classes. I got it to work once and then I turned it off. But now I can't get re-enabled. Here is what I'm doing....
1
by: Oenone | last post by:
I've been working on migration of my company's VB6 ASP system to VB2005 over the last year or so, and am currently presenting my findings and recommended course of action to our management team....
3
by: jason | last post by:
Please pardon my completely lack of understanding on the topic. I have a website I developed with another developer. We are both far from experts in VB.NET and OOP. We developed the site WITHOUT...
1
by: amitdedhia | last post by:
Hi I am migrating a product code (originally written in VS2003) to VS2005. The application is written in VC++ (using MFC) and has mix of managed and unmanaged classes. It is a desktop based...
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.