Groups | Blog | Home
all groups > dotnet interop > september 2004 >

dotnet interop : struct layout of SHITEMID


style
9/6/2004 12:41:37 AM
Hi

I'm trying to define the following C++ shell struct in C# (see shtypes.h or
shtypes.idl):

typedef struct _SHITEMID {
USHORT cb;
BYTE abID[1];
} SHITEMID;

where cb is the size of the whole struct (in bytes) including cb itself and
abID can have a variable length.

here is my definition (I fixed the abID size to 4 bytes - 2 bytes data and 2
bytes for a recommended terminator '\0'):

[StructLayout(LayoutKind.Sequential, Size=6)]
public struct SHITEMID
{
public UInt16 cb; // 2 bytes
public UInt16 abID; // 2 bytes
// public UInt16 terminator; // 2 bytes
public SHITEMID(short val)
{
if (val == 0)
cb = 0;
else
cb=6;
abID= val;
// terminator = 0;
}
}

This struct works fine together with the shell. But if I uncomment the two
out commented lines, windows explorer crashes. I have absolutely no idea why
this happens. As you can see, the struct has always a length of 6 bytes -
whether the lines are uncommented or not. And in both cases, the last 2
bytes are zero, right?

Any suggestions? It's driving me crazy. The terminator seems to be the key.

Thanx for your help in advance.
Thomas

PS: Remove !spam. from the mail address to contact me.

Mattias Sjögren
9/6/2004 11:44:33 PM
Thomas,

[quoted text, click to view]

How and in which context are you using the struct? Are you providing
PIDLs in for example a namespace extension, or are you just consuming
them?

Explorer doesn't care about the Size attribute you set in managed
code, it only cares about the cb member.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
style
9/7/2004 12:57:12 AM
Hello Mattias

Exactly, I'm writing a namespace extension with C# and I'm desperately
trying to send my own pidls to Windows Explorer.
Of course I know, Windows Explorer only cares about the cb member. But if I
set cb to 6 bytes, I have to ensure nevertheless that the struct really has
a length of 6 bytes in unmanaged code - which you can control with the Size
parameter in [StructLayout(LayoutKind.Sequential, Size=6)]. Otherwise my
struct delivers wrong data. If my struct is marshalled, the data alignment
of my struct has to be correct, right?

For any reason there still seems to be something wrong with that. Any
further suggestions? I'd appreciate any help.

Best regards from Switzerland
Thomas

PS: Remove !spam. from the mail address to contact me.
..

AddThis Social Bookmark Button