Groups | Blog | Home
all groups > dotnet interop > november 2005 >

dotnet interop : pointer to a c++struct kept by c# code


antoine
11/4/2005 12:00:00 AM
hi,

i would like to know how a c# class can keep a reference (pointer) to a
structure
allocated by unmanaged c++ code.

******************************************************
c++
struct MyUnmanagedStruct
{
int foo;
}

__declspec(dllexport) void GetAPtrToAnUnmanagedStruct( MyUnmanagedStruct *
ptr);
void GetAPtrToAnUnmanageStruct( MyUnmanagedStruct * ptr)
{
ptr = new MyUnmanagedStruct();
}
******************************************************
c#
[dllimport]
unsafe void GetAPtrToAnUnmanagedStruct( MyUnmanagedStruct * ptr);

class MyUnmanagedStruct
{
int foo;
}
class StructOwner
{
MyUnmanagedStruct uStruct; // i would like this reference points to the
unmanaged struct

StructOwner()
{
unsafe
{
GetAPtrToAnUnmanagedStruct( uStruct); // actually it doesn't
work ...
}
}
}

thanks for answers

Antoine

Mattias Sjögren
11/4/2005 12:00:00 AM
[quoted text, click to view]

That will not work since you'r ejust changing the local ptr value. You
have to make it

void GetAPtrToAnUnmanageStruct( MyUnmanagedStruct ** ptr)
{
*ptr = new MyUnmanagedStruct();
}

or

MyUnmanagedStruct * GetAPtrToAnUnmanageStruct( )
{
return new MyUnmanagedStruct();
}


[quoted text, click to view]

You can never make a reference type variable point to something that
isn't allocated off the managed GC heap. You'll have to make
MyUnmanagedStruct a struct in the C# code instead and then either
store the pointer as a "real" pointer (MyUnmanagedStruct* - requires
use of unsafe code) or as an IntPtr.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
antoine
11/7/2005 10:19:38 AM
thanks Mattias but :

[quoted text, click to view]


how can I convert (cast) the IntPtr coming from the unmanaged code to my
unmanaged structure :

******************************************************
c++
******************************************************
__declspec(dllexport) void GetAPtrToAnUnmanagedStruct( MyUnmanagedStruct **
ptr);
void GetAPtrToAnUnmanageStruct( MyUnmanagedStruct ** ptr)
{
*ptr = new MyUnmanagedStruct();
}
******************************************************
c#
******************************************************
[dllImport]
void GetAPtrToAnUnmanagedStruct( ref IntPtr ptr);
struct MyUnmanagedStruct
{
int foo;
}
class StructOwner
{
IntPtr ptrToAnUnmanagedStructure;
MyUnmanagedStruct localUnmanagedStructure;

InitPtr()
{
GetAPtrToAnUnmanagedStruct( ref ptrToAnUnmanagedStructure);
}

RefreshLocalData()
{
localUnmanagedStructure = (MyUnmanagedStruct)
ptrToAnUnmanagedStructure; /// how do I cast the IntPtr ????
}
}

what I want to do is to have a memory zone shared between a unmanaged c++
dll and a c# application.
in another words, I want to have structure that can be seen and modified in
both worlds.
(I can't make a managed c++ wrapper).

thanks
Antoine


Michael Höhne
11/8/2005 9:26:13 PM
If it does not affect your existing code, you could simply create an ATL
project and implement the C++ structure as an automation object (COM) .NET
interop will do the rest for you.

Michael

"antoine" <antoine.bourdale@oktal.fr> schrieb im Newsbeitrag
news:upq37x34FHA.2552@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button