Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

pointer to a c++struct kept by c# code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
antoine - 04 Nov 2005 13:16 GMT
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 - 04 Nov 2005 17:20 GMT
>__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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.