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

Avatar of undefined
Last Comment
bcladd

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jayesh_j_patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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?



i_jester69

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

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes