Re: Right way of passing unmanaged structures between managed and native code?

From: Vladimir Kouznetsov (vladimir.kouznetsov_at_ngrain.com)
Date: 04/01/04

  • Next message: William DePalo [MVP VC++]: "Re: VC++ vs C# Windows Forms Performance"
    Date: Wed, 31 Mar 2004 17:12:28 -0800
    
    

    Thanks a lot Ronald,

    That's a relief! I knew that __value struct may not be binary compatible
    with struct and judging by the context of the article it most probably
    should have been the topic. Though other people could be misled as I was. Is
    anywhere an explicit statement regarding the matter? That would be nice to
    know it's not going to change in the future.
    BTW why are you saying that opaqueness of the type is the guarantee of
    binary compatibility?

    thanks,
    v

    "Ronald Laeremans [MSFT]" <ronaldl@online.microsoft.com> wrote in message
    news:%23TGAna3FEHA.2208@TK2MSFTNGP09.phx.gbl...
    > This article is talking about taking (in C++.Net version 7.x terms about
    the
    > layout between:
    >
    > __gc struct Foo
    > {
    > SomeType SomeMember;
    > ...
    > };
    >
    > or
    >
    > __value struct Foo
    > {
    > SomeType SomeMember;
    > };
    >
    > And
    >
    > struct Foo
    > {
    > SomeType SomeMember;
    > }
    >
    > And NOT about the latter definition compiled either with or without the
    > /CLR switch.
    >
    > If you use ildasm on the latter type you will see that it is eximitted in
    a
    > CLR sense as an opqaue (i.e. empty type) with explicit layout and explicit
    > size with the compiler generated code directly managing the layout. Which
    is
    > indeed guaranteed to be the same in either case.
    >
    > Ronald
    >
    > "Vladimir Kouznetsov" <vladimir.kouznetsov@ngrain.com> wrote in message
    > news:OhyZgA2FEHA.3640@tk2msftngp13.phx.gbl...
    > > Thank you Ronald,
    > >
    > > I found the following phrase
    > >
    >
    (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstecha
    > > rt/html/vbtchtroubleshootingnetinteroperability.asp "Troubleshooting
    .NET
    > > Interoperability"): "Because Visual Studio .NET optimizes the way data
    is
    > > stored, the unmanaged representation of the structure does not always
    > match
    > > the managed representation". From that I concluded that either alignment
    > or
    > > order of members or both can be different for managed and unmanaged
    code.
    > If
    > > I'm misinterpreting that, that probably should be clarified. Otherwise
    if
    > > that is incorrect I'm happy to know that.
    > >
    > > thanks,
    > > v
    > >
    > > "Ronald Laeremans [MSFT]" <ronaldl@online.microsoft.com> wrote in
    message
    > > news:eFothx1FEHA.3064@tk2msftngp13.phx.gbl...
    > > > Hi Vladimir,
    > > >
    > > > Unmanaged types are guaranteed to have the same representation
    > regardless
    > > of
    > > > whether they are compiled with or without the /clr switch. What
    > > > documentation or experiment makes you believe otherwise?
    > > >
    > > > Ronald Laeremans
    > > > Visual C++ team
    > > >
    > > > "Vladimir Kouznetsov" <vladimir.kouznetsov@ngrain.com> wrote in
    message
    > > > news:%23hbHnueFEHA.3032@TK2MSFTNGP09.phx.gbl...
    > > > > Hi group,
    > > > >
    > > > > It seems there are no guarantees that the same unmanaged structures
    > have
    > > > the
    > > > > same binary representation for managed and native code. How do I use
    > > them
    > > > in
    > > > > mixed-code assemblies? Is there just some compiler magic behind
    > curtains
    > > > > (IJW, some implicit marshalling, which can lead to a performance
    > > > > degradation) or the data are binary compatible in these scenarios or
    I
    > > > > perhaps should use attributes to explicitly specify the layout
    (which
    > > BTW
    > > > > may depend on compiler switches increasing complexity of
    maintenance)?
    > > > >
    > > > > thanks,
    > > > > v
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >


  • Next message: William DePalo [MVP VC++]: "Re: VC++ vs C# Windows Forms Performance"

    Relevant Pages

    • Re: Right way of passing unmanaged structures between managed and native code?
      ... The note about compatibility of __value struct was really useful, ... > not is guaranteed to use the same layout for native structs/classes. ... > compiler is binary compatible, since they went to pains to make it so, but ...
      (microsoft.public.dotnet.languages.vc)
    • Re: struct stat
      ... assembler declarations, using sizeof and offsetof. ... importance of version-to-version binary compatibility. ... redefining a "public" interface like `struct stat' seems like ... executables, it changes the magic in the executable file (or ...
      (comp.unix.programmer)
    • struct memory layout
      ... I want to make a struct in C# that has binary compatibility with an ... public char char0; ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Casting Byte Buffer returned from CeReadRecordPropsEx to CEPROPVAL in C#
      ... use explicit layout and put the members where you need them. ... UInt16 uiVal; // UInt16 ... IntPtr tmp = new IntPtr; ... internal struct CEBLOB ...
      (microsoft.public.pocketpc.developer)
    • Re: Pre-offsetof() question
      ... > now-standard offsetof() macro. ... consider the typical layout of ... pre-offsetofcompiler decide to make ... > the layout of the struct vary, ...
      (comp.lang.c)