[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.interop

struct alignment problems in native/managed interop code

brad

3/19/2007 6:46:00 PM

This problem has been baffling me for a few days now, and I really
need to get some help.

I've used tlbimp to create a interop dll for a COM component. the
assembly which is output by tlbimp includes some struct definitions.
My problem is that the native code and managed code don't agree on the
struct alignments.

The struct alignment generally looks correct when read/written by
native code. (writing to the first field in the struct will write to
the first byte of the structure)

It looks very strange when read/written by managed code. (writing to
the first field in the struct will write to the third byte of the
structure, the field is offset by two bytes! Other fields are just as
weird).

This is causing serious problems with my interop, because the native/
managed worlds can't agree on the struct alignment. Passing a pointer
from the managed world to the native world has bad results.

I originally thought that this was a compiler optimization issue, but
optimizations are turned off.

Others have requested that I use the struct layout attribute to tackle
this problem, but when I see that used, it's usually in conjunction
with P/Invoke. I'm not using PInvoke, I'm using a tlbimp wrapper, and
I don't have the freedom to redefine the struct layout. (when I try,
I get 'class type redefinition' compilation errors).

Any ideas on what could be going wrong here? any answers will help.
Let me know if you need more information

1 Answer

Christian Fröschlin

3/20/2007 8:36:00 AM

0

brad wrote:

> with P/Invoke. I'm not using PInvoke, I'm using a tlbimp wrapper, and
> I don't have the freedom to redefine the struct layout. (when I try,
> I get 'class type redefinition' compilation errors).

tlbimp may not work out of the box with your COM library.
In such a case, you can either modify the original COM library
to make it more import-friendly, or edit the generated wrapper:
http://msdn2.microsoft.com/en-us/library/8zb...