Junk data after Marshal.PtrToStructure

S

Suja

Hi All,

I have a WIN32 DLL which pass some information to a .NET based
application via the windows messages. When I try to convert this data
in the managed code, I am getting junk data. Does any have any clue on
whats wrong?. Here is the code snippet.

//From WIN32 DLL, I am passing the TOTAL_STATUS structure as wParam of
the message

#define MAX_DATA_SIZE = 100;
#define MAX_NO_STATUS = 10;
typedef struct _STATUS
{
BYTE Status1[MAX_NO_STATUS];
BYTE Status2[MAX_NO_STATUS];

}STATUS , *STATUS;
typedef struct _TOTAL_STATUS
{
STATUS Sts ;
BYTE RawSts[MAX_DATA_SIZE];

}TOTAL_STATUS, *PTOTAL_STATUS;

//In the managed code, I am defining the structure as follows

const int MAX_DATA_SIZE = 100;
const int MAX_NO_STATUS = 10;

[ StructLayout( LayoutKind.Sequential, Pack = 1 ) ]
struct _STATUS
{
[MarshalAs(UnmanagedType.ByValArray,ArraySubType = UnmanagedType.U1,
SizeConst=MAX_NO_STATUS)]
public byte[] Status1;
public byte[] Status2;
}
[ StructLayout( LayoutKind.Sequential, Pack = 1 ) ]
struct TOTAL_STATUS
{
public _STATUS Sts ;
[MarshalAs(UnmanagedType.ByValArray,ArraySubType = UnmanagedType.U1,
SizeConst=MAX_DATA_SIZE)]
public byte[] RawSts;
}

//Doing a conversion as follows
TOTAL_STATUS TotalSts = (TOTAL_STATUS)Marshal.PtrToStructure(m.WParam,
typeof(TOTAL_STATUS));

But the values in "TotalSts " are junk values( I did zero outting in
both locations and after conversion I am getting random values)

Any idea about whats wrong here?

Thanks in advance
Suja.
 
M

Mattias Sjögren

I have a WIN32 DLL which pass some information to a .NET based
application via the windows messages.

Which message? If you need to to work cross process bnundaries, it has
to be WM_COPYDATA.

[ StructLayout( LayoutKind.Sequential, Pack = 1 ) ]
struct _STATUS
{
[MarshalAs(UnmanagedType.ByValArray,ArraySubType = UnmanagedType.U1,
SizeConst=MAX_NO_STATUS)]
public byte[] Status1;
public byte[] Status2;
}

An attribute only applies to the following member, so there's no
marshaling information attached to Status2.


Mattias
 

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