How to use managed variable as global in Visual C++?

731 views Asked by At

I'm writing MFC application in Visual C++ and using one C# lib. Dou to I combine unmanaged and managed classes and variables. I need some managed classes from C# put and read to/from global scope to be accessible from whole app. I tried (simple example):

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

When I use "cl1->var", i get System.NullReferenceException, MyClass2 return error

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

Please help me, how to use "Something^ var" in whole app?

1

There are 1 answers

0
Dark Falcon On BEST ANSWER

As with any other c++ static member, you need to define it in addition to declaring it. So in App.cpp at the namespace scope, you need:

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