Link to home
Start Free TrialLog in
Avatar of donfmorrison
donfmorrison

asked on

How to create a DLL with C++.NET that can be used in VB.NET

Ok. I have this class written in C++.  I want to use it in a VB.NET project.  I need to know how to make this class into a DLL that can be used in VB.NET.  I have included the header file for my class.  I have read that the class must be "Managed" and being new to the .NET Enviroment, am having diffulty transforming my thinking to this new methodology.  I understand the basic concept behind it, but haven't quite grasped it totally, yet.  

If someone could help me get this into a DLL it would be quite helpful.  

Here is my header file:
-------------------------------------------------------------------------------------
//=========================================================== INCLUDES
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

//========================================================= DEFINITIONS
#define pi       3.141593
#define twopi    ( 2.0*pi )
#define qrtrpi   ( pi/4.0 )
#define halfpi   ( pi/2.0 )

//===================================================== DEFINE NEW TYPES
struct pt {
      double z;
      double x;
};

struct surface {
      pt beg;
      pt end;
      pt cnt;
};

struct sCARLOS {
      surface s1;
      surface s2;
      surface s3;
      surface s4;
      surface s5;
      surface s6;
      surface s7;
      surface s8;
      surface s9;
      surface s10;

      // dynamic array
      pt *sting_res;
      int sting_pts;
};

struct sEMU {
      // dynamic arrays
      pt *s1;
      pt *s2;
      pt *s3;
      pt *s4;
      pt *s5;
      pt *s6;
      pt *s7;
      pt *s8;
      pt *s9;
      pt *s10;      

      // keep track of number of dynamic pts
      int s1_pts;
      int s2_pts;
      int s3_pts;
      int s4_pts;
      int s5_pts;
      int s6_pts;
      int s7_pts;
      int s8_pts;
      int s9_pts;
      int s10_pts;
};

//====================================================== CLASS DEFINITION
class ParametricWedge {
private:
      double WedgeAngle;            // degrees
      double WedgeLength;             // inches
      double Freq;                       // GHz
      double TailRadii;                // inches
      double BodyExt;                     // wavelengths
      int    Resolution;                 // pts per wavelength

      sEMU    EMU;
        sCARLOS CARLOS;

public:
      ParametricWedge();
      ParametricWedge(double Angle, double Length, double Freq, double Radii, double Body, int Res);
      ~ParametricWedge();
      void UpdateWedgeParams(double Angle, double Length, double Freq, double Radii, double Body, int Res);      
      void MakeWedge(double Angle, double Length, double Freq, double Radii, double Body, int Res);
      void MakeWedge();
      
public: //============================================== OVERLOADED MEMBERS
      double   GetWedgeAngle() { return WedgeAngle; }
      double   GetWedgeLength() { return WedgeLength; }
      double   GetFreq() { return Freq; }
      double   GetTailRadii() { return TailRadii; }
      double   GetBodyExt() { return BodyExt; }
      int      GetResolution() { return Resolution; }
      sEMU*    GetEMU() { return &EMU; }
      sCARLOS* GetCARLOS() { return &CARLOS; }

private:
      int OddNum(int val) { return val & 1; }      

};
--------------------------------------------------------------------------------------

Thanks for taking a look at it... Don
Avatar of drichards
drichards

You need to use the managed extensions and create a managed C++ project.  Use '__gc class':

__gc class ParametricWedge {
...
};

You have to decide whether you want the structs to be value types or garbage-collected reference types (really classes in .NET).  Use either:

__gc struct pt {
...
};

to create reference types (need to use 'new' to create them) or

__value struct pt {
...
};

to create value types that are created on the stack.  Used in a managed C++ project, these types will be usable by another .NET language by adding a reference to the C++ project in the other .NET project.
struct sCARLOS cannot be a __value struct because the internal pointer will be a __gc pointer.  That is not allowed in a __value type.
Same with sEMU
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

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
Remember to create a managed C++ library - Class Library (.NET) in the project selections.
Avatar of donfmorrison

ASKER

Wow.  Thank you very much.  I have not tried this yet, but I will out the project together later today, this project just got puched back a day in place of another.  Thanks so much!
There is a bit of a difference between this and the original.  Note that the 'EMU' and 'CARLOS' members of 'ParametricWedge' are now pointers that need to be initialized in the constructors.  This is because these types needed to be reference types since they also contain pointers.
Yeah, I caught that.  I also posted another question about this project referring to some linking problems when I compile.  I am getting some unresolved external symbols. Here is a link to the question.

https://www.experts-exchange.com/questions/21142418/Unresolved-External-Symbols-HELP.html

Thanks again. I appreciate your help!
Another quick question about the modification you made...

Does the destructor need to specifically delete all the objects I create, or does the managed __gc stuff take care of that for me now?