Problem about FindNextPrinterChangeNotification

  • Thread starter Thread starter sunny
  • Start date Start date
S

sunny

Dear ALL:
I'm writing a program about printer by C# now.I have read Duncan's
article about monitor printer by VB before and followed his method. I use
FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification API
to monitor printer.
Now I'm puzzled by the FindNextPrinterChangeNotification function
..Whenever I call this method to get a pointer of PRINTER_NOTIFY_INFO , it
always return null.
My code is:

int phand;
int whand;
RegisteredWaitHandle rwh;
ManualResetEvent wh;

OpenPrinter("MicrosoftOffice",ref phand, 0) ; //MicrosoftOffice is my
printer name
whand =
FindFirstPrinterChangeNotification(phand,(int)PrinterChangeNotificationGener
alFlags.PRINTER_CHANGE_JOB,0,0);
wh = new ManualResetEvent(false);
wh.Handle = new IntPtr(whand);
rwh = ThreadPool.RegisterWaitForSingleObject(wh,new
WaitOrTimerCallback(PrinterNotifyWaitCallBack),wh,-1,true);

public void PrinterNotifyWaitCallBack(object state,bool timeout)
{
int pdwChange=0;
IntPtr ptr= IntPtr.Zero;

bool next = FindNextPrinterChangeNotification(whand,ref pdwChange,0,ref
ptr); //The next is true,but the ptr always returns 0
PRINTER_NOTIFY_INFO pni =
(PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(ptr,typeof(PRINTER_NOTIFY_INFO))
; //So the pni is always null.

FreePrinterNotifyInfo(ptr);
MessageBox.Show("Notify");
wh.Reset();
rwh = ThreadPool.RegisterWaitForSingleObject(wh,new
WaitOrTimerCallback(PrinterNotifyWaitCallBack),wh,-1,true);
}

I know here are many experts at this field.Could anyone tell me why and
how to solve this problem? I'm crashed by it.

Thank you very much!
 
Back
Top