Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. c++ exporting class variables

c++ exporting class variables

Scheduled Pinned Locked Moved ATL / WTL / STL
c++helpcsharpdebuggingquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bfis108137
    wrote on last edited by
    #1

    I am very very new to c++. I have been allowed lol to be a part of this c++ project. I am supposed to write a c++ dll that will export to c#. Anyways I am having trouble with class variables in functions that are exported. Could someone help me pleaser with this. Fyi we are using qt framework so that's why you see a few qt things. Basically it is crashing if I access a class variable which in this case is called testInt. this is not the actual code we are working with but it does duplicate the problem exactly as it occurs in the real code. The problem is my c# code crashes when the variable testInt is accessed but not if testInt2 is accessed. header

    #ifndef QTDDLLTESTCLASSVARIABLE_H
    #define QTDDLLTESTCLASSVARIABLE_H

    #include "qtddlltestclassvariable_global.h"

    class QTDDLLTESTCLASSVARIABLE_EXPORT qtdDllTestClassVariable
    {
    public:
    qtdDllTestClassVariable();
    ~qtdDllTestClassVariable();
    void Test();
    int testInt;
    private:

    };

    #endif // QTDDLLTESTCLASSVARIABLE_H

    #include "qtddlltestclassvariable.h"
    #include

    cpp file
    qtdDllTestClassVariable::qtdDllTestClassVariable()
    {

    }

    qtdDllTestClassVariable::~qtdDllTestClassVariable()
    {

    }

    void qtdDllTestClassVariable::Test()
    {
    qDebug() << "started";
    testInt = 5;
    int testInt2 = 5;
    qDebug() << testInt << " after int";
    qDebug() << testInt2 << " after int2";
    }

    c# code

    [DllImport(@"..\..\..\Win32\Debug\qtdDllTestClassVariable.dll", CharSet = CharSet.Unicode, EntryPoint = "?Test@qtdDllTestClassVariable@@QAEXXZ")]
    public static extern void Test();

    M 1 Reply Last reply
    0
    • B bfis108137

      I am very very new to c++. I have been allowed lol to be a part of this c++ project. I am supposed to write a c++ dll that will export to c#. Anyways I am having trouble with class variables in functions that are exported. Could someone help me pleaser with this. Fyi we are using qt framework so that's why you see a few qt things. Basically it is crashing if I access a class variable which in this case is called testInt. this is not the actual code we are working with but it does duplicate the problem exactly as it occurs in the real code. The problem is my c# code crashes when the variable testInt is accessed but not if testInt2 is accessed. header

      #ifndef QTDDLLTESTCLASSVARIABLE_H
      #define QTDDLLTESTCLASSVARIABLE_H

      #include "qtddlltestclassvariable_global.h"

      class QTDDLLTESTCLASSVARIABLE_EXPORT qtdDllTestClassVariable
      {
      public:
      qtdDllTestClassVariable();
      ~qtdDllTestClassVariable();
      void Test();
      int testInt;
      private:

      };

      #endif // QTDDLLTESTCLASSVARIABLE_H

      #include "qtddlltestclassvariable.h"
      #include

      cpp file
      qtdDllTestClassVariable::qtdDllTestClassVariable()
      {

      }

      qtdDllTestClassVariable::~qtdDllTestClassVariable()
      {

      }

      void qtdDllTestClassVariable::Test()
      {
      qDebug() << "started";
      testInt = 5;
      int testInt2 = 5;
      qDebug() << testInt << " after int";
      qDebug() << testInt2 << " after int2";
      }

      c# code

      [DllImport(@"..\..\..\Win32\Debug\qtdDllTestClassVariable.dll", CharSet = CharSet.Unicode, EntryPoint = "?Test@qtdDllTestClassVariable@@QAEXXZ")]
      public static extern void Test();

      M Offline
      M Offline
      MicroVirus
      wrote on last edited by
      #2

      Using DLL Export you can export your C/C++ variables and functions to make them available. However, you can not just export an entire class and use it in C#. The direct problem here is that you are accessing a member function (Test) without an instance of your test-class being created. Probably the best way to interface in a class based manner between C++ and C# is to use COM; that is, create a COM class in C++, export it, and then use the COM class in C#.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups