Solved

C++ Language question

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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 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.
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.

760 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

24 Experts available now in Live!

Get 1:1 Help Now