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

G

guy.gorodish

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
 
A

Alberto Poblacion

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.
 
G

guy.gorodish

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
 
A

Alberto Poblacion

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.
 
J

John Vottero

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.
 
G

guy.gorodish

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top