struct pointers?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can somone tell me why one senario works but not the other?

namespace mynamespaceA
{
[StructLayout(LayoutKind.Sequential)]
public unsafe struct structA
{
public int a1;
}
public unsafe class classA
{
public unsafe void FunctionA(void* ptrA)
{
structA* castPtrA = (structA*)ptrA;
// this will fail!!!!!!!!!!!! WHY???????????????
}
}
}


namespace mynamespaceB
{
public unsafe class classB
{
public void FunctionB(void* ptrB)
{
structB* castPtrB = (structB*)ptrB;
// this will work l!!!!!!!!!!!!
}

[StructLayout(LayoutKind.Sequential)]
public unsafe struct structB
{
public int b1;
}
}
}


Q2: why wont the following work:

namespace mynamespaceA
{
[StructLayout(LayoutKind.Sequential)]
public unsafe struct structA
{
public int a1;

public void FunctionA(void* ptrA)
{
structA* castPtrA = (structA*)ptrA;
// this will fail!!!!!!!!!!!! WHY???????????????
}
}
}


Yes I know I like unsafe too much :)
 
Grr....
Never mind....

It was because I used an attribute on a field
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string someField;

The .net 2.0 fix:
public fixed char someField[64];

It is still odd that it alowed me to use it if I embeded it into an object.
hummm...


Thanks all for trying to figure out my code with the one or two details
missing. :)
 
Back
Top