Groups | Blog | Home
all groups > dotnet interop > february 2004 >

dotnet interop : Problems with FindNextPrinterChangeNotification in C#


Michal Michalec
2/26/2004 9:30:51 PM
I'm writing a .NET application for monitoring print queues. I can get a
handle to a printer by OpenPrinter. I can easily run
FindFirstPrinterChangeNotification (but only with IntPtr.Zero as
pPrinterNotifyOptions argument) and wait for the printer event. Finally I
can use FindNextPrinterChangeNotification function and get pdwChange
attribute that indicates what kind of event has occured. The tricky thing
is to get to the nested structures, that describes the print job that
ocurred. The question is: how can I pass ppPrinterNotifyInfo argument to
find these nested structures with print job details?

Michal


Mattias Sjögren
2/27/2004 10:56:37 PM
Michal,

[quoted text, click to view]

Declare the structs something like this

struct PRINTER_NOTIFY_INFO
{
public uint Version;
public uint Flags;
public uint Count;
// Array of PRINTER_NOTIFY_INFO_DATA follows
}

struct PRINTER_NOTIFY_INFO_DATA
{
public ushort Type;
public ushort Field;
public uint Reserved;
public uint Id;
public uint cbBuf; // or adwData[0]
public IntPtr pBuf; // or uint adwData[1]
}

Declare FindNextPrinterChangeNotification like this

static extern bool FindNextPrinterChangeNotification(..., out IntPtr
ppPrinterNotifyInfo);

and finally call it like this

IntPtr pNotifyInfo;
if ( FindNextPrinterChangeNotification( ..., out pNotifyInfo ) ) {
PRINTER_NOTIFY_INFO info =
(PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(
pNotifyInfo, typeof(PRINTER_NOTIFY_INFO) );
int pData = (int)pNotifyInfo +
Marshal.SizeOf( typeof(PRINTER_NOTIFY_INFO) );
PRINTER_NOTIFY_INFO_DATA[] data =
new PRINTER_NOTIFY_INFO_DATA[info.Count];
for ( uint i = 0; i < info.Count; i++ ) {
data[i] = (PRINTER_NOTIFY_INFO_DATA)Marshal.PtrToStructure(
(IntPtr)pData, typeof(PRINTER_NOTIFY_INFO_DATA) );
pData += Marshal.SizeOf( typeof(PRINTER_NOTIFY_INFO_DATA) );
}
FreePrinterNotifyInfo( pNotifyInfo );
}



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Michal Michalec
3/1/2004 9:56:02 AM
Thank You very much Mattias.
I'v been counting on your answer ;) It looks like you know everything about
printer notifications and interoperability
Thank you once more. Your answer solves my problem.
Michal
[quoted text, click to view]

ehabhassan
6/3/2007 3:03:30 AM
i have the same problem, and i tried to solve it that way, but the count of the info is always zero
int pdwChange = 0;
IntPtr ptr = IntPtr.Zero;
PRINTER_NOTIFY_OPTIONS notifyOptions = new PRINTER_NOTIFY_OPTIONS();
notifyOptions.Count = 1;
notifyOptions.dwFlags = PRINTER_NOTIFY_OPTIONS_REFRESH;
notifyOptions.dwVersion = 2;
bool nxt = FindNextPrinterChangeNotification(whand, out pdwChange, notifyOptions, out ptr);
PRINTER_NOTIFY_INFO info = (PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(ptr, typeof(PRINTER_NOTIFY_INFO));


From http://developmentnow.com/g/21_2004_2_0_0_105339/Problems-with-FindNextPrinterChangeNotification-in-C.htm

Posted via DevelopmentNow.com Groups
AddThis Social Bookmark Button