[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


C++ Language question

Posted on 1997-09-23
Medium Priority
Last Modified: 2012-06-21
Could someone give me an example using the following API:


I'm using MS Visual C++ on Windows NT 4.0
I am trying to log the print jobs that are sent to the printer on an NT 4.0 PrintServer, together with the username and number of pages. I'm able to get a printer handle, then a handle to the notification object. I then use the WaitForObjectEx() function to wait for the change notification (PRINTER_CHANGE_ADD_JOB. The wait functions then returns successfully. When I use the FindNextPrinterChangeNotification() It returns a value of 1 yet the information structure contains no information.I'm declaring a PRINTER_NOTIFY_INFO and I then use a pointer to this structure to pass it to the FindNextPrinterChangeNotification().

Question by:santamaria
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Author Comment

ID: 1170334
Edited text of question
LVL 15

Expert Comment

by:Tommy Hui
ID: 1170335
I'm writing an example right now. Will post when done.
LVL 15

Accepted Solution

Tommy Hui earned 1000 total points
ID: 1170336
This should get you going:

#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
      DWORD needed = 0;
      DWORD returned = 0;
      int i = 0;

      if (!EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &needed, &returned))
            BYTE* buffer = new BYTE[needed];

            if (!EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 4, (LPBYTE)buffer, needed, &needed, &returned))
                  return 0;

            HANDLE* printerHandles = new HANDLE[returned];
            HANDLE* waitHandles = new HANDLE[returned];
            PRINTER_INFO_4* pInfo = (PRINTER_INFO_4*)buffer;

            for (i = 0; i < returned; i++, pInfo++)
                  printf("Name %s on %s: 0x%08X\r\n",
                        pInfo->pPrinterName, pInfo->pServerName, pInfo->Attributes);

                  if (OpenPrinter(pInfo->pPrinterName, &printerHandles[i], NULL) == 0)
                        printf("Failed to open printer %s\r\n", pInfo->pPrinterName);
                        printerHandles[i] = NULL;
                        waitHandles[i] = NULL;
                        waitHandles[i] = FindFirstPrinterChangeNotification(
                              printerHandles[i], PRINTER_CHANGE_ALL, 0, NULL);

                        if (waitHandles[i] == INVALID_HANDLE_VALUE)
                              printf("Failed to create printer notification at index %d\r\n", i);

                  DWORD ret = WaitForMultipleObjects(returned, waitHandles, FALSE, INFINITE);

                  if (WAIT_OBJECT_0 <= ret && ret < (WAIT_OBJECT_0 + returned))
                        int index = ret - WAIT_OBJECT_0;
                        printf("printer %d signaled\n", index);
                        DWORD changed = 0;
                        PRINTER_NOTIFY_INFO* ptr = NULL;
                        PRINTER_NOTIFY_OPTIONS options;
                        PRINTER_NOTIFY_OPTIONS_TYPE      types;
                        WORD fields[] =

                        options.Version      = 2;
                        options.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
                        options.Count = 1;
                        options.pTypes = &types;

                        types.Type = PRINTER_NOTIFY_TYPE;
                        types.Count = sizeof fields/sizeof fields[0];
                        types.pFields = fields;

                        if (FindNextPrinterChangeNotification(waitHandles[index], &changed, NULL, (void**)&ptr))
                              printf("changed = %d\r\n", changed);

                              if (ptr != NULL)
                                    for (int j = 0; j < ptr->Count; j++)
                                          PRINTER_NOTIFY_INFO_DATA* pniData = &(ptr->aData)[j];

                                          printf("Type %d, field %d\n", pniData->Type, pniData->Field);
                              DWORD lastError = GetLastError();
                              printf("FNPCN LastError = %d\r\n", lastError);
            while (TRUE);

            // If it theoretically gets here
            for (i = 0; i < returned; i++)
                  if (printerHandles[i] != NULL)
                  printerHandles[i] = NULL;

                  if (waitHandles[i] != NULL)
                  waitHandles[i] = NULL;

            delete[] printerHandles;
            delete[] waitHandles;
            DWORD lastError = GetLastError();
            printf("LastError = %d\r\n", lastError);

      return 0;


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question