Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

AddPrinter (winspool)
 
.
Summary
Add a printer to the local (or remote) print system

C# Signature:

[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

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;
   }

}

Documentation
AddPrinter on MSDN

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).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions