Link to home
Start Free TrialLog in
Avatar of michaelu77
michaelu77

asked on

Creating a dll from a class

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
ASKER CERTIFIED SOLUTION
Avatar of jayesh_j_patel
jayesh_j_patel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weary
weary

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?



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