473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Junk data after Marshal.PtrToStructure

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.

Sep 13 '06 #1
1 1922
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ken Allen | last post by:
The documentation is not clear on the exact behaviour of the Marshal.PtrToStructure method and whether it copies the contents of the IntPtr region to a new managed object or whether it creates the...
1
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
1
by: Jay | last post by:
Hi, In my application, C++ dll is posting some message,which is processed by a form in C# ,where I use Message.Lparam to convert it in structure using Marshal.PtrToStructure() mehtod ,but it...
3
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
2
by: pedrito | last post by:
I've got an app that takes a URL as an argument. If a second instance of my application is started with a URL argument, instead of starting up, it sends the URL to the first instance. ...
2
by: =?Utf-8?B?dmxhZGltaXI=?= | last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll functions do: 1) allocate global memory for internal structures 2) controls dll subsystem (communicate by sockets,...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.