[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Accessing Unmanaged(COM DLL) Struct pointer from C#

sekhar.roy

9/5/2007 3:38:00 PM

Hi,
I'm trying to access a struct pointer from a ATL COM DLL.
The Signature of the function in C++ end is like -

C++:
STDMETHODIMP Ctest::GetPtr(DataRecord** pstData)

I have added the DLL as a referance in the project and trying to
access from C# like that -

IntPtr ds = Marshal.AllocHGlobal(4);
UnManagedLib.DataRecord dtDLL;
testClass cls = new testClass();
cls.GetPtr(ds);
dtDLL=
(UnManagedLib.DataRecord)Marshal.PtrToStructure(ds,typeof(UnManagedLib.DataRecord));

I have marshal the whole structure in C# end. My Question is what is
the best way to Set/Get data from a unmanaged nested structure ? Is it
safe to using unsafe block ? The above code is not returning proper
values.

If you have any sample code I would appreciate that.

--Sekhar
sekhar_k_roy@yahoo.com

2 Answers

(Mattias Sjögren)

9/5/2007 10:00:00 PM

0

>IntPtr ds = Marshal.AllocHGlobal(4);
>UnManagedLib.DataRecord dtDLL;
>testClass cls = new testClass();
>cls.GetPtr(ds);
>dtDLL=
>(UnManagedLib.DataRecord)Marshal.PtrToStructure(ds,typeof(UnManagedLib.DataRecord));

That should probably be

IntPtr ds = Marshal.AllocHGlobal(IntPtr.Size);
....
dtDLL=
(UnManagedLib.DataRecord)Marshal.PtrToStructure(Marshal.ReadIntPtr(ds),typeof(UnManagedLib.DataRecord));
Marshal.FreeGlobal(ds);

Or change the interop assembly so that the parameter type is out
IntPtr.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.n... | http://www.dotneti...
Please reply only to the newsgroup.

sekhar.roy

9/6/2007 4:23:00 PM

0

On Sep 5, 5:00 pm, Mattias Sj?gren <mattias.dont.want.s...@mvps.org>
wrote:
> >IntPtr ds = Marshal.AllocHGlobal(4);
> >UnManagedLib.DataRecord dtDLL;
> >testClass cls = new testClass();
> >cls.GetPtr(ds);
> >dtDLL=
> >(UnManagedLib.DataRecord)Marshal.PtrToStructure(ds,typeof(UnManagedLib.Dat?aRecord));
>
> That should probably be
>
> IntPtr ds = Marshal.AllocHGlobal(IntPtr.Size);
> ...
> dtDLL=
> (UnManagedLib.DataRecord)Marshal.PtrToStructure(Marshal.ReadIntPtr(ds),type?of(UnManagedLib.DataRecord));
> Marshal.FreeGlobal(ds);
>
> Or change the interop assembly so that the parameter type is out
> IntPtr.
>
> Mattias
>
> --
> Mattias Sj?gren [C#MVP] mattias @ mvps.orghttp://www.msjogren.n...|http://www.dotneti...
> Please reply only to the newsgroup.

Thanks Mattias for your reply.What if when I share the Unmanaged
Structure pointer using ref (from C#) ?
My requirement is all the times unmanaged(C++) Structure data and C#
structure data should be in Sync.
Now since memory management is different in C++ and C# what is the
best way to do that.

When I use ref in C# and do Set and Get from managed code then I want
my unmaged data be updated automatically.

What is the best way to do that ?

Signature in C#
cls.GetPtr(ref ds);

Signature in C++
GetData(EFEMData *pstEFEMData)