FXCop warning with explicit struct layout

C

cody

I got a warning from fxcop from the declararation of a struct.

the warning:

"Structures with explicit layout containing misaligned
fields cause crashes on 64 bit platforms"

the struct:

[StructLayout(LayoutKind.Explicit)]
public struct STRRET
{
[FieldOffset(0)]
public UInt32 uType; // One of the STRRET_* values
[FieldOffset(4)]
public IntPtr pOleStr; // must be freed by caller of GetDisplayNameOf
[FieldOffset(4)]
public IntPtr pStr; // NOT USED
[FieldOffset(4)]
public UInt32 uOffset; // Offset into SHITEMID
[FieldOffset(4)]
public IntPtr cStr; // Buffer to fill in (ANSI)
}

Iam now wondering how to fix this potential problem without loosing union
semantics of the struct.

anyone some ideas?
 
B

Bob Powell [MVP]

Do you intend to distribute on a 64 bit platform?

AFAIK that's the only way to create a union in a struct...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
C

cody

Do you intend to distribute on a 64 bit platform?

No that is not planned yet but you never know.
AFAIK that's the only way to create a union in a struct...

So there is no portable way to do it. I hoped there is a good solution.

cody said:
I got a warning from fxcop from the declararation of a struct.

the warning:

"Structures with explicit layout containing misaligned
fields cause crashes on 64 bit platforms"

the struct:

[StructLayout(LayoutKind.Explicit)]
public struct STRRET
{
[FieldOffset(0)]
public UInt32 uType; // One of the STRRET_* values
[FieldOffset(4)]
public IntPtr pOleStr; // must be freed by caller of GetDisplayNameOf
[FieldOffset(4)]
public IntPtr pStr; // NOT USED
[FieldOffset(4)]
public UInt32 uOffset; // Offset into SHITEMID
[FieldOffset(4)]
public IntPtr cStr; // Buffer to fill in (ANSI)
}

Iam now wondering how to fix this potential problem without loosing union
semantics of the struct.

anyone some ideas?
 
C

Christof Nordiek

The problem seemse to be the IntPtr.
While it's 32-bit on 32-bit plattforms it is 64-bit on 64-bit platforms.
I don't know of thiswould be a problem for your code; it doesn't seeme so,
but you've got to decide yourself.

Christof
 

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