add SetPrinterDataExW.exe.
[gd/win32-spoolss/.git] / SetPrinterDataExW.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 int __cdecl wmain(int argc, WCHAR *argv[])
6 {
7         HANDLE printer;
8         DWORD type, cbData;
9         DWORD type_ex, cbData_ex;
10         LPBYTE pData = NULL;
11         LPBYTE pData_ex = NULL;
12         DWORD i, status;
13         LPCWSTR pKeyName;
14         LPCWSTR pValueName;
15         PRINTER_DEFAULTSW defaults;
16         LPCSTR str = "abcdefghijklmnopqrstuvwxzy";
17         DWORD s;
18
19         if (argc < 3) {
20                 fprintf(stderr, "usage: %s <printername> <keyname> <valuename>\n", argv[0]);
21                 exit(-1);
22         }
23
24         defaults.pDatatype = NULL;
25         defaults.pDevMode = NULL;
26         defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
27
28         if (!OpenPrinterW(argv[1], &printer, &defaults)) {
29                 exit(-1);
30         }
31
32         printf ("Printer [%s] opened successfully.\n\n", argv[1]);
33
34         pKeyName = argv[2];
35         pValueName = argv[3];
36
37         type = REG_SZ;
38
39         for (s=0; s < strlen(str); s++) {
40 #if 0
41                 DWORD k = 0;
42
43                 cbData = s * 2 + 2;
44                 pData = malloc(cbData);
45                 memset(pData, '\0', cbData);
46
47                 for (i=0; i < cbData; i += 2) {
48                         memcpy(&pData[i], &str[k], 1);
49                         k++;
50                 }
51 #else
52                 cbData = s;
53                 pData = malloc(cbData);
54                 memcpy(pData, str, s);
55 #endif
56                 printf("Testing SetPrinterDataExW with REG_SZ len: %d\n", cbData);
57 #if 0
58                 for (i=0; i < cbData; i++) {
59                         printf("0x%02x ", pData[i]);
60                 }
61                 printf("\n");
62 #endif
63                 status = SetPrinterDataExW(printer, pKeyName, pValueName, type, pData, cbData);
64                 if (status) {
65                         printf("SetPrinterDataExW failed with 0x%08x\n", status);
66                         ClosePrinter(printer);
67                         exit(-1);
68                 }
69
70                 status = GetPrinterDataExW(printer, pKeyName, pValueName, &type_ex, NULL, 0, &cbData_ex);
71                 if (status == ERROR_MORE_DATA) {
72                         pData_ex = malloc(cbData_ex);
73                         status = GetPrinterDataExW(printer, pKeyName, pValueName, &type_ex, pData_ex, cbData_ex, &cbData_ex);
74                 }
75                 if (status) {
76                         printf("GetPrinterDataExW failed with 0x%08x\n", status);
77                         ClosePrinter(printer);
78                         exit(-1);
79                 }
80
81                 printf("did set %d of data\n", cbData);
82                 for (i=0; i < cbData; i++) {
83                         printf("0x%02x ", pData[i]);
84                 }
85                 printf("\n");
86
87                 printf("received %d of data\n", cbData_ex);
88                 for (i=0; i < cbData_ex; i++) {
89                         printf("0x%02x ", pData_ex[i]);
90                 }
91                 printf("\n");
92
93                 DeletePrinterDataExW(printer, pKeyName, pValueName);
94                 DeletePrinterKeyW(printer, pKeyName);
95
96                 free(pData);
97                 pData = NULL;
98                 free(pData_ex);
99                 pData_ex = NULL;
100         }
101
102         if (!ClosePrinter(printer)) {
103                 exit(-1);
104         }
105
106         return 0;
107 }