PC Review


Reply
Thread Tools Rate Thread

data corrupted while transfering struct from c# interop into c function

 
 
guy.gorodish@googlemail.com
Guest
Posts: n/a
 
      6th May 2007
hi,
i have a c# interop that pass struct into c dll. for some reason the
data transfered into the c function is with incorrect order (meaning -
members of the struct get other struct values and other members get
zero)
can someone know how can i fix it?

thanks,
Guy

 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      6th May 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> i have a c# interop that pass struct into c dll. for some reason the
> data transfered into the c function is with incorrect order (meaning -
> members of the struct get other struct values and other members get
> zero)
> can someone know how can i fix it?


There is probably some difference in the way that C# and C are laying out
the structure, such as members that are not 32-bits long being aligned on
different boundaries. In C# you can apply to your struct the attributes
[StructLayout(...)] and [FieldOffset(...)] which let you fine-tune the
internal layout of the struct so that you can match the layout of the struct
in the dll.


 
Reply With Quote
 
guy.gorodish@googlemail.com
Guest
Posts: n/a
 
      6th May 2007
On May 6, 1:03 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> <guy.gorod...@googlemail.com> wrote in message
>
> news:(E-Mail Removed)...
>
> > i have a c# interop that pass struct into c dll. for some reason the
> > data transfered into the c function is with incorrect order (meaning -
> > members of the struct get other struct values and other members get
> > zero)
> > can someone know how can i fix it?

>
> There is probably some difference in the way that C# and C are laying out
> the structure, such as members that are not 32-bits long being aligned on
> different boundaries. In C# you can apply to your struct the attributes
> [StructLayout(...)] and [FieldOffset(...)] which let you fine-tune the
> internal layout of the struct so that you can match the layout of the struct
> in the dll.


i have used [StructLayout(LayoutKind.Sequential)] and it didn't
helped.
my struct contains 6 members: first two are enums and the other are
UINT32.
when i pass them into the c function the enums pass o.k but the other
members are shifted - 3rd and 4th get 0, 5th get the value the should
have been of the 3rd, and 6th get the value that should have been of
the 4th.

any suggestion?

thanks.
Guy

 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      6th May 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> i have used [StructLayout(LayoutKind.Sequential)] and it didn't
> helped.
> [...]
> any suggestion?


Instead of LayoutKind.Sequential, use LayoutKind.Explicit and then apply
to each member the attribute [FieldOffset(position)] to "move" each one to
the offset where it is expected by your dll.

 
Reply With Quote
 
John Vottero
Guest
Posts: n/a
 
      6th May 2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On May 6, 1:03 pm, "Alberto Poblacion" <earthling-
> quitaestoparacontes...@poblacion.org> wrote:
>> <guy.gorod...@googlemail.com> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>> > i have a c# interop that pass struct into c dll. for some reason the
>> > data transfered into the c function is with incorrect order (meaning -
>> > members of the struct get other struct values and other members get
>> > zero)
>> > can someone know how can i fix it?

>>
>> There is probably some difference in the way that C# and C are laying
>> out
>> the structure, such as members that are not 32-bits long being aligned on
>> different boundaries. In C# you can apply to your struct the attributes
>> [StructLayout(...)] and [FieldOffset(...)] which let you fine-tune the
>> internal layout of the struct so that you can match the layout of the
>> struct
>> in the dll.

>
> i have used [StructLayout(LayoutKind.Sequential)] and it didn't
> helped.
> my struct contains 6 members: first two are enums and the other are
> UINT32.
> when i pass them into the c function the enums pass o.k but the other
> members are shifted - 3rd and 4th get 0, 5th get the value the should
> have been of the 3rd, and 6th get the value that should have been of
> the 4th.
>
> any suggestion?
>


Your StructLayout Pack isn't set to what you expect. The Framework is
adding padding between the enums and the UINT32s, that's what the zeros are
and that's why 3 and 4 seem to be in 5th and 6th position.

 
Reply With Quote
 
guy.gorodish@googlemail.com
Guest
Posts: n/a
 
      7th May 2007
On May 7, 1:02 am, "John Vottero" <JVott...@mvpsi.com> wrote:
> <guy.gorod...@googlemail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
>
>
> > On May 6, 1:03 pm, "Alberto Poblacion" <earthling-
> > quitaestoparacontes...@poblacion.org> wrote:
> >> <guy.gorod...@googlemail.com> wrote in message

>
> >>news:(E-Mail Removed)...

>
> >> > i have a c# interop that pass struct into c dll. for some reason the
> >> > data transfered into the c function is with incorrect order (meaning -
> >> > members of the struct get other struct values and other members get
> >> > zero)
> >> > can someone know how can i fix it?

>
> >> There is probably some difference in the way that C# and C are laying
> >> out
> >> the structure, such as members that are not 32-bits long being aligned on
> >> different boundaries. In C# you can apply to your struct the attributes
> >> [StructLayout(...)] and [FieldOffset(...)] which let you fine-tune the
> >> internal layout of the struct so that you can match the layout of the
> >> struct
> >> in the dll.

>
> > i have used [StructLayout(LayoutKind.Sequential)] and it didn't
> > helped.
> > my struct contains 6 members: first two are enums and the other are
> > UINT32.
> > when i pass them into the c function the enums pass o.k but the other
> > members are shifted - 3rd and 4th get 0, 5th get the value the should
> > have been of the 3rd, and 6th get the value that should have been of
> > the 4th.

>
> > any suggestion?

>
> Your StructLayout Pack isn't set to what you expect. The Framework is
> adding padding between the enums and the UINT32s, that's what the zeros are
> and that's why 3 and 4 seem to be in 5th and 6th position.- Hide quoted text -
>
> - Show quoted text -


i have found my problem, i have defined inside the c file by mistake
one of the members as INT8 instead of INT32
so that what cause me the shifting!

thanks you all,

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using a structure data from C function returning pointer to struct vladimir Microsoft C# .NET 1 22nd Jun 2008 01:49 PM
How to call this native function with a struct in a struct ? Peter Microsoft Dot NET Compact Framework 1 22nd Mar 2007 10:06 PM
How do I create a dynamic collection @ run-time using C (struct data Type) : C++/CLI Interop Russell Mangel Microsoft VC .NET 1 7th Mar 2006 07:40 AM
Interop: union in a struct Markus Stiller Microsoft VB .NET 1 22nd Jul 2004 09:01 PM
Convert a C struct to C# (an interop problem) ~toki Microsoft C# .NET 5 20th Feb 2004 12:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:20 AM.