0

I have a library a friend wrote in C# and I need to use it in C++.NET. I'm new to VC++.NET and I don't know how to declare my class so it can be used by all functions in my file.. Here's my code..

using namespace System;
using namespace ONEAPI;
namespace Bot{

    void InitializeBot();
    ONEAPI::Connection connection(true);
    int main(array<System::String ^> ^args)
    {
        Console::BackgroundColor = ConsoleColor::Black;
        InitializeBot();
        return 0;
    }
    void InitializeBot(){
        connection.StartConnection("127.0.0.1", 1274, "username",
            "B73955EE7A30E959819BAE2392F6F4441DF98C66A4026EF55287A81D1F815504",
            "R0Bo");
    }
}

I get this error:

Error 1 error C3145: 'connection' : global or static variable may not have managed type 'ONEAPI::Connection' Visual Studio 2010\Projects\1hubBot\1hubBot\1hubBot.cpp 10 1 1hubBot

3
  • 2
    In future please just put the text in the question, rather than using Pastie. I've edited it in for you this time... Commented Jul 6, 2011 at 19:44
  • Thanks, I didn't know how to use the code thing. Commented Jul 6, 2011 at 19:49
  • @Troy: All you have to do is paste in the code, select it all, and click the "code format" button in the toolbar. It looks like a pair of curly braces: {}. All it's doing is indenting each line with 4 spaces--that's what the formatting engine uses to detect code blocks. Commented Jul 7, 2011 at 8:17

2 Answers 2

2

You can't use a managed type ("Connection") within a static or global - it has to exist inside of a ref class or as a local. This is a requirement of C++/CLI.

You'll need to put your code into a managed class, and use it there. I recommend going through a C++/CLI Tutorial, as it will explain this fairly quickly.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can't have managed types at global- or file-scope. Wrap the Connection object in an unmanaged (regular C++-style) class and create a global instance of that instead.

EDIT: After looking up the appropriate compiler mumbo-jumbo, here's what I came up with:

#include <vcclr.h>
using namespace System;
using namespace ONEAPI;

namespace Bot {
    class ConnectionWrapper {
    public:
        static gcroot<ONEAPI::Connection^> connection;
    };

    gcroot<ONEAPI::Connection^> ConnectionWrapper::connection = gcnew Connection(true);

    void InitializeBot() {
        ConnectionWrapper::connection->StartConnection(...);
    }
}

3 Comments

No need to create a global instance of a dummy class if he puts it into a static field of said class.
Now it says "error C3265: cannot declare a managed 'connection' in an unmanaged 'ConnectionWrapper'
Try using gcroot<Connection> like the MSDN page for C3265 suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.