[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);!!!!VB Signature:
Declare Function AddPrinter Lib "Winspool.dll" (TODO) As TODO
None.
Do you know one? Please contribute it!
Sample code is done as a console application with hardcoded parameters to make the example simple
Please add some!
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;
}
}