locked
How to add a printer RRS feed

  • Question

  • I need to add a printer to a customer computer. I have the printer address but can't find any component to do this. Any way to do this programmatically ?
    Wednesday, February 1, 2006 11:58 AM

Answers

  • Try this:


    [DllImport("winspool.drv", CharSet=CharSet.Auto)]
    static extern IntPtr AddPrinter(string pName, uint Level, [In] ref
    PRINTER_INFO_2 pPrinter);

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    struct PRINTER_INFO_2
    {
      public string pServerName,
                    pPrinterName,
                    pShareName,
                    pPortName,
                    pDriverName,
                    pComment,
                    pLocation;
      public IntPt  pDevMode;
      public string pSepFile,
                    pPrintProcessor,
                    pDatatype,
                    pParameters;
      public IntPtr pSecurityDescriptor;
      public uint   Attributes,
                    Priority,
                    DefaultPriority,
                    StartTime,
                    UntilTime,
                    Status,
                    cJobs,
                    AveragePPM;
    }

     

    You can use this, this way:


    private void cmdAddPrinter_Click(object sender,
    System.EventArgs e)
    {
      string mynull;
      mynull = null;
      IntPtr mystrptr = new IntPtr(0);
      bool mysend;
      IntPtr mysend2;
      PRINTER_INFO_2 pi = new PRINTER_INFO_2();
      string myservername;
      myservername = "blahblah"

      pi.pServerName =  "\\\\" + myservername ;
      pi.pPrinterName = "\\\\" + myservername  + "\\" + "CN-3-NCA" ;
      pi.pShareName = "CN-3-NCA";
      pi.pPortName = "123.123.123.27:print";
      pi.pDriverName = "Cannon iR2200-3300 PCL6";
      pi.pComment = "No Comment";
      pi.pLocation = "3rd Floor North Alcove Copy Center";
      pi.pDevMode = mystrptr;
      pi.pSepFile = "";
      pi.pPrintProcessor = "WinPrint";
      pi.pDatatype = "RAW";
      pi.pParameters = "";
      pi.pSecurityDescriptor = mystrptr;
      mysend2 = AddPrinter(mynull,2, ref
    pi);                    
    }

     

    Wednesday, February 1, 2006 12:41 PM
  • You can do this with the sample i gave, but you must specify the port. Meybe you can scan the ports and look it a printer is listning to it. But that isn't a common practice. You can't add a printer only on IP.

    You can scan for connected and findable printers, here is a example.
    Thursday, February 2, 2006 10:22 AM

All replies

  • Try this:


    [DllImport("winspool.drv", CharSet=CharSet.Auto)]
    static extern IntPtr AddPrinter(string pName, uint Level, [In] ref
    PRINTER_INFO_2 pPrinter);

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    struct PRINTER_INFO_2
    {
      public string pServerName,
                    pPrinterName,
                    pShareName,
                    pPortName,
                    pDriverName,
                    pComment,
                    pLocation;
      public IntPt  pDevMode;
      public string pSepFile,
                    pPrintProcessor,
                    pDatatype,
                    pParameters;
      public IntPtr pSecurityDescriptor;
      public uint   Attributes,
                    Priority,
                    DefaultPriority,
                    StartTime,
                    UntilTime,
                    Status,
                    cJobs,
                    AveragePPM;
    }

     

    You can use this, this way:


    private void cmdAddPrinter_Click(object sender,
    System.EventArgs e)
    {
      string mynull;
      mynull = null;
      IntPtr mystrptr = new IntPtr(0);
      bool mysend;
      IntPtr mysend2;
      PRINTER_INFO_2 pi = new PRINTER_INFO_2();
      string myservername;
      myservername = "blahblah"

      pi.pServerName =  "\\\\" + myservername ;
      pi.pPrinterName = "\\\\" + myservername  + "\\" + "CN-3-NCA" ;
      pi.pShareName = "CN-3-NCA";
      pi.pPortName = "123.123.123.27:print";
      pi.pDriverName = "Cannon iR2200-3300 PCL6";
      pi.pComment = "No Comment";
      pi.pLocation = "3rd Floor North Alcove Copy Center";
      pi.pDevMode = mystrptr;
      pi.pSepFile = "";
      pi.pPrintProcessor = "WinPrint";
      pi.pDatatype = "RAW";
      pi.pParameters = "";
      pi.pSecurityDescriptor = mystrptr;
      mysend2 = AddPrinter(mynull,2, ref
    pi);                    
    }

     

    Wednesday, February 1, 2006 12:41 PM
  • Thanks a lot, i try this right now.
    Wednesday, February 1, 2006 12:53 PM
  • I'm looking forward to your experience!
    Wednesday, February 1, 2006 1:16 PM
  • Which port should i use for a network printer ?
    Wednesday, February 1, 2006 4:12 PM
  • This depends on wish port he is listening.
    Wednesday, February 1, 2006 4:15 PM
  • Well, i want to add a network printer. I have the printer shared name, the name of the computer on which the printer is plugged and the name of the printer driver. But i can't find how to use those information to add the printer to my computer programmatically :(
    Thursday, February 2, 2006 10:08 AM
  • You can do this with the sample i gave, but you must specify the port. Meybe you can scan the ports and look it a printer is listning to it. But that isn't a common practice. You can't add a printer only on IP.

    You can scan for connected and findable printers, here is a example.
    Thursday, February 2, 2006 10:22 AM