Solved

C++ Language question

Posted on 1997-09-23
3
1,450 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 clear a vector as well as how to detect empty vectors in C++.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now