473,320 Members | 1,872 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,320 software developers and data experts.

Problem using pointer to a struct holding another struct containing pointer to an array

I have this problem:
'pno' is a pointer to a struct of type PRINTER_NOTIFY_OPTIONS.
This struct is holding another struct called 'not' (of type
PRINTER_NOTIFY_OPTIONS_TYPE) which contains a couple of 'normal' fields
and a pointer (called pFields) to an array of integers.

Now, when I make up everything (probably incorrectly), it compiles ok,
but when I execute this code, I get an error saying that pno is a
nullreference.

I'm pretty sure this has something to do with the pointer to the array
(pFields) because when I leave out "pno->pTypes=not[0];", then there's
no problem with NullReferenceExecptions or anything.
I don't understand what the problem is (apart from me being not too
great at C#) and I've been trying lots of different approaches...

The nested structure/array looks like this:
struct PRINTER_NOTIFY_OPTIONS pno*
{
struct PRINTER_NOTIFY_OPTIONS_TYPE pTypes (or not[])
{
int...
int* pFields: array of integers
pFields[0]
pFields[1]
pFields[2]
...
pFields[8]
}
}

Here are my definitions and code with some comment. I'd be grateful if
someone would have a look at it and point me to the right direction...
[StructLayout(LayoutKind.Sequential)]
public unsafe struct PRINTER_NOTIFY_OPTIONS_TYPE
{
public ushort Type;
public ushort Reserved0;
public uint Reserved1;
public uint Reserved2;
public uint Count;
public int* pFields;
}

[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_NOTIFY_OPTIONS
{
public uint Version;
public uint Flags;
public uint Count;
public PRINTER_NOTIFY_OPTIONS_TYPE pTypes;
}
PRINTER_NOTIFY_OPTIONS no;
PRINTER_NOTIFY_OPTIONS_TYPE[] not = new PRINTER_NOTIFY_OPTIONS_TYPE[1];

//Fill up pFields array in not struct
int []Fieldsarray = new Int32[9];
fixed(int* pFields = &Fieldsarray[0])
{
pFields[0]=0;
pFields[1]=1;
pFields[2]=3;
pFields[3]=11;
pFields[4]=13;
pFields[5]=17;
pFields[6]=18;
pFields[7]=20;
pFields[8]=21;
not[0].pFields=pFields;
}

//complete not struct
not[0].Type = 1;
not[0].Count = 9;

//fill in struct where not points to
PRINTER_NOTIFY_OPTIONS Struct = new PRINTER_NOTIFY_OPTIONS();
PRINTER_NOTIFY_OPTIONS *pno=&Struct;
pno->Count=1;
pno->Flags=0;
pno->pTypes=not[0];
pno->Version=2;

//check something
Console.WriteLine(pno->pTypes.pFields[2]); //This shows "3", and that's
correct

//pass the pno structpointer to a function
hNotify=FindFirstPrinterChangeNotification(hPrinte r, 0x0000FF00,0,pno);

//this returns the NullReferenceException

Peter

Nov 17 '05 #1
0 1395

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

Similar topics

2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
6
by: Rahul K | last post by:
Hi I am working on Visual Studio on Windows. I have a function which return the list of all the IP Addresses of a machine: vector<char *getAllLocalIPAddress() { char localHostName; struct...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.