C ++在本机代码中声明托管变量

时间:2012-07-10 18:19:58

标签: visual-c++ variables native global managed

我的.NET表单和Visual Studio中的本机代码。问题是:我无法在我的本机代码中声明我的.NET表单的全局实例,如下所示:

Editor^ maineditor;

它给了我这个问题:

error C3145: 'EditorEntry' : global or static variable may not have managed type 'Cube3D::Editor ^'

3 个答案:

答案 0 :(得分:12)

而不是使用全局静态尝试使其成为容器类型中的静态方法

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

答案 1 :(得分:8)

用gcroot<>包裹句柄struct

gcroot<Editor^> maineditor;

答案 2 :(得分:0)

你的静态课程最高(参考:Can a class be declared static in c++?

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

现在只引用该类

ManagedGlobals::xl = gcnew Excel::Application();