C++ declaring a managed variable in a native code

岁酱吖の 提交于 2019-12-10 12:39:38

问题


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

回答1:


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

ref class ManagedGlobals {
  public:
  static Editor^ maineditor = nullptr;
};



回答2:


wrap the handle with a gcroot<> struct

gcroot<Editor^> maineditor;



回答3:


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();


来源:https://stackoverflow.com/questions/11419459/c-declaring-a-managed-variable-in-a-native-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!