iterationx

Hi,
I'm trying to pass a managed type into unmanaged code and assign it.

//unmanaged code
client2Dlg.h
gcroot<ManagedType^> m;

client2Dlg.cpp
int CClient2Dlg::loadManaged(gcroot<ManagedType^> refm)
{
m = refm; //
return 0;
}

I got an error cannot convert from 'System:: Object ^' to 'Client2Dlg ^'

I also tried m = &refm , and m = %refm and they both failed.

Any ideas Thanks,
Patrick



Re: Visual C++ Language gcroot<ManagedType^> m assignment

Nishant Sivakumar

You can change

loadManaged(gcroot<ManagedType^> refm)

to

loadManaged(ManagedType^ refm)

and then do

m = refm.





Re: Visual C++ Language gcroot<ManagedType^> m assignment

iterationx

When I do
int CClient2Dlg::loadManaged(ManagedType^ refm);
I get error C3645: 'CClient2Dlg::loadManaged' : __clrcall cannot be used on functions compiled to native code. Do you have any thoughts Thanks,
Patrick




Re: Visual C++ Language gcroot<ManagedType^> m assignment

Nishant Sivakumar

Oh, is that code in a #pragma unmanaged block Or perhaps in a cpp file without /clr compilation turned on





Re: Visual C++ Language gcroot<ManagedType^> m assignment

Nishant Sivakumar

The following code compiles fine :-

ref class R
{
};

#pragma unmanaged

class N
{
gcroot<R^> _r;
public:
void Assign(gcroot<R^> r)
{
_r = r;
}
};

#pragma managed

int main(array<System :: String ^> ^args)
{
N n;
n.Assign(gcnew R());
return 0;
}





Re: Visual C++ Language gcroot<ManagedType^> m assignment

iterationx

Hi,
I got your sample to work, the only difference between yours and mine is my forward declarations.

client2Dlg.h
#ifndef CLIENT2DLG_H //unmanaged class
#define CLIENT2DLG_H

#pragma managed
ref class managedClient2Dlg; //forward declaration
#pragma unmanaged
int loadManaged(gcroot<managedClient2Dlg^> r);
gcroot<managedClient2Dlg^> _r;

client2Dlg.cpp
#include "client2Dlg.h"
#pragma managed
ref class managedClient2Dlg; //forward declaration

#pragma unmanaged

int CClient2Dlg::loadManaged(gcroot<managedClient2Dlg^> r)
{
_r = r; //error C2440
return 0;
}

And I get, error C2440: 'static_cast' : cannot convert from 'System:: Object ^' to 'Client2Dlg ^'

Also I tried your sample and it worked except when I added a function to the managed class and called it from N.Assign(gcroot<R^> r), it wouldn't compile:
error C3821: managed type or function cannot be used in an unmanaged function
error C3642: 'void ManagedType::HelloDotNet(void)' : cannot call a function with __clrcall calling convention from native code

Thanks for helping me out again.
Patrick







Re: Visual C++ Language gcroot<ManagedType^> m assignment

Nishant Sivakumar

Patrick,

May I ask why you are using #pragma unmanaged in this case Since you need to directly access and manipulate a managed class (via gcroot) it doesn't seem to make much sense to mark those code blocks as unmanaged.

If you really do need to do that, you could refactor part of the code that accesses the managed object into a separate helper class. But in your current design, all this managed/unmanaged shifting sounds like asking for trouble.





Re: Visual C++ Language gcroot<ManagedType^> m assignment

iterationx

The unmanaged class contains a socket interface, which needs to get to my VB program. So here's what I thought:

The socket data comes into the unmanaged socket interface code then it propagates the data to the managed block via a unmanaged to managed function call, then the managed C++ function propagates the data to the VB program.

If I made a managed helper class, I would still have the problem of calling managed functions from unmanaged code I think.
Thanks,
Patrick






Re: Visual C++ Language gcroot<ManagedType^> m assignment

Nishant Sivakumar

Patrick,

Just because you are using a non-ref class does not mean you have to put it in a #pragma unmanaged block. And I doubt if that would be a major factor as far as performance is concerned - considering that the bottleneck is most likely to be the socket connection.

What happens if you remove all the #pragma unmanaged from the code Try and get that working first and then gradually mark safe blocks of code as #pragma unmanaged.