Josh Anon

Hi all,I am fairly new to .net, and I'm having problems compiling something that, from what I've read, should work. I have a simple C++/CLI WinForms app with 3 classes, Form1 and Params are managed and Client is unmanaged (I'm simplifying the names for discussion). I want to call the SharedParams singleton from the unmanaged class, take its return values (sometimes of managed types like String^), pass them into other unmanaged functions, and store a pointer to an instance of the C++ class within a C++/CLI class.

The unmanaged class header looks like this:

Code Snippet

#pragma unmanaged

class Client : public AnotherCPPClass {

public:

void Connect();

...};

and in Connect, I do:

Code Snippet

void Client::Connect() {

System::String ^ip = MyNamespace::Params::SharedParams->IPAddress;

_client->doConnect( ip, 0, 0);

}

and in Form1, I have:

Code Snippet
private: Client *_client;

within the class.I've switched my compiler setting to /clr, but when I compile this, I get errors like:.

Code Snippet

\Client.cpp(18) : error C3821: System::String^': managed type or function cannot be used in an unmanaged function

.\Client.cpp(18) : error C2653: 'MyNamespace' : is not a class or namespace name

.\Client.cpp(18) : error C2065: 'Params' : undeclared identifier

.\Client.cpp(18) : error C2227: left of '->IPAddress' must point to class/struct/union/generic type type is ''unknown-type''

.\Client.cpp(22) : error C2664: 'XClient:doConnect: cannot convert parameter 1 from 'System::String^' to 'const char *'

No user-defined-conversion operator available, or Cannot convert a managed type to an unmanaged type

Form1.h(72) : error C2143: syntax error : missing ';' before '*' (this is complaint is about the pointer to the unmanaged class within Form1)

I'm guessing I am missing something simple; any help would be appreciated!Thanks.



Re: Visual C++ Language C++/CLI and C++ interop question

Bite Qiu - MSFT

you can not use managed type directly in an unmanaged class or function, you have 3 options to fix it:

1. change Client class to be a managed class

2. use string type in native c++, #include <string>

3. use gcroot<String^> so that you can use managed type in an unmanaged class, you need to #include <vcclr.h> in order to make gcroot available for use.

From the other error messages it seems you also need to change some interfaces so that you can pass a managed/unmanaged type.