C++ declaring a managed variable in a native code

2019-06-15 14:58发布

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 ^'

3条回答
叼着烟拽天下
2楼-- · 2019-06-15 15:14

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

ref class ManagedGlobals {
  public:
  static Editor^ maineditor = nullptr;
};
查看更多
Juvenile、少年°
3楼-- · 2019-06-15 15:19

You have your static class up top (referece: Can a class be declared static in c++?)

ref class ManagedGlobals abstract sealed {
public:
    static Excel::Application^ xl;
};

Now just reference that class

ManagedGlobals::xl = gcnew Excel::Application();
查看更多
我只想做你的唯一
4楼-- · 2019-06-15 15:30

wrap the handle with a gcroot<> struct

gcroot<Editor^> maineditor;
查看更多
登录 后发表回答