Solved

C++ Language question

Posted on 1997-09-23
3
1,467 Views
Last Modified: 2012-06-21
Could someone give me an example using the following API:

OpenPrinter();
FindFirstPrinterChangeNotification();
WaitForSingleObjectEx()
FindNextPrinterChangeNotification();

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().

Thanks.
0
Comment
Question by:santamaria
  • 2
3 Comments
 

Author Comment

by:santamaria
ID: 1170334
Edited text of question
0
 
LVL 15

Expert Comment

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

Accepted Solution

by:
Tommy Hui earned 250 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))
            {
                  printf("Failed\r\n");
                  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;
                  }
                  else
                  {
                        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);
                  }
            }


            do
            {
                  printf("Waiting...\n");
                  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[] =
                        {
                              PRINTER_NOTIFY_FIELD_PRINTER_NAME,
                        };

                        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);
                                    }
                              }
                              FreePrinterNotifyInfo(ptr);
                        }
                        else
                        {
                              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)
                        ClosePrinter(printerHandles[i]);
                  printerHandles[i] = NULL;

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

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

      return 0;
}


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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