473,396 Members | 2,158 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,396 software developers and data experts.

Marshal.PtrToStructure complains about Null Reference

public const int FRAME_LENGTH=144;
[StructLayout(LayoutKind.Sequential, Pack=1)]
public class FRAME_T
{
public byte []Block = new byte[FRAME_LENGTH]; }

public void MyDataHandler(IntPtr appContext, IntPtr data)
FRAME_T frame = new FRAME_T();
Marshal.PtrToStructure(data, frame);

This code throws an exception on the PtrToStructure saying there is a
null reference.

What am I doing wrong?

THANKS!!!

John
Nov 16 '05 #1
2 3008
Hi John,

I can only guess at what is going wrong. I see two possible errors. One
being that the array is managed and not of constant length. The other being
that you use a class instead of a struct. Perhaps you should try the
following:

[ StructLayout( LayoutKind.Sequential, Pack = 1 ) ]
public struct Frame_t
{
[ MarshalAs( UnmanagedType.ByValArray,
ArraySubType = UnmanagedType.U1,
SizeConst = 144 ) ]
public byte[] Block;
}

//...
public void MyDataHandler( IntPtr appContext, IntPtr data )
{
Frame_t frame = ( Frame_t ) Marshal.PtrToStructure (
data,
typeof( Frame_t )
);
// ...
}

That should work.

HTH,
Tom T.

"John" wrote:
public const int FRAME_LENGTH=144;
[StructLayout(LayoutKind.Sequential, Pack=1)]
public class FRAME_T
{
public byte []Block = new byte[FRAME_LENGTH]; }

public void MyDataHandler(IntPtr appContext, IntPtr data)
FRAME_T frame = new FRAME_T();
Marshal.PtrToStructure(data, frame);

This code throws an exception on the PtrToStructure saying there is a
null reference.

What am I doing wrong?

THANKS!!!

John

Nov 16 '05 #2
Dude, you rock!!!! THANK YOU!!! That worked. I spent over 4 hours
yesterday trying to make this work and I couldn't figure it out.

Please email your mailing address to me so that I may send you a gift.

THANK YOU! THANK YOU!

John

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmail.com|/\|@P$0_|\|_> wrote in message news:<77**********************************@microso ft.com>...
Hi John,

I can only guess at what is going wrong. I see two possible errors. One
being that the array is managed and not of constant length. The other being
that you use a class instead of a struct. Perhaps you should try the
following:

[ StructLayout( LayoutKind.Sequential, Pack = 1 ) ]
public struct Frame_t
{
[ MarshalAs( UnmanagedType.ByValArray,
ArraySubType = UnmanagedType.U1,
SizeConst = 144 ) ]
public byte[] Block;
}

//...
public void MyDataHandler( IntPtr appContext, IntPtr data )
{
Frame_t frame = ( Frame_t ) Marshal.PtrToStructure (
data,
typeof( Frame_t )
);
// ...
}

That should work.

HTH,
Tom T.

"John" wrote:
public const int FRAME_LENGTH=144;
[StructLayout(LayoutKind.Sequential, Pack=1)]
public class FRAME_T
{
public byte []Block = new byte[FRAME_LENGTH]; }

public void MyDataHandler(IntPtr appContext, IntPtr data)
FRAME_T frame = new FRAME_T();
Marshal.PtrToStructure(data, frame);

This code throws an exception on the PtrToStructure saying there is a
null reference.

What am I doing wrong?

THANKS!!!

John

Nov 16 '05 #3

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

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
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...
7
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
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...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
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...
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: 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: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.