Reputation: 11
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
Upvotes: 0
Views: 641
Reputation: 3423
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(...);
}
}
Upvotes: -1
Reputation: 564363
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.
Upvotes: 2