【问题标题】:How to use managed variable as global in Visual C++?如何在 Visual C++ 中将托管变量用作全局变量?
【发布时间】:2014-05-29 19:42:29
【问题描述】:

我正在用 Visual C++ 编写 MFC 应用程序并使用一个 C# 库。 Dou to I 结合了非托管和托管的类和变量。我需要一些来自 C# 的托管类 put 和 read to/from global scope 可以从整个应用程序访问。我试过了(简单的例子):

App.h:
    class MyClass1 {
        public:
            gcroot<Namespace::Something^> var;
        };

    class MyClass2 {
        public:
            static gcroot<Namespace::Something^> var;
        };

    extern MyClass1 *cl1;

App.cpp
    MyClass1 *cl1 = new MyClass1();

当我使用“cl1->var”时,我得到 System.NullReferenceException,MyClass2 返回错误

error LNK2020: unresolved token (0A0003BE) "public: static struct gcroot ...
error LNK2001: unresolved external symbol "public: static struct gcroot ...

请帮助我,如何在整个应用程序中使用“Something^ var”?

【问题讨论】:

    标签: c# c++ visual-c++ clr managed-code


    【解决方案1】:

    与任何其他 c++ 静态成员一样,除了声明它之外,您还需要定义它。所以在命名空间范围内的 App.cpp 中,你需要:

    gcroot<Namespace::Something^> MyClass2::var;
    

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2015-06-12
      • 2012-12-31
      • 1970-01-01
      相关资源
      最近更新 更多