473,549 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting list of printer paper names / sizes: calling DeviceCapabilit ies from C#

I'm having a devil of a time calling DeviceCapabilit ies() in order to
get the list of paper names / codes / sizes for a printer. Here is my
code and the input it produces:

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern Int32 DeviceCapabilit ies(
[MarshalAs(Unman agedType.LPTStr )] string device,
[MarshalAs(Unman agedType.LPTStr )] string port,
Int16 capability,
out IntPtr outputBuffer,
[MarshalAs(Unman agedType.LPStru ct)] DEVMODE deviceMode);

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern Int32 DeviceCapabilit ies(
[MarshalAs(Unman agedType.LPTStr )] string device,
[MarshalAs(Unman agedType.LPTStr )] string port,
Int16 capability,
out IntPtr outputBuffer,
IntPtr deviceMode);

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern bool EnumPrintersW(I nt32 flags,
[MarshalAs(Unman agedType.LPTStr )] string printerName,
Int32 level, IntPtr buffer, Int32 bufferSize, out Int32
requiredBufferS ize, out Int32 numPrintersRetu rned);

public static PaperInfo[] GetDefinedPaper s(string printerName)
{
PRINTER_INFO_5 info5;
int requiredSize;
int numPrinters;
bool foundPrinter = EnumPrintersW(P RINTER_ENUM_LOC AL |
PRINTER_ENUM_CO NNECTIONS, printerName, 5, (IntPtr)null, 0, out
requiredSize, out numPrinters);
Console.WriteLi ne("Required size is: {0}", requiredSize);
int info5Size = requiredSize;
IntPtr info5Ptr = Marshal.AllocHG lobal(info5Size );
try
{
foundPrinter = EnumPrintersW(P RINTER_ENUM_LOC AL |
PRINTER_ENUM_CO NNECTIONS, printerName, 5, info5Ptr, info5Size, out
requiredSize, out numPrinters);
Console.WriteLi ne("Size: {0}, required size: {1}, num printers:
{2}", info5Size, requiredSize, numPrinters);
string port = null;
for (int i = 0; i < numPrinters; i++)
{
info5 = (PRINTER_INFO_5 )Marshal.PtrToS tructure((IntPt r)((i *
Marshal.SizeOf( typeof(PRINTER_ INFO_5))) + (int)info5Ptr),
typeof(PRINTER_ INFO_5));
if (info5.PrinterN ame == printerName)
{
port = info5.PortName;
}
Console.WriteLi ne("Printer: '{0}', Port:'{1}'", info5.PrinterNa me,
info5.PortName) ;
}
IntPtr buffer;
int numNames = DeviceCapabilit ies(printerName , port, DC_PAPERNAMES,
out buffer, (IntPtr)null);
if (numNames < 0)
{
int errorCode = GetLastError();
IntPtr bufferPtr;
FormatMessageW( FORMAT_MESSAGE_ ALLOCATE_BUFFER |
FORMAT_MESSAGE_ FROM_SYSTEM, (IntPtr)null, errorCode,
GetUserDefaultL angID(), out bufferPtr, 0, (IntPtr)null);
string errorMessage = Marshal.PtrToSt ringUni(bufferP tr);
Console.WriteLi ne("Number of names = {2}: {0} / {1}", errorCode,
errorMessage, numNames);
return new PaperInfo[0];
}
string[] names = new string[numNames];
for (int i = 0; i < numNames; i++)
{
names[i] = Marshal.PtrToSt ringAuto((IntPt r)((i * 64) +
(int)buffer), 64);
}
int numPapers = DeviceCapabilit ies(printerName , port, DC_PAPERS,
out buffer, (IntPtr)null);
if (numPapers < 0)
{
Console.WriteLi ne("No papers");
return new PaperInfo[0];
}
short[] kinds = new short[numPapers];
for (int i = 0; i < numPapers; i++)
{
kinds[i] = Marshal.ReadInt 16(buffer, i * 16);
}
int numSizes = DeviceCapabilit ies(printerName , port, DC_PAPERSIZE,
out buffer, (IntPtr)null);
if (numSizes < 0)
{
Console.WriteLi ne("No sizes");
return new PaperInfo[0];
}
int[] widths = new int[numSizes];
int[] heights = new int[numSizes];
for (int i = 0; i < numSizes; i++)
{
widths[i] = Marshal.ReadInt 32(buffer, i * 64);
heights[i] = Marshal.ReadInt 32(buffer, i * 64 + 32);
}

int finalSize = Math.Min(Math.M in(numNames, numPapers), numSizes);
PaperInfo[] result = new PaperInfo[finalSize];
for (int i = 0; i < finalSize; i++)
{
result[i] = new PaperInfo(names[i], kinds[i], widths[i],
heights[i]);
}
return result;
}
finally
{
Marshal.FreeHGl obal(info5Ptr);
}
}

The output from this method is:

Required size is: 444
Size: 444, required size: 444, num printers: 4
Printer: 'Zebra IS', Port:'IP_xxx.xx x.xxx.xxx'
Printer: 'Zebra 105SL (200dpi)', Port:'IP_yyy.yy y.yyy.yyy'
Printer: 'Microsoft Office Live Meeting Document Writer',
Port:'Microsoft Office Live Meeting Document Writer Port:'
Printer: 'Main IS', Port:'zzz.zzz.z zz.zzz'
Number of names = -1: 126 / The specified module could not be found.

Does anyone know why the call to DeviceCapabilit ies() fails with a
LastError indicating "The specified module could not be found"? Do I
have to load the device driver first, or something? The printer and
port name I am passing to DeviceCapabilit ies is the correct one.

Nov 17 '05 #1
1 11856
Hi Bruce

Have been successful in fixing the problem
I am also having the same problem with devicecapabilit ies.
Can you let me know what you found.

thanks.
Iyyengar.

"Bruce Wood" wrote:
I'm having a devil of a time calling DeviceCapabilit ies() in order to
get the list of paper names / codes / sizes for a printer. Here is my
code and the input it produces:

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern Int32 DeviceCapabilit ies(
[MarshalAs(Unman agedType.LPTStr )] string device,
[MarshalAs(Unman agedType.LPTStr )] string port,
Int16 capability,
out IntPtr outputBuffer,
[MarshalAs(Unman agedType.LPStru ct)] DEVMODE deviceMode);

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern Int32 DeviceCapabilit ies(
[MarshalAs(Unman agedType.LPTStr )] string device,
[MarshalAs(Unman agedType.LPTStr )] string port,
Int16 capability,
out IntPtr outputBuffer,
IntPtr deviceMode);

[DllImport("wins pool.drv", SetLastError=tr ue)]
static extern bool EnumPrintersW(I nt32 flags,
[MarshalAs(Unman agedType.LPTStr )] string printerName,
Int32 level, IntPtr buffer, Int32 bufferSize, out Int32
requiredBufferS ize, out Int32 numPrintersRetu rned);

public static PaperInfo[] GetDefinedPaper s(string printerName)
{
PRINTER_INFO_5 info5;
int requiredSize;
int numPrinters;
bool foundPrinter = EnumPrintersW(P RINTER_ENUM_LOC AL |
PRINTER_ENUM_CO NNECTIONS, printerName, 5, (IntPtr)null, 0, out
requiredSize, out numPrinters);
Console.WriteLi ne("Required size is: {0}", requiredSize);
int info5Size = requiredSize;
IntPtr info5Ptr = Marshal.AllocHG lobal(info5Size );
try
{
foundPrinter = EnumPrintersW(P RINTER_ENUM_LOC AL |
PRINTER_ENUM_CO NNECTIONS, printerName, 5, info5Ptr, info5Size, out
requiredSize, out numPrinters);
Console.WriteLi ne("Size: {0}, required size: {1}, num printers:
{2}", info5Size, requiredSize, numPrinters);
string port = null;
for (int i = 0; i < numPrinters; i++)
{
info5 = (PRINTER_INFO_5 )Marshal.PtrToS tructure((IntPt r)((i *
Marshal.SizeOf( typeof(PRINTER_ INFO_5))) + (int)info5Ptr),
typeof(PRINTER_ INFO_5));
if (info5.PrinterN ame == printerName)
{
port = info5.PortName;
}
Console.WriteLi ne("Printer: '{0}', Port:'{1}'", info5.PrinterNa me,
info5.PortName) ;
}
IntPtr buffer;
int numNames = DeviceCapabilit ies(printerName , port, DC_PAPERNAMES,
out buffer, (IntPtr)null);
if (numNames < 0)
{
int errorCode = GetLastError();
IntPtr bufferPtr;
FormatMessageW( FORMAT_MESSAGE_ ALLOCATE_BUFFER |
FORMAT_MESSAGE_ FROM_SYSTEM, (IntPtr)null, errorCode,
GetUserDefaultL angID(), out bufferPtr, 0, (IntPtr)null);
string errorMessage = Marshal.PtrToSt ringUni(bufferP tr);
Console.WriteLi ne("Number of names = {2}: {0} / {1}", errorCode,
errorMessage, numNames);
return new PaperInfo[0];
}
string[] names = new string[numNames];
for (int i = 0; i < numNames; i++)
{
names[i] = Marshal.PtrToSt ringAuto((IntPt r)((i * 64) +
(int)buffer), 64);
}
int numPapers = DeviceCapabilit ies(printerName , port, DC_PAPERS,
out buffer, (IntPtr)null);
if (numPapers < 0)
{
Console.WriteLi ne("No papers");
return new PaperInfo[0];
}
short[] kinds = new short[numPapers];
for (int i = 0; i < numPapers; i++)
{
kinds[i] = Marshal.ReadInt 16(buffer, i * 16);
}
int numSizes = DeviceCapabilit ies(printerName , port, DC_PAPERSIZE,
out buffer, (IntPtr)null);
if (numSizes < 0)
{
Console.WriteLi ne("No sizes");
return new PaperInfo[0];
}
int[] widths = new int[numSizes];
int[] heights = new int[numSizes];
for (int i = 0; i < numSizes; i++)
{
widths[i] = Marshal.ReadInt 32(buffer, i * 64);
heights[i] = Marshal.ReadInt 32(buffer, i * 64 + 32);
}

int finalSize = Math.Min(Math.M in(numNames, numPapers), numSizes);
PaperInfo[] result = new PaperInfo[finalSize];
for (int i = 0; i < finalSize; i++)
{
result[i] = new PaperInfo(names[i], kinds[i], widths[i],
heights[i]);
}
return result;
}
finally
{
Marshal.FreeHGl obal(info5Ptr);
}
}

The output from this method is:

Required size is: 444
Size: 444, required size: 444, num printers: 4
Printer: 'Zebra IS', Port:'IP_xxx.xx x.xxx.xxx'
Printer: 'Zebra 105SL (200dpi)', Port:'IP_yyy.yy y.yyy.yyy'
Printer: 'Microsoft Office Live Meeting Document Writer',
Port:'Microsoft Office Live Meeting Document Writer Port:'
Printer: 'Main IS', Port:'zzz.zzz.z zz.zzz'
Number of names = -1: 126 / The specified module could not be found.

Does anyone know why the call to DeviceCapabilit ies() fails with a
LastError indicating "The specified module could not be found"? Do I
have to load the device driver first, or something? The printer and
port name I am passing to DeviceCapabilit ies is the correct one.

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
17459
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which...
0
1245
by: Kevin | last post by:
I'm trying to convert my VB6 program to VB2005. In my VB6 program I'm able to retrieve the paper bins for the selected printer and set the bin when printing. I'm using the DeviceCapabilities API and would like an example of it in NET. I've spent hours searching Google and only find VB6 examples.
2
3980
by: anniec | last post by:
Hi, I've got a Zebra 110XIIIPlus and I've made a barcode software in A2K. I need to be able to print 4 different size of barcode... all in 4'' widht. My problem is that all label are being printed in the size that is the default in the printer driver setting... no matter which paper size I choose in my report. I'm either missing...
7
2031
by: Jean Paul Mertens | last post by:
Hello, Is there a way to send a string of text to a generic tekst printer under ..NET. Somethings as in the good old days File f = Open("LPT1"); f.Writeline("Blablabla"); The goal is to use an old lineprinter as a log printer printing out each line (incomming allert) at a time without having to build a whole page
1
4821
by: Ed Sutton | last post by:
My C# app. apparently has problems with PageSettings not being initialized properly after calling PageSetupDialog. If a user selects a non-default printer from PageSetupDialog, the PageSize is apparently not-initialized properly. It appears to be printing to a very small page size and only prints a small piece of each page. Alternatively,...
0
1533
by: Academic | last post by:
I posted this in the vb group but it is a pretty esoteric question and I did not get a reply. I'm hoping someone here has some experience generating custom printer forms. If I generate a new form by using Start/Printer and Faxes and then right click a printer and chose Properties and then generate a new form, my program then shows the...
6
9749
by: Ian | last post by:
I am trying to get MS Access 2000 to print names and addresses onto a Dymo 400 label printer, I have set the Access Report to automatically select the correct printer, then select the label size of “99012 Large Address”. This works fine but when I move my database to another PC (with identical Dymo printer and drivers) the report defaults...
7
6031
by: ARC | last post by:
Hello all, What's the proper paper size setting if you want to do a receipt printer report, that's a continuous form? I don't really see an option for a continuous paper size. Thanks! Andy
23
11418
by: Al Grant | last post by:
I have a 'printable' button to generate printable output and want this to use A3 if available, irrespective of the user's default printer setting. Is there any way to set a page's preferred printed paper size from HTML? Portable if possible, but IE-only would be better than nothing.
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.