Solved

C++ Language question

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

821 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