Solved

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

Posted on 2004-09-22
9
795 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:donfmorrison
  • 6
  • 3
9 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12129764
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.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12129846
struct sCARLOS cannot be a __value struct because the internal pointer will be a __gc pointer.  That is not allowed in a __value type.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12129856
Same with sEMU
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 12129913
Here's a converted header:

#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
namespace <namespace name here>
{
public __value struct pt {
     double z;
     double x;
};

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

public __gc 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 __gc[];
     int sting_pts;
};

public __gc struct sEMU {
     // dynamic arrays
     pt s1 __gc[];
     pt s2 __gc[];
     pt s3 __gc[];
     pt s4 __gc[];
     pt s5 __gc[];
     pt s6 __gc[];
     pt s7 __gc[];
     pt s8 __gc[];
     pt s9 __gc[];
     pt s10 __gc[];

     // 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
public __gc 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; }    

};
}

You will, of course, need to implement the constructors, etc.  These types can then be referenced from VB.NET or C# projects.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12129920
Remember to create a managed C++ library - Class Library (.NET) in the project selections.
0
 
LVL 1

Author Comment

by:donfmorrison
ID: 12133149
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!
0
 
LVL 19

Expert Comment

by:drichards
ID: 12134092
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.
0
 
LVL 1

Author Comment

by:donfmorrison
ID: 12134238
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.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VisualC_PLUS_PLUS_DOT_NET/Q_21142418.html

Thanks again. I appreciate your help!
0
 
LVL 1

Author Comment

by:donfmorrison
ID: 12134266
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?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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