Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ declaring a managed variable in a native code

I have a .NET form, and a native code in my Visual Studio. The problem is: I can't declare a global instance of my .NET form in my native code, like this:

Editor^ maineditor;

It gives me this problem:

error C3145: 'EditorEntry' : global or static variable may not have managed type 'Cube3D::Editor ^'
like image 536
Miguel P Avatar asked Jul 10 '12 18:07

Miguel P


2 Answers

Instead of using a global static try making it a static method in a container type

ref class ManagedGlobals {
  public:
  static Editor^ maineditor = nullptr;
};
like image 55
JaredPar Avatar answered Nov 09 '22 18:11

JaredPar


wrap the handle with a gcroot<> struct

gcroot<Editor^> maineditor;
like image 32
user2242746 Avatar answered Nov 09 '22 16:11

user2242746