Advertisement
Guest User

Untitled

a guest
May 6th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "windows.h"
  4.  
  5. #define PRINTER_NAME "Samsung CLX-216x Series"
  6. #define USB_PRINTER_ADDRESS "268435467"
  7.  
  8. int main(int argc, char* argv[]) {
  9.     HANDLE hPrinter = INVALID_HANDLE_VALUE;
  10.     HANDLE hChangeNotificationObject = INVALID_HANDLE_VALUE;
  11.  
  12.     printf("INFO: Starting...\n");
  13.  
  14.     // initialize
  15.    
  16.     if (OpenPrinter(PRINTER_NAME, &hPrinter, NULL) == 0) {
  17.         printf("ERR: Could not open printer\n");
  18.         exit(1);
  19.     } else {
  20.         printf("INFO: Printer handle is 0x%08X\n", hPrinter);
  21.     }
  22.  
  23.     hChangeNotificationObject = FindFirstPrinterChangeNotification(hPrinter, PRINTER_CHANGE_JOB, 0, NULL);
  24.     if (hChangeNotificationObject == INVALID_HANDLE_VALUE) {
  25.         printf("ERR: Could not get a notification object\n");
  26.     } else {
  27.         printf("INFO: notification object handle is 0x%08X\n", hChangeNotificationObject);
  28.     }
  29.  
  30.     printf("Waiting for change notification... Try to print something like the test page...\n");
  31.     WaitForSingleObject(hChangeNotificationObject, INFINITE);
  32.  
  33.     // we've got an event... connect the USB port via API
  34.     system("vhui64.exe -t \"USE," USB_PRINTER_ADDRESS "\"");
  35.  
  36.     // cleanup
  37.    
  38.     if (hChangeNotificationObject != INVALID_HANDLE_VALUE) {
  39.         if (FindClosePrinterChangeNotification(hChangeNotificationObject) == 0) {
  40.             printf("ERR: Could not close notification object\n");
  41.         } else {
  42.             printf("INFO: Notification object closed\n");
  43.         }
  44.     }
  45.    
  46.     if (hPrinter != INVALID_HANDLE_VALUE) {
  47.         if (ClosePrinter(hPrinter) == 0) {
  48.             printf("ERR: Could not close printer\n");
  49.         } else {
  50.             printf("INFO: Printer handle closed\n");
  51.         }
  52.     }
  53.  
  54.     printf("INFO: done.\n");
  55.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement