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