Windows API --AddPrinter Does not seem to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ken

    Windows API --AddPrinter Does not seem to work

    Hello Everyone,

    I am trying (for the pass 4 days) to try and add a server printer to my
    computer. Below is the code I used to add the printer, the problem is that
    nothing seems to happen!! I followed the code and it has no problem,
    however, when I go to start-- settings--printers there is no new printer. I
    need to know what I am doing wrong. Do I have to (declare/or declare and
    populate) a DevMode structure? Do I have to also create a printerconnecti on
    using the printer pointer? I have found very little on the net or microsoft
    about what is required. Some of what is below was sent to me through this
    news group, but there are some inconsistancies . I found a scrap of code that
    actual has an entry point for addprinter "AddPrinter A" , should I use that?
    It amazes me that microsoft has spent so little time on knowledge base and
    support issues about this subject.
    Any help and direction on this problem would be greatly appreciated. I have
    found myself going over the same ground and no change in the results.

    Thank You in Advance for any Help,
    Ken

    [DllImport("wins pool.drv",CharS et=CharSet.Auto )]
    private static extern IntPtr AddPrinter(stri ng myserver,
    uint dwLevel, ref PRINTER_INFO_2 pi );

    private void menuItem4_Click (object sender,
    System.EventArg s 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.pPrintProces sor = "WinPrint";
    pi.pDatatype = "RAW";
    pi.pParameters = "";
    pi.pSecurityDes criptor = mystrptr;
    //pi.Attributes = mystrptr;
    //pi.Priority = mystrptr;
    //pi.DefaultPrior ity = mystrptr;
    //pi.StartTime = mystrptr;
    //pi.UntilTime = mystrptr;
    //pi.Status = mystrptr;
    //pi.cJobs = mystrptr;
    //pi.AveragePPM = mystrptr;
    mysend2 = AddPrinter(mynu ll,2, ref
    pi);
    }

    [StructLayout(La youtKind.Sequen tial,
    CharSet=CharSet .Auto)]

    private class 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
    pSecurityDescri ptor;
    public uint Attributes;
    public uint Priority;
    public uint
    DefaultPriority ;
    public uint StartTime;
    public uint UntilTime;
    public uint Status;
    public uint cJobs;
    public uint AveragePPM;
    }


  • Willy Denoyette [MVP]

    #2
    Re: Windows API --AddPrinter Does not seem to work

    ken wrote:
    || Hello Everyone,
    ||
    || I am trying (for the pass 4 days) to try and add a server printer to
    || my computer. Below is the code I used to add the printer, the
    || problem is that nothing seems to happen!! I followed the code and it
    || has no problem, however, when I go to start-- settings--printers
    || there is no new printer. I need to know what I am doing wrong. Do I
    || have to (declare/or declare and populate) a DevMode structure? Do I
    || have to also create a printerconnecti on using the printer pointer? I
    || have found very little on the net or microsoft about what is
    || required. Some of what is below was sent to me through this news
    || group, but there are some inconsistancies . I found a scrap of code
    || that actual has an entry point for addprinter "AddPrinter A" , should
    || I use that? It amazes me that microsoft has spent so little time on
    || knowledge base and support issues about this subject.
    || Any help and direction on this problem would be greatly appreciated.
    || I have found myself going over the same ground and no change in the
    || results.
    ||
    || Thank You in Advance for any Help,
    || Ken
    ||
    || [DllImport("wins pool.drv",CharS et=CharSet.Auto )]
    || private static extern IntPtr AddPrinter(stri ng myserver,
    || uint dwLevel, ref PRINTER_INFO_2 pi );
    ||
    || private void menuItem4_Click (object sender,
    || System.EventArg s 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.pPrintProces sor = "WinPrint";
    || pi.pDatatype = "RAW";
    || pi.pParameters = "";
    || pi.pSecurityDes criptor = mystrptr;
    || //pi.Attributes = mystrptr;
    || //pi.Priority = mystrptr;
    || //pi.DefaultPrior ity = mystrptr;
    || //pi.StartTime = mystrptr;
    || //pi.UntilTime = mystrptr;
    || //pi.Status = mystrptr;
    || //pi.cJobs = mystrptr;
    || //pi.AveragePPM = mystrptr;
    || mysend2 = AddPrinter(mynu ll,2, ref
    || pi);
    || }
    ||
    || [StructLayout(La youtKind.Sequen tial,
    || CharSet=CharSet .Auto)]
    ||
    || private class 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
    || pSecurityDescri ptor;
    || public uint Attributes;
    || public uint Priority;
    || public uint
    || DefaultPriority ;
    || public uint StartTime;
    || public uint UntilTime;
    || public uint Status;
    || public uint cJobs;
    || public uint AveragePPM;
    || }


    In general if you have to go down the PInvoke road take some time to read the API description in platform SDK documentation.
    AddPrinter returns a NULL handle if the function failed, a non-null handle to be passed to the ClosePrinter API when succeeded, when
    NULL is returned one should call "Marshal.GetLas tWin32Error" to get the reason for the failure.
    Nowhere in your code I see a check for the handle returned (IntPtr), nor a call to GetLastWin32Err or.

    Please add error checking to your code and search the SDK platform documentation for an error description.

    Willy.


    Comment

    • Ken

      #3
      Re: Windows API --AddPrinter Does not seem to work

      Hello Willy,

      I am receiving no error messages, here is what I am using:
      What would you sugest next? Also, what is the SDK Platform
      CD you are refering too? I am using W2K.

      Thank You for All Your Help,
      Ken
      P.S. If microsoft wants people to buy into C#, then they
      need to have better documentation!! !!!!!!
      Let's here it for Googles

      catch
      (System.Runtime .InteropService s.MarshalDirect iveException
      exs)
      {
      MessageBox.Show ("Here is the error message" +
      exs.Message);
      }
      catch ( System.Exceptio n ex)
      {
      MessageBox.Show ("Here is the error message" + ex.Message);
      }

      The IntPtr = 1527664;
      next time =1520616;
      next time = 1528184;

      [color=blue]
      >-----Original Message-----
      >ken wrote:
      >|| Hello Everyone,
      >||
      >|| I am trying (for the pass 4 days) to try and add a[/color]
      server printer to[color=blue]
      >|| my computer. Below is the code I used to add the[/color]
      printer, the[color=blue]
      >|| problem is that nothing seems to happen!! I followed[/color]
      the code and it[color=blue]
      >|| has no problem, however, when I go to start-- settings-[/color]
      -printers[color=blue]
      >|| there is no new printer. I need to know what I am[/color]
      doing wrong. Do I[color=blue]
      >|| have to (declare/or declare and populate) a DevMode[/color]
      structure? Do I[color=blue]
      >|| have to also create a printerconnecti on using the[/color]
      printer pointer? I[color=blue]
      >|| have found very little on the net or microsoft about[/color]
      what is[color=blue]
      >|| required. Some of what is below was sent to me through[/color]
      this news[color=blue]
      >|| group, but there are some inconsistancies . I found a[/color]
      scrap of code[color=blue]
      >|| that actual has an entry point for[/color]
      addprinter "AddPrinter A" , should[color=blue]
      >|| I use that? It amazes me that microsoft has spent so[/color]
      little time on[color=blue]
      >|| knowledge base and support issues about this subject.
      >|| Any help and direction on this problem would be[/color]
      greatly appreciated.[color=blue]
      >|| I have found myself going over the same ground and no[/color]
      change in the[color=blue]
      >|| results.
      >||
      >|| Thank You in Advance for any Help,
      >|| Ken
      >||
      >|| [DllImport("wins pool.drv",CharS et=CharSet.Auto )]
      >|| private static extern IntPtr AddPrinter(stri ng[/color]
      myserver,[color=blue]
      >|| uint dwLevel, ref PRINTER_INFO_2 pi );
      >||
      >|| private void menuItem4_Click (object sender,
      >|| System.EventArg s 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-[/color]
      3-NCA" ;[color=blue]
      >|| 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.pPrintProces sor = "WinPrint";
      >|| pi.pDatatype = "RAW";
      >|| pi.pParameters = "";
      >|| pi.pSecurityDes criptor = mystrptr;
      >|| //pi.Attributes = mystrptr;
      >|| //pi.Priority = mystrptr;
      >|| //pi.DefaultPrior ity = mystrptr;
      >|| //pi.StartTime = mystrptr;
      >|| //pi.UntilTime = mystrptr;
      >|| //pi.Status = mystrptr;
      >|| //pi.cJobs = mystrptr;
      >|| //pi.AveragePPM = mystrptr;
      >|| mysend2 = AddPrinter(mynu ll,2, ref
      >|| pi);
      >|| }
      >||
      >|| [StructLayout(La youtKind.Sequen tial,
      >|| CharSet=CharSet .Auto)]
      >||
      >|| private class 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
      >|| pSecurityDescri ptor;
      >|| public uint Attributes;
      >|| public uint Priority;
      >|| public uint
      >|| DefaultPriority ;
      >|| public uint StartTime;
      >|| public uint UntilTime;
      >|| public uint Status;
      >|| public uint cJobs;
      >|| public uint AveragePPM;
      >|| }
      >
      >
      >In general if you have to go down the PInvoke road take[/color]
      some time to read the API description in platform SDK
      documentation.[color=blue]
      >AddPrinter returns a NULL handle if the function failed,[/color]
      a non-null handle to be passed to the ClosePrinter API
      when succeeded, when[color=blue]
      >NULL is returned one should[/color]
      call "Marshal.GetLas tWin32Error" to get the reason for
      the failure.[color=blue]
      >Nowhere in your code I see a check for the handle[/color]
      returned (IntPtr), nor a call to GetLastWin32Err or.[color=blue]
      >
      >Please add error checking to your code and search the SDK[/color]
      platform documentation for an error description.[color=blue]
      >
      >Willy.
      >
      >
      >.
      >[/color]

      Comment

      • Günter Prossliner

        #4
        Re: Windows API --AddPrinter Does not seem to work

        You could create a tempoary .vbs script that adds the printer via
        AddPrinterConne ction(), and execute it via Process.Start

        "ken" <kkoski1@mindsp ring.com> wrote in message
        news:OQlRFbnWDH A.1896@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Hello Everyone,
        >
        > I am trying (for the pass 4 days) to try and add a server printer to my
        > computer. Below is the code I used to add the printer, the problem is that
        > nothing seems to happen!! I followed the code and it has no problem,
        > however, when I go to start-- settings--printers there is no new printer.[/color]
        I[color=blue]
        > need to know what I am doing wrong. Do I have to (declare/or declare and
        > populate) a DevMode structure? Do I have to also create a[/color]
        printerconnecti on[color=blue]
        > using the printer pointer? I have found very little on the net or[/color]
        microsoft[color=blue]
        > about what is required. Some of what is below was sent to me through this
        > news group, but there are some inconsistancies . I found a scrap of code[/color]
        that[color=blue]
        > actual has an entry point for addprinter "AddPrinter A" , should I use[/color]
        that?[color=blue]
        > It amazes me that microsoft has spent so little time on knowledge base and
        > support issues about this subject.
        > Any help and direction on this problem would be greatly appreciated. I[/color]
        have[color=blue]
        > found myself going over the same ground and no change in the results.
        >
        > Thank You in Advance for any Help,
        > Ken
        >
        > [DllImport("wins pool.drv",CharS et=CharSet.Auto )]
        > private static extern IntPtr AddPrinter(stri ng myserver,
        > uint dwLevel, ref PRINTER_INFO_2 pi );
        >
        > private void menuItem4_Click (object sender,
        > System.EventArg s 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.pPrintProces sor = "WinPrint";
        > pi.pDatatype = "RAW";
        > pi.pParameters = "";
        > pi.pSecurityDes criptor = mystrptr;
        > //pi.Attributes = mystrptr;
        > //pi.Priority = mystrptr;
        > //pi.DefaultPrior ity = mystrptr;
        > //pi.StartTime = mystrptr;
        > //pi.UntilTime = mystrptr;
        > //pi.Status = mystrptr;
        > //pi.cJobs = mystrptr;
        > //pi.AveragePPM = mystrptr;
        > mysend2 = AddPrinter(mynu ll,2, ref
        > pi);
        > }
        >
        > [StructLayout(La youtKind.Sequen tial,
        > CharSet=CharSet .Auto)]
        >
        > private class 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
        > pSecurityDescri ptor;
        > public uint Attributes;
        > public uint Priority;
        > public uint
        > DefaultPriority ;
        > public uint StartTime;
        > public uint UntilTime;
        > public uint Status;
        > public uint cJobs;
        > public uint AveragePPM;
        > }
        >
        >[/color]


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Windows API --AddPrinter Does not seem to work

          Ken,
          see inline ****

          Ken wrote:
          || Hello Willy,
          ||
          || I am receiving no error messages, here is what I am using:
          || What would you sugest next? Also, what is the SDK Platform
          || CD you are refering too? I am using W2K.

          **** The Platform SDK is available as a free download from : http://www.microsoft.com/msdownload/...sdk/sdkupdate/ and is also
          part of the MSDN Library CD.

          You can also consult the MSDN library online : http://msdn.microsoft.com/library/default.asp
          To search the MSDN site goto MSDN home page http://msdn.microsoft.com and search for the API description, f.i searchinf for
          AddPrinter returns:



          ||
          || Thank You for All Your Help,
          || Ken
          || P.S. If microsoft wants people to buy into C#, then they
          || need to have better documentation!! !!!!!!


          **** The documentation is available, as I said what you are trying is to call a native Win32 API, this has nothing to do with C#.


          ||
          || The IntPtr = 1527664;
          || next time =1520616;
          || next time = 1528184;
          ||

          **** You should call the Win32 "ClosePrint er API using this IntPtr (Handle) as argument.



          Willy.


          Comment

          • Jeffrey Tan[MSFT]

            #6
            Re: Windows API --AddPrinter Does not seem to work


            Hi Ken,

            Yes, just as Gunter said you can add a printer via WSH.
            Maybe you can get some information in the below links:

            ml/wsmthaddwindows printerconnecti on.asp
            and

            =1

            Hope it helps.

            Jeffrey Tan
            Microsoft Online Partner Support
            Get Secure! - www.microsoft.com/security
            This posting is provided "as is" with no warranties and confers no rights.

            --------------------
            | From: "Günter Prossliner" <g.prossliner@g mx.at>
            | References: <OQlRFbnWDHA.18 96@TK2MSFTNGP12 .phx.gbl>
            | Subject: Re: Windows API --AddPrinter Does not seem to work
            | Date: Mon, 4 Aug 2003 18:16:15 +0200
            | Lines: 109
            | X-Priority: 3
            | X-MSMail-Priority: Normal
            | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
            | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
            | Message-ID: <eJ#CkOqWDHA.18 72@TK2MSFTNGP12 .phx.gbl>
            | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
            | NNTP-Posting-Host: 80.240.224.178
            | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
            | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1740 24
            | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
            |
            | You could create a tempoary .vbs script that adds the printer via
            | AddPrinterConne ction(), and execute it via Process.Start
            |
            | "ken" <kkoski1@mindsp ring.com> wrote in message
            | news:OQlRFbnWDH A.1896@TK2MSFTN GP12.phx.gbl...
            | > Hello Everyone,
            | >
            | > I am trying (for the pass 4 days) to try and add a server printer to my
            | > computer. Below is the code I used to add the printer, the problem is
            that
            | > nothing seems to happen!! I followed the code and it has no problem,
            | > however, when I go to start-- settings--printers there is no new
            printer.
            | I
            | > need to know what I am doing wrong. Do I have to (declare/or declare and
            | > populate) a DevMode structure? Do I have to also create a
            | printerconnecti on
            | > using the printer pointer? I have found very little on the net or
            | microsoft
            | > about what is required. Some of what is below was sent to me through
            this
            | > news group, but there are some inconsistancies . I found a scrap of code
            | that
            | > actual has an entry point for addprinter "AddPrinter A" , should I use
            | that?
            | > It amazes me that microsoft has spent so little time on knowledge base
            and
            | > support issues about this subject.
            | > Any help and direction on this problem would be greatly appreciated. I
            | have
            | > found myself going over the same ground and no change in the results.
            | >
            | > Thank You in Advance for any Help,
            | > Ken
            | >
            | > [DllImport("wins pool.drv",CharS et=CharSet.Auto )]
            | > private static extern IntPtr AddPrinter(stri ng myserver,
            | > uint dwLevel, ref PRINTER_INFO_2 pi );
            | >
            | > private void menuItem4_Click (object sender,
            | > System.EventArg s 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.pPrintProces sor = "WinPrint";
            | > pi.pDatatype = "RAW";
            | > pi.pParameters = "";
            | > pi.pSecurityDes criptor = mystrptr;
            | > //pi.Attributes = mystrptr;
            | > //pi.Priority = mystrptr;
            | > //pi.DefaultPrior ity = mystrptr;
            | > //pi.StartTime = mystrptr;
            | > //pi.UntilTime = mystrptr;
            | > //pi.Status = mystrptr;
            | > //pi.cJobs = mystrptr;
            | > //pi.AveragePPM = mystrptr;
            | > mysend2 = AddPrinter(mynu ll,2, ref
            | > pi);
            | > }
            | >
            | > [StructLayout(La youtKind.Sequen tial,
            | > CharSet=CharSet .Auto)]
            | >
            | > private class 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
            | > pSecurityDescri ptor;
            | > public uint Attributes;
            | > public uint Priority;
            | > public uint
            | > DefaultPriority ;
            | > public uint StartTime;
            | > public uint UntilTime;
            | > public uint Status;
            | > public uint cJobs;
            | > public uint AveragePPM;
            | > }
            | >
            | >
            |
            |
            |

            Comment

            Working...