Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than winspool, prefix the name with the module name and a period.
Declare Function AddPrinter Lib "Winspool.dll" (TODO) As TODO
User-Defined Types:
None.
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
Sample code is done as a console application with hardcoded parameters to make the example simple
Tips & Tricks:
Please add some!
Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace AddPrinter
{
class Program
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
static extern int AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter);
[DllImport("winspool.drv")]
static extern int ClosePrinter(int hPrinter);
static void Main(string[] args)
{
PRINTER_INFO_2 pInfo = new PRINTER_INFO_2();
int hPrt;
int iError;
pInfo.pPrinterName = "Test";
pInfo.pPortName = "IP_10.63.185.199";
pInfo.pDriverName = "HP LaserJet 4 Plus";
pInfo.pPrintProcessor = "WinPrint";
hPrt = AddPrinter("", 2, ref pInfo);
if (hPrt == 0)
{
iError = Marshal.GetLastWin32Error();
switch (iError)
{
case 1796: Console.WriteLine("The specified port is unknown"); break;
case 1797: Console.WriteLine("Printer driver is unknown or not loaded"); break;
case 1798: Console.WriteLine("The print processor is unknown"); break;
case 1801: Console.WriteLine("Printer name is invalid"); break;
case 1802: Console.WriteLine("Printer name already exists"); break;
default: Console.WriteLine("An error occured. Errorcode: " + iError); break;
}
}
else
{
Console.WriteLine("Printer was created successfully"); }
ClosePrinter(hPrt);
Console.WriteLine("Press any key to exit!");
Console.ReadKey();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct PRINTER_INFO_2
{
public string pServerName;
public string pPrinterName;
public string pShareName;
public string pPortName;
public string pDriverName;
public string pComment;
public string pLocation;
public IntPtr pDevMode;
public string pSepFile;
public string pPrintProcessor;
public string pDatatype;
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).