?
Solved

Creating a dll from a class

Posted on 2003-02-21
5
Medium Priority
?
253 Views
Last Modified: 2010-04-02
Hello,

I have a class and want to make it available fpr Visual Basic in a dll.
It is a mathematical class and the dll should create one instance of it. The dll-functions are controling the class. I have written the following code. But when I call the dll from VB the program shuts down.

This is the C++ dll-File:

#include <windows.h>
#include "stdafx.h"
#include "stdio.h"
#include "cStoppingpower.h"

// Instance of the Class cStopping
// not available from outside - is controlled by x_ functions
cStopping xx;

void _stdcall x_setEnv(int z1, double m1, int z2, double E_max, int unterteilungen);

BOOL WINAPI DllEntryPoint ( HINSTANCE hDLL, DWORD dwREASON, LPVOID Reserved )
{
     return TRUE;
}


void _stdcall x_setEnv(int z1, double m1, int z2, double E_max, int unterteilungen)
{
 xx.setEnv( z1,  m1,  z2,  E_max,  unterteilungen);
}

What am I doing wrong? I think the error lies in this line "cStopping xx;" - but why can`t I create an instance of a class in an dll?

Thx

Michael U.


PS: Here is the headerfile cStoppingpower.h
#ifndef H_STOPPING
#define H_STOPPING

#include <windows.h>
#include "cKonstanten.h"
#include <math.h>
#include <vector>

class cStopping
{
private:
     cKonstanten konst;
     std::vector<double> stoppingpower;
     double obergrenze, energystep;

     double amax1(double value1, double value2);
     double amin1(double value1, double value2);

     double rpstop(int z2, double energy);

     void rstop(int z1, double m1, int z2, double E_max, int unterteilungen);

public:
      cStopping();
     
      cStopping(int z1, double m1, int z2, double E_max, int unterteilungen);
     
     ~cStopping() {}

     void  setEnv(int z1, double m1, int z2, double E_max, int unterteilungen);

     double  getSP(double energy);

     void  multSP(double faktor);

     double  obergr() { return obergrenze; }
     
     double  SP(int index);
     
     double  EN(int index);
};

#endif
0
Comment
Question by:michaelu77
4 Comments
 

Accepted Solution

by:
jayesh_j_patel earned 200 total points
ID: 7995189
I am not sure that are you calling those dll functions properly from vb application. But another solution is to make this vc dll as activex dll and then create an object of that and call methods/functions. Hope that will be very easy.
Thanks,
Jayesh
0
 
LVL 1

Expert Comment

by:weary
ID: 7995683
I agree it's likely not a good idea to call this export from VB as calling conventions might be different etc. A COM dll would be the best.

I'm not sure how or when global members of DLLs are initialized. I'm thinking you might want to have a global pointer, that you initialize in the DLLentrypoint.  

Can anyone else explain when the constructor for this object would be called, if it is declared as it is now?

And what happens when the dll is unloaded/loaded?



0
 
LVL 2

Expert Comment

by:i_jester69
ID: 8030008
VB Always automatically looks for an entry point into a DLL named 'DLLMain'.

Using non-Com classes from VB is problematic at best. VB is designed for Com, so using it to create classes in a DLL would be the best choice.  
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9784107
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to  jayesh_j_patel

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question