- Joined
- Jun 3, 2007
- Messages
- 1
- Reaction score
- 0
Hi,
i'm trying to catch the printer notification using the winspool.drv, my problem is how can i extract the PRINTER_NOTIFY_INFO_DATA from the resulting PRINTER_NOTIFY_INFO, by the way the count of the PRINTER_NOTIFY_INFO is (70015864140) and the Flags is (30399774226120704):
//--------------------------
[DllImport("winspool.drv", EntryPoint = "OpenPrinterA",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter(String pPrinterName,
out IntPtr phPrinter,
Int32 pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter",
SetLastError = true,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter
(Int32 hPrinter);
[DllImport("winspool.drv",
EntryPoint = "FindFirstPrinterChangeNotification",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindFirstPrinterChangeNotification
([InAttribute()] IntPtr hPrinter,
[InAttribute()] Int32 fwFlags,
[InAttribute()] Int32 fwOptions,
[InAttribute(), MarshalAs(UnmanagedType.LPStruct)] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions)
;
[DllImport("winspool.drv", EntryPoint = "FindNextPrinterChangeNotification",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern bool FindNextPrinterChangeNotification
([InAttribute()] IntPtr hChangeObject,
[OutAttribute()] out Int32 pdwChange,
[InAttribute(), MarshalAs(UnmanagedType.LPStruct)] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions,
[OutAttribute()] out IntPtr lppPrinterNotifyInfo
)
;
//---------------------------------------------
int phand;
IntPtr whand ;
RegisteredWaitHandle rwh ;
ManualResetEvent wh;
const int PRINTER_NOTIFY_OPTIONS_REFRESH = 1;
//---------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
wh = new ManualResetEvent(false);
IntPtr phand ;
int pValue = 0;
OpenPrinter("HPLaserJet4050SeriesPCL6", out phand, pValue);
int pdwChange = 0;
PRINTER_NOTIFY_OPTIONS notifyOptions = new PRINTER_NOTIFY_OPTIONS();
notifyOptions.Count = 0;
notifyOptions.dwFlags = PRINTER_NOTIFY_OPTIONS_REFRESH;
notifyOptions.dwVersion = 2;
IntPtr ptr = IntPtr.Zero;
whand = FindFirstPrinterChangeNotification(phand, (int)Printer_Change_Notification_General_Flags.PRINTER_CHANGE_JOB, 0, notifyOptions);
wh.Handle = whand;
rwh = ThreadPool.RegisterWaitForSingleObject(wh,
new WaitOrTimerCallback(PrinterNotifyWaitCallback),
wh, -1, true);
}
//-----------------------------------------------
public void PrinterNotifyWaitCallback(
Object state ,
bool timedOut )
{
Console.WriteLine("IN");
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 pni = (PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(ptr, typeof(PRINTER_NOTIFY_INFO));
wh.Reset();
rwh = ThreadPool.RegisterWaitForSingleObject(wh, new WaitOrTimerCallback(PrinterNotifyWaitCallback), wh, -1, true);
}
//------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_NOTIFY_OPTIONS
{
public int dwVersion;
public int dwFlags;
public int Count;
public IntPtr lpTypes;
public PRINTER_NOTIFY_OPTIONS_TYPE pTypes;
}
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_NOTIFY_OPTIONS_TYPE
{
public Int16 wType;
public Int16 wReserved0 ;
public Int32 dwReserved1 ;
public Int32 dwReserved2 ;
public Int32 FieldCount;
public IntPtr pFields ;
}
//---------------------------------------------
public enum Printer_Change_Notification_General_Flags
{
PRINTER_CHANGE_JOB = 0x0000ff00
}
struct PRINTER_NOTIFY_INFO
{
public long Version;
public long Flags;
public long Count;
}
struct PRINTER_NOTIFY_INFO_DATA
{
public int Type;
public int Field;
public long Reserved;
public long Id;
}
i'm trying to catch the printer notification using the winspool.drv, my problem is how can i extract the PRINTER_NOTIFY_INFO_DATA from the resulting PRINTER_NOTIFY_INFO, by the way the count of the PRINTER_NOTIFY_INFO is (70015864140) and the Flags is (30399774226120704):
//--------------------------
[DllImport("winspool.drv", EntryPoint = "OpenPrinterA",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter(String pPrinterName,
out IntPtr phPrinter,
Int32 pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter",
SetLastError = true,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter
(Int32 hPrinter);
[DllImport("winspool.drv",
EntryPoint = "FindFirstPrinterChangeNotification",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindFirstPrinterChangeNotification
([InAttribute()] IntPtr hPrinter,
[InAttribute()] Int32 fwFlags,
[InAttribute()] Int32 fwOptions,
[InAttribute(), MarshalAs(UnmanagedType.LPStruct)] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions)
;
[DllImport("winspool.drv", EntryPoint = "FindNextPrinterChangeNotification",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern bool FindNextPrinterChangeNotification
([InAttribute()] IntPtr hChangeObject,
[OutAttribute()] out Int32 pdwChange,
[InAttribute(), MarshalAs(UnmanagedType.LPStruct)] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions,
[OutAttribute()] out IntPtr lppPrinterNotifyInfo
)
;
//---------------------------------------------
int phand;
IntPtr whand ;
RegisteredWaitHandle rwh ;
ManualResetEvent wh;
const int PRINTER_NOTIFY_OPTIONS_REFRESH = 1;
//---------------------------------------------
private void Form1_Load(object sender, EventArgs e)
{
wh = new ManualResetEvent(false);
IntPtr phand ;
int pValue = 0;
OpenPrinter("HPLaserJet4050SeriesPCL6", out phand, pValue);
int pdwChange = 0;
PRINTER_NOTIFY_OPTIONS notifyOptions = new PRINTER_NOTIFY_OPTIONS();
notifyOptions.Count = 0;
notifyOptions.dwFlags = PRINTER_NOTIFY_OPTIONS_REFRESH;
notifyOptions.dwVersion = 2;
IntPtr ptr = IntPtr.Zero;
whand = FindFirstPrinterChangeNotification(phand, (int)Printer_Change_Notification_General_Flags.PRINTER_CHANGE_JOB, 0, notifyOptions);
wh.Handle = whand;
rwh = ThreadPool.RegisterWaitForSingleObject(wh,
new WaitOrTimerCallback(PrinterNotifyWaitCallback),
wh, -1, true);
}
//-----------------------------------------------
public void PrinterNotifyWaitCallback(
Object state ,
bool timedOut )
{
Console.WriteLine("IN");
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 pni = (PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(ptr, typeof(PRINTER_NOTIFY_INFO));
wh.Reset();
rwh = ThreadPool.RegisterWaitForSingleObject(wh, new WaitOrTimerCallback(PrinterNotifyWaitCallback), wh, -1, true);
}
//------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_NOTIFY_OPTIONS
{
public int dwVersion;
public int dwFlags;
public int Count;
public IntPtr lpTypes;
public PRINTER_NOTIFY_OPTIONS_TYPE pTypes;
}
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_NOTIFY_OPTIONS_TYPE
{
public Int16 wType;
public Int16 wReserved0 ;
public Int32 dwReserved1 ;
public Int32 dwReserved2 ;
public Int32 FieldCount;
public IntPtr pFields ;
}
//---------------------------------------------
public enum Printer_Change_Notification_General_Flags
{
PRINTER_CHANGE_JOB = 0x0000ff00
}
struct PRINTER_NOTIFY_INFO
{
public long Version;
public long Flags;
public long Count;
}
struct PRINTER_NOTIFY_INFO_DATA
{
public int Type;
public int Field;
public long Reserved;
public long Id;
}