?
Solved

Creating a dll from a class

Posted on 2003-02-21
5
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

741 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