Take the 2-minute tour ×

The MSDN document that I am trying to follow is located here. Basically I am trying to figure out in C# how to read that pointer into a list of the DHCP_OPTION_DATA structures.

I have the following code but I don't think that it is the proper way to do this.

DHCP_OPTION_ARRAY optionArray = (DHCP_OPTION_ARRAY)Marshal.PtrToStructure(options, typeof(DHCP_OPTION_ARRAY));
List<DHCP_OPTION> allOptions = new List<DHCP_OPTION>();
for (int i = 0; i < optionArray.NumElements; i++) {
    DHCP_OPTION option = (DHCP_OPTION)Marshal.PtrToStructure(optionArray.Options, typeof(DHCP_OPTION));
    optionArray.Options = (IntPtr)((int)optionArray.Options + (int)Marshal.SizeOf(option));

Since I can't Marshal the pointer into a generic list collection I tried this way. My problem is that I am getting skewed results based on how much I increase the IntPtr to. Initially I was doing this.

optionArray.Options = (IntPtr)((int)optionArray.Options + (int)Marshal.SizeOf(typeof(DHCP_OPTION_DATA)));

However, I then realized that the next element would be located after the size of the actual option.

So the question still remains, how do I Marshal a Ptr to a list of structures?


I posted the wrong article it is fixed now.


Although both answers were great, I chose the answer to my problem because it addressed my lack of understanding of how the data was being handled on the back end of marshaling the information.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Is the first option object you get correct?
If so, the reason for the rest being skewed most likely is the alignment of the structure.

You could try to find the correct alignment, for example:

var offset = (int)Marshal.SizeOf(typeof(DHCP_OPTION_DATA));
var alignment = 4; 
var remainder = offset % alignment;
if(remainder != 0)
    offset += alignment - remainder;

optionArray.Options = (IntPtr)((int)optionArray.Options + offset);
share|improve this answer
Ok I will definitely check this out tonight when I get home. –  meanbunny Dec 12 '12 at 16:33
@meanbunny: Did you get it to work? –  Daniel Hilgarth Dec 18 '12 at 9:01
Yes I did actually and your answer helped me get there and gave me my understanding that I was looking for. I didn't know it was so simplistic I thought it would be a lot more complicated than that –  meanbunny Dec 18 '12 at 19:34
@meanbunny: That's great to hear. It would be nice if you could post your final solution for others. –  Daniel Hilgarth Dec 20 '12 at 11:25

Here is a paper Jason Rupard wrote using the DHCP_OPTION_ARRAY...


Looks like he has everything you need and more... :)

Although looking at it you could define the structure a little differently and have it automatically turned into an array upon deserialization if you get the Pack attribute right.

share|improve this answer
Just wondering if there was anything more you needed for me to get the bounty? –  Jay Dec 14 '12 at 17:52
You should extract the relevant parts of that document and reproduce them here. Your answer will otherwise be useless when the link goes down. Even if the link stays up, the document you linked is over a hundred pages long, making it hard to extract the information you meant. –  Daniel Hilgarth Dec 18 '12 at 9:01

Your Answer


