Monitor printer queue status using C ++ Builder programming

Source: Internet
Author: User
Tags printer name list
Currently, the application of API is spread across various journals and magazines. However, most instances and applications are based on Visual Basic, and most people report that there are too few applications for C ++ builder, there are few print-related applications, even though Visual Basic has very few applications. This is true. This article introduces how to use C ++ builder and Windows API to monitor the printer queue status.

I. API introduction:

API is the abbreviation of application programming interface, that is, the interface for an application to call system functions. Windows APIs mainly include system-defined functions, messages, and related data types. We use various development tools to ultimately deal with APIs.

Ii. APIs related to the printing status:

API function name function description w3.x w9x NT
Enumjobs get printed job information no Yes
Openprinter: Get the handle of the specified printer. No, yes

In Windows NT and Windows 9x, a number of brand-new APIs are added to process printer and printer queue statuses. Here, the openprinter and enumjobs functions are used to determine the information required to print the printer job status.

Openprinter is used to obtain the handle of a given printer. With this handle, you can perform operations on the corresponding printer.

The enumjobs function is used to list the job information being printed on the specified printer. Here, the reference of the given printer is the printer handle fed back by using the above openprinter function.

Introduction to the enumjobs function:

Enumjobs (byval hprinter as long, byval firstjob as long, byval nojobs as long, byval level as long, pjob as long, byval cbbuf as long, pcbneeded as long, pcreturned as long)

The API function enumjobs is used to list all the task status information that is currently being printed on a given printer. This function can be used to list brief or detailed (level-determined) queue task status information of a given printer. When referencing this function, we usually set the cbbuf variable to 0 to get the number of bytes of the printer queue task. The number of bytes is stored in pcbneeded to determine the size of the pjob variable, then reference this function again to obtain the details of the printer queue task.

Parameters:

Hprinter printer handle.

The starting point for firstjob to count the number of printer queue tasks. 0 indicates counting and listing all print tasks from the first task.

The maximum number of print tasks to be listed by nojobs. It is generally set to 127.

Level indicates the Information Format stored in pjob. If the value is 1, data is stored in the format of job_info_1; if the value is 2, data is stored in the format of job_info_2.

Pjob array variable to save all information about the print task. When cbbuf is set to 0, this value is also 0. The value of the Level variable determines the pjob data format (job_info_1 or job_info_2)

The value of cbbuf is usually 0.

Pcbneeded stores the number of bytes of the print task in the printer.

The pcreturned variable is used to store the number of print tasks.

The above table shows that these two API functions are only used in Windows 9x and Windows NT environments.

Iii. development tools:

C ++ builder, as a tool for rapid development of applications on Windows platforms, has been adopted by more and more developers. However, if you want to develop professional Windows applications, you also need to use a large number of Windows API functions, the following describes how to monitor the printer status from time to time using C ++ builder and Windows API functions.

Reasons for choosing C ++ builder:

I. Since windows APIs are all written in C or C ++, C ++ builder is easier to use at the underlying layer.

II. C ++ builder's reference to Windows APIS is simpler, which is lacking in Visual Basic.

4. program instance:

1. Start the C ++ builder program and create a new project;

2. Go to the project and add a checklistbox control and a timer control to the current form;

3. Add the printer name list in the item attribute of the checklistbox control (assuming there are three printers printer1, printer2, and printer3 on the hostprinter host in the Network), the content format of the item is: //// hostprinter // printer1, //// hostprinter //// printer2, //// hostprinter /// printer3;

4. Set the interval attribute of timer to 60000 (one minute). Add the following code to the timer ontimer event:
{
Handle hprinter; // printer handle
Job_info_2 jobs [30]; // retain the print job details
DWORD size = sizeof (jobs );
Job_info_2 * jobs1 = NULL; // a dynamic array is used to operate tasks larger than size.
DWORD bytes needed = 0; // Number of all printed bytes
Int actneed = 0;
DWORD pcreturned = 0; // number of print tasks
Char Buf [19]; // point to the printer or machine name
Int ret1; // get the printer handle Return Value
Int ret; // get the printer task Return Value
Tdatetime starttime;
Word hour = 0;
Word hour1 = 0;
Word min = 0;
Word min1 = 0;
Word sec = 0;
Word MSEC = 0;
Int starttime = 0; // task Start Time
Int nowtime = 0; // current time
Int totaltime = 0; // task stay time
String STR;
Int length;
Int printcount;
Int initcount = 15; // set the number of printed queues -------- alarm bottom line
Int inittime = 10; // set the print queue event -------- alarm bottom line (minutes)
Printcount = checklistbox1-> count;
For (INT I = 0; I <printcount; I ++)
{
// ...... Get the printer in the list
STR = checklistbox1-> items-> strings;
Length = Str. Length ();
// ...... String to character conversion,
Strplcopy (BUF, STR, length );
// ...... Get the printer handle
Ret1 = openprinter (BUF, & hprinter, null );
// ............. Obtain the number of print tasks
Ret = enumjobs (hprinter, 0,127, 0, & export needed, & pcreturned );
// Take the necessary step, first retrieve the number of bytes of the task in the printer, and then determine the size of the jobs1 variable.
// ---------------------------- ①
// ...... Re-define jobs1 and decide which variable to use according to javasneeded.
If (distinct needed> size)
{
Actneed = effecneeded;
Jobs1 = new job_info_2 [actneed];
Ret = enumjobs (hprinter, 0,127, 2, (lpbyte) jobs1, actneed, & pcbneeded, & pcreturned );
Starttime = systemtimetodatetime (jobs1 [0]. Submitted); // ---------------- ②
Delete jobs1;
}
Else
{
Ret = enumjobs (hprinter, 0,127, 2, (lpbyte) jobs, size, & pcbneeded, & pcreturned); // --------------------------- ③
If (pcreturned> 0)
Starttime = systemtimetodatetime (jobs [0]. Submitted );
}
// ........................... Extend needed> size ends
// ...... If a print task exists in the queue, compare the quantity and time.
If (pcreturned> 0)
{
// .............. Time conversion, calculate the time difference within one day
{
// ............ Time comparison calculation;
// ............ Time conversion starttime to minutes
}
Decodetime (now (), hour1, min1, SEC, msec );
Nowtime = hour1 * 60 + min1;
Totaltime = nowtime-starttime;
}
// Determines whether an alarm is triggered based on the conditions.
If (pcreturned> initcount) | (totaltime> inittime ))
{
Beep ();
}
Pcreturned = 0;
Required needed = 0;
}
// .................. Pcreturned> 0
}//..................................... ............... For ends

5. Compile and run the above program:

① In the program, the main purpose of obtaining the printer task here is to obtain the number of bytes of the printer job ------ bytes needed, the size of the bytes in this variable, the variables used to further obtain the job status in the printer are jobs or jobs1.

② When the size is larger than the size, allocate space to jobs1 and obtain the current job status of the printer.

③ When pcbneeded <size, use jobs as the variable to obtain the current printer job status.

Note: ② and ③ parameter references are different from ①.

5. Application Analysis:

It can be said that it technology has been widely used in the manufacturing industry, while printing control and monitoring is a typical application of many applications in the manufacturing industry. Generally, many printers are used to print production sorting orders, assembly orders, nameplate and labels during the manufacturing process. Therefore, the printer's time-to-time status (lack of paper, ink, paper jam) is particularly important here. If you cannot monitor the printer's status in time, it will inevitably affect the normal operation of production. Therefore, in order to ensure normal production, it is necessary to have a set of programs for monitoring the printer status from time to time.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.