>__declspec(dllexport) void GetAPtrToAnUnmanagedStruct( MyUnmanagedStruct *
>ptr);
>void GetAPtrToAnUnmanageStruct( MyUnmanagedStruct * ptr)
>{
> ptr = new MyUnmanagedStruct();
>}
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();
}
>class MyUnmanagedStruct
>{
[quoted text clipped - 4 lines]
> MyUnmanagedStruct uStruct; // i would like this reference points to the
>unmanaged struct
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

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
antoine - 07 Nov 2005 09:19 GMT
thanks Mattias but :
>>class MyUnmanagedStruct
>>{
[quoted text clipped - 11 lines]
> store the pointer as a "real" pointer (MyUnmanagedStruct* - requires
> use of unsafe code) or as an IntPtr.
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 - 08 Nov 2005 20:26 GMT
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
> thanks Mattias but :
>
[quoted text clipped - 60 lines]
> thanks
> Antoine