C # Use WIN32API to get a printer

Source: Internet
Author: User

The enumprinters function in winspool. DRV is mainly used. The Code is as follows:

[Dllimport ("winspool. DRV", setlasterror = true, charset = charset. Auto)]
[Return: financialas (unmanagedtype. bool)]
Private Static extern bool enumprinters ([financialas (unmanagedtype. U4)] printer_enum flags,
[Financialas (unmanagedtype. lpstr)] string sname,
Uint ilevel,
Intptr pprinterdesc,
Uint isize,
[Financialas (unmanagedtype. U4)] ref uint Ineeded,
[Financialas (unmanagedtype. U4)] ref uint ireturned
);

Note: The marshal attribute provides data delivery for hosted and unmanaged code.

The Win32 API of enumprinters is defined as follows:

Bool enumprinters (
DWORD flags, // printer Object Types
Lptstr name, // name of printer object
DWORD level, // information level
Lpbyte pprinterenum, // printer information Buffer
DWORD cbbuf, // size of printer information Buffer
Lpdword encoded needed, // bytes encoded ed or required
Lpdword pcreturned // Number of printers Enumerated
);

The problem is coming again. enumprinters gets printer_info through level, and can getPrinterThe driver is printer_info_2, andC #There is no printer_info_2 structure in, And I am dizzy again .....

After checking the data for half a day, the Internet is basically the definition of printer_info_1, while printer_info_2 is different from printer_info_1. It also includes the devmode structure and the unmanaged structure ~~~~

Finally, we found thatC #It is better to directly use classes to define the structure to correspond to the unmanaged structure. Therefore, two classes are defined.

Printer_info_2 and devmode (Note: Because printer_info_2 only uses the devmode structure to receivePrinterDriver information, so only this class is defined, and no specific implementation is made for other classes ).

In printer_info_2, all data of the DWORD type corresponds to the int32 type, and all data of the lptstr, lpdevmode, and psecurity_descriptor correspond to the intptr pointer type.

In order to obtain the data in the unmanaged database, the following function is used to obtain the data.PrinterInformation

.

Printer_info_2 Pi = new printer_info_2 ();
// Transfers data from the unmanaged memory to the managed memory

For (INT I = 0; I <numprinters; I ++)
{
Marshal. ptrtostructure (prinfo, Pi); // prinfo is obtained by the above enumprintersPrinter

String driver = marshal. ptrtostringauto (PI. pdrivername );

If (printerdriver = "" driver. tolower (). indexof (printerdriver )! =-1)
{

// Handle the problem

}
Prinfo = new intptr (prinfo. toint32 () + marshal. sizeof (typeof (printer_info_2); // get the nextPrinterStart of Information Section
}
.

The problem is now basically solved. HoweverC #It is difficult to call unmanaged functions and encapsulate data between them. If you have time, you need to sort them out.

Source: http://spaces.msn.com/sharkoo/Blog/cns! D8e832ce4545af! 158. Entry

Supplement: In 2.0, the fixed keyword can be used to define a fixed-size array cache, rather than defining a number size as in 1.x. However, this method can only be used for structure (struct), but not for class definition.

 

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.