add SetPrinterDataExW.exe.
[gd/win32-spoolss/.git] / PrinterChangeNotification.c
1 /******************************************************************
2  * SPOOLSS regression testing code for Samba print servers
3  *
4  *****************************************************************/
5
6 #include <windows.h>
7 #include <stdio.h>
8 #include "printlib.h"
9
10 void PrintNotifyInfoData(PRINTER_NOTIFY_INFO_DATA *p)
11 {
12         printf("\tType\t=0x%x\n", p->Type);
13         printf("\tField\t=0x%x\n", p->Field);
14         printf("\tId\t=0x%x\n", p->Id);
15         printf("\tdata\t= %s\n", p->NotifyData.Data.pBuf);
16
17         return;
18
19 }
20
21
22 int main (int argc, char* argv[])
23 {
24
25         HANDLE  server, change;
26         PRINTER_NOTIFY_INFO     *change_info = NULL;
27         PRINTER_NOTIFY_OPTIONS watch, *pWatch = NULL;
28         PRINTER_NOTIFY_OPTIONS_TYPE data;
29         DWORD option = PRINTER_CHANGE_PRINTER_DRIVER;
30         BOOL use_notify_options = TRUE;
31         DWORD   condition;
32 /*      DWORD flags = PRINTER_NOTIFY_OPTIONS_REFRESH; */
33
34
35
36         if ((argc < 3) || (strcmp(argv[1], "-l") && strcmp(argv[1], "-t")))
37         {
38                 fprintf (stderr, "usage: -l|-t %s <servername>\n", argv[0]);
39                 exit (-1);
40         }
41
42         if (strcmp(argv[1], "-l") == 0)
43                 use_notify_options = FALSE;
44
45
46         if (!OpenPrinter (argv[2], &server, NULL))
47         {
48                 PrintLastError();
49                 exit (-1);
50         }
51         else
52         {
53                 printf ("Printer [%s] opened successfully.\n\n", argv[2]);
54         }
55
56         printf ("flags = PRINTER_CHANGE_PRINTER\n");
57         watch.Version = 2;
58         watch.Count = 1;
59         watch.pTypes = &data;
60         data.Type = PRINTER_NOTIFY_TYPE;
61         data.Count = 1;
62         data.pFields = (PWORD)&option;
63
64         if (use_notify_options) {
65                 pWatch = &watch;
66                 option = PRINTER_NOTIFY_FIELD_DRIVER_NAME;
67         }
68
69         if ((change=FindFirstPrinterChangeNotification(server, option, 0, pWatch)) == INVALID_HANDLE_VALUE)
70         {
71                 printf ("FindFirstPrinterChangeNotification failed : ");
72                 PrintLastError();
73                 ClosePrinter(server);
74                 exit (-1);
75         }
76         else
77                 printf ("FindFirstPrinterChangeNotification succeeded with flags [PRINTER_CHANGE_PRINTER_DRIVER]\n\n");
78
79         if (pWatch) {
80                 printf ("Calling FindNextPrinterCangeNotification() with PRINTER_NOTIFY_OPTIONS_REFRESH\n");
81                 memset(&watch, 0x0, sizeof(watch));
82                 watch.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
83                 watch.Version = 2;
84                 if (FindNextPrinterChangeNotification(change, &condition, pWatch, &change_info))
85                         printf ("Received change notification of [0x%x]\n", condition);
86                 else
87                         PrintLastError();
88
89                 if (change_info) {
90                         int i;
91
92                         printf("\tVersion\t=0x%x\n", change_info->Version);
93                         printf("\tFlags\t=0x%x\n", change_info->Flags);
94                         printf("\tCount\t=%d\n", change_info->Count);
95
96                         for (i=0; i<(int)change_info->Count; i++) {
97                                 PrintNotifyInfoData(&change_info->aData[i]);
98                                 printf("\n");
99                         }
100                 }
101
102                 FreePrinterNotifyInfo(change_info);
103                 printf ("\n");
104         }
105
106         do
107         {
108
109                 printf ("Waiting for Printer Change Notification...\n");
110
111                 if (WaitForSingleObject(change, INFINITE) != WAIT_OBJECT_0)
112                         continue;
113
114                 if (FindNextPrinterChangeNotification(change, &condition, NULL, &change_info))
115                         printf ("Received change notification of [0x%x]\n", condition);
116                 else
117                         PrintLastError();
118
119                 if (change_info) {
120                         int i;
121
122                         printf("\tVersion\t=0x%x\n", change_info->Version);
123                         printf("\tFlags\t=0x%x\n", change_info->Flags);
124                         printf("\tCount\t=%d\n", change_info->Count);
125
126                         for (i=0; i<(int)change_info->Count; i++) {
127                                 PrintNotifyInfoData(&change_info->aData[i]);
128                                 printf("\n");
129                         }
130                 }
131
132                 FreePrinterNotifyInfo(change_info);
133                 printf ("\n");
134         } while (1);
135
136
137         if (FindClosePrinterChangeNotification(change))
138                 printf ("FindClosePrinterChangeNotification call succeeded.\n");
139         else
140         {
141                 printf ("FindClosePrinterChangeNotification failed : ");
142                 PrintLastError();
143         }
144         printf ("\n");
145
146         if (!ClosePrinter(server))
147                 PrintLastError();
148         else
149                 printf ("Printer [%s] closed successfully.\n", argv[2]);
150
151         return 0;
152
153 }