Does .Netframework have a way to add a network printer

  • Thread starter Thread starter Ken
  • Start date Start date
K

Ken

Hello Everyone,

I think that there is no way to add a network printer
in .Net. I have tried ,as an alternative, using windows
API AddPrinter. This was not successful. Does anybody have
a possible solution in .NET?

Thank You in Advance for Your Help,
Ken
 
Hi Ken,

In Window NT/2000/XP, you should use AddPrinterConnection() API function to
add a printer.
My source code was listed below:

using System;
using System.Runtime .InteropServices ;

namespace addprinterconnec
{

class Class1
{

[DllImport("winspool.drv")]
public static extern bool AddPrinterConnection(string pName);

[STAThread]
static void Main(string[] args)
{
bool result;
try
{
result=AddPrinterConnection ("\\\\sha-prn-01\\2017hp8100");
Console.WriteLine ("result:"+result);
}
catch(Exception e)
{
Console.WriteLine ("Exception:"+e.Message );
}
Console.Read ();
}
}
}

The parameter of AddPrinterConnection is "\\\\server\\printer's share name"
This code worked well on my machine.

Hope this 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.

--------------------
| Content-Class: urn:content-classes:message
| From: "Ken" <[email protected]>
| Sender: "Ken" <[email protected]>
| Subject: Does .Netframework have a way to add a network printer
| Date: Tue, 5 Aug 2003 06:33:04 -0700
| Lines: 9
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNbVhl/GdfEzsr4QRSdDXLFhjKK9w==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174286
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hello Everyone,
|
| I think that there is no way to add a network printer
| in .Net. I have tried ,as an alternative, using windows
| API AddPrinter. This was not successful. Does anybody have
| a possible solution in .NET?
|
| Thank You in Advance for Your Help,
| Ken
|
 
Jeffrey Tan[MSFT] wrote:
|| Hi Ken,
||
|| In Window NT/2000/XP, you should use AddPrinterConnection() API
|| function to add a printer.

I guess Ken want's to add a new printer to the system, not to connect a user to an existing printer.

Ken, can you please post a repro, or at least that part that fails.

Willy.
 
Hi,

To use AddPrinter() fucntion to add a new printer:

using System;
using System.Runtime .InteropServices ;

namespace addprint
{
class Class1
{
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern int AddPrinter(string pName, uint Level, [In] ref
PRINTER_INFO_2 pPrinter);

[DllImport("KERNEL32.dll")]
static extern void ZeroMemory(ref PRINTER_INFO_2 pPrinter,int length);

[DllImport("winspool.drv")]
static extern int ClosePrinter(int hPrinter);

[DllImport("kernel32.dll")]
public static extern int GetLastError();

[STAThread]
static void Main(string[] args)
{
PRINTER_INFO_2 pi2=new PRINTER_INFO_2 ();
int hPrinter;
int herror;
ZeroMemory(ref pi2,Marshal.SizeOf(pi2));
pi2.pPrinterName = "HP LaserJet 8100 Series PCL6";
pi2.pPortName = "\\\\sha-prn-01\\2017hp8100";
pi2.pDriverName = "HP LaserJet 8100 Series PCL6";
pi2.pPrintProcessor = "WinPrint";
hPrinter = AddPrinter("", 2, ref pi2);
if(hPrinter==0)
{
herror=GetLastError();
}
ClosePrinter(hPrinter);
}

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

It works well on my machine.

Best regards,

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: "Willy Denoyette [MVP]" <[email protected]>
| References: <[email protected]>
<7D#Y3Z#[email protected]>
| Subject: Re: Does .Netframework have a way to add a network printer
| Date: Wed, 6 Aug 2003 12:16:16 +0200
| Lines: 13
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174555
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Jeffrey Tan[MSFT] wrote:
| || Hi Ken,
| ||
| || In Window NT/2000/XP, you should use AddPrinterConnection() API
| || function to add a printer.
|
| I guess Ken want's to add a new printer to the system, not to connect a
user to an existing printer.
|
| Ken, can you please post a repro, or at least that part that fails.
|
| Willy.
|
|
|
 
Note that you should call Marshal.GetLastWin32Error() instead of GetLastError() , the result of GetLastError() is unreliable in
PInvoke scenarios. (see: http://blogs.gotdotnet.com/anathan/CategoryView.aspx/Interop)

Willy.

Jeffrey Tan[MSFT] wrote:
|| Hi,
||
|| To use AddPrinter() fucntion to add a new printer:
||
|| using System;
|| using System.Runtime .InteropServices ;
||
|| namespace addprint
|| {
|| class Class1
|| {
|| [DllImport("winspool.drv", CharSet=CharSet.Auto)]
|| static extern int AddPrinter(string pName, uint Level, [In] ref
|| PRINTER_INFO_2 pPrinter);
||
|| [DllImport("KERNEL32.dll")]
|| static extern void ZeroMemory(ref PRINTER_INFO_2 pPrinter,int
|| length);
||
|| [DllImport("winspool.drv")]
|| static extern int ClosePrinter(int hPrinter);
||
|| [DllImport("kernel32.dll")]
|| public static extern int GetLastError();
||
|| [STAThread]
|| static void Main(string[] args)
|| {
|| PRINTER_INFO_2 pi2=new PRINTER_INFO_2 ();
|| int hPrinter;
|| int herror;
|| ZeroMemory(ref pi2,Marshal.SizeOf(pi2));
|| pi2.pPrinterName = "HP LaserJet 8100 Series PCL6";
|| pi2.pPortName = "\\\\sha-prn-01\\2017hp8100";
|| pi2.pDriverName = "HP LaserJet 8100 Series PCL6";
|| pi2.pPrintProcessor = "WinPrint";
|| hPrinter = AddPrinter("", 2, ref pi2);
|| if(hPrinter==0)
|| {
|| herror=GetLastError();
|| }
|| ClosePrinter(hPrinter);
|| }
||
|| [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
|| struct PRINTER_INFO_2
|| {
|| public string pServerName,
|| pPrinterName,
|| pShareName,
|| pPortName,
|| pDriverName,
|| pComment,
|| pLocation;
|| public IntPtr pDevMode;
|| public string pSepFile,
|| pPrintProcessor,
|| pDatatype,
|| pParameters;
|| public IntPtr pSecurityDescriptor;
|| public uint Attributes,
|| Priority,
|| DefaultPriority,
|| StartTime,
|| UntilTime,
|| Status,
|| cJobs,
|| AveragePPM;
|| }
|| }
|| }
||
|| It works well on my machine.
||
|| Best regards,
||
|| 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: "Willy Denoyette [MVP]" <[email protected]>
||| References: <[email protected]>
||| <7D#Y3Z#[email protected]> Subject: Re: Does
||| .Netframework have a way to add a network printer
||| Date: Wed, 6 Aug 2003 12:16:16 +0200
||| Lines: 13
||| 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: <[email protected]>
||| Newsgroups: microsoft.public.dotnet.languages.csharp
||| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
||| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
||| Xref: cpmsftngxa06.phx.gbl
||| microsoft.public.dotnet.languages.csharp:174555 X-Tomcat-NG:
||| microsoft.public.dotnet.languages.csharp
|||
||| Jeffrey Tan[MSFT] wrote:
||||| Hi Ken,
|||||
||||| In Window NT/2000/XP, you should use AddPrinterConnection() API
||||| function to add a printer.
|||
||| I guess Ken want's to add a new printer to the system, not to
||| connect a user to an existing printer.
|||
||| Ken, can you please post a repro, or at least that part that fails.
|||
||| Willy.
 
Yes, it is,
Thanks very much.

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: "Willy Denoyette [MVP]" <[email protected]>
| References: <[email protected]>
<7D#Y3Z#[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Does .Netframework have a way to add a network printer
| Date: Thu, 7 Aug 2003 12:09:23 +0200
| Lines: 121
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174824
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Note that you should call Marshal.GetLastWin32Error() instead of
GetLastError() , the result of GetLastError() is unreliable in
| PInvoke scenarios. (see:
http://blogs.gotdotnet.com/anathan/CategoryView.aspx/Interop)
|
| Willy.
|
| Jeffrey Tan[MSFT] wrote:
| || Hi,
| ||
| || To use AddPrinter() fucntion to add a new printer:
| ||
| || using System;
| || using System.Runtime .InteropServices ;
| ||
| || namespace addprint
| || {
| || class Class1
| || {
| || [DllImport("winspool.drv", CharSet=CharSet.Auto)]
| || static extern int AddPrinter(string pName, uint Level, [In] ref
| || PRINTER_INFO_2 pPrinter);
| ||
| || [DllImport("KERNEL32.dll")]
| || static extern void ZeroMemory(ref PRINTER_INFO_2 pPrinter,int
| || length);
| ||
| || [DllImport("winspool.drv")]
| || static extern int ClosePrinter(int hPrinter);
| ||
| || [DllImport("kernel32.dll")]
| || public static extern int GetLastError();
| ||
| || [STAThread]
| || static void Main(string[] args)
| || {
| || PRINTER_INFO_2 pi2=new PRINTER_INFO_2 ();
| || int hPrinter;
| || int herror;
| || ZeroMemory(ref pi2,Marshal.SizeOf(pi2));
| || pi2.pPrinterName = "HP LaserJet 8100 Series PCL6";
| || pi2.pPortName = "\\\\sha-prn-01\\2017hp8100";
| || pi2.pDriverName = "HP LaserJet 8100 Series PCL6";
| || pi2.pPrintProcessor = "WinPrint";
| || hPrinter = AddPrinter("", 2, ref pi2);
| || if(hPrinter==0)
| || {
| || herror=GetLastError();
| || }
| || ClosePrinter(hPrinter);
| || }
| ||
| || [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
| || struct PRINTER_INFO_2
| || {
| || public string pServerName,
| || pPrinterName,
| || pShareName,
| || pPortName,
| || pDriverName,
| || pComment,
| || pLocation;
| || public IntPtr pDevMode;
| || public string pSepFile,
| || pPrintProcessor,
| || pDatatype,
| || pParameters;
| || public IntPtr pSecurityDescriptor;
| || public uint Attributes,
| || Priority,
| || DefaultPriority,
| || StartTime,
| || UntilTime,
| || Status,
| || cJobs,
| || AveragePPM;
| || }
| || }
| || }
| ||
| || It works well on my machine.
| ||
| || Best regards,
| ||
| || 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: "Willy Denoyette [MVP]" <[email protected]>
| ||| References: <[email protected]>
| ||| <7D#Y3Z#[email protected]> Subject: Re: Does
| ||| .Netframework have a way to add a network printer
| ||| Date: Wed, 6 Aug 2003 12:16:16 +0200
| ||| Lines: 13
| ||| 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: <[email protected]>
| ||| Newsgroups: microsoft.public.dotnet.languages.csharp
| ||| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| ||| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| ||| Xref: cpmsftngxa06.phx.gbl
| ||| microsoft.public.dotnet.languages.csharp:174555 X-Tomcat-NG:
| ||| microsoft.public.dotnet.languages.csharp
| |||
| ||| Jeffrey Tan[MSFT] wrote:
| ||||| Hi Ken,
| |||||
| ||||| In Window NT/2000/XP, you should use AddPrinterConnection() API
| ||||| function to add a printer.
| |||
| ||| I guess Ken want's to add a new printer to the system, not to
| ||| connect a user to an existing printer.
| |||
| ||| Ken, can you please post a repro, or at least that part that fails.
| |||
| ||| Willy.
|
|
|
 
Back
Top