471,071 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Does .Netframework have a way to add a network printer

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
Nov 15 '05 #1
5 19449

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" <kk*****@mindspring.com>
| Sender: "Ken" <kk*****@mindspring.com>
| Subject: Does .Netframework have a way to add a network printer
| Date: Tue, 5 Aug 2003 06:33:04 -0700
| Lines: 9
| Message-ID: <8c****************************@phx.gbl>
| 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
|

Nov 15 '05 #2
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.
Nov 15 '05 #3

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]" <wi*************@skynet.be>
| References: <8c****************************@phx.gbl>
<7D**************@cpmsftngxa06.phx.gbl>
| 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: <u9*************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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.
|
|
|

Nov 15 '05 #4
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/C...w.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]" <wi*************@skynet.be>
||| References: <8c****************************@phx.gbl>
||| <7D**************@cpmsftngxa06.phx.gbl> 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: <u9*************@tk2msftngp13.phx.gbl>
||| Newsgroups: microsoft.public.dotnet.languages.csharp
||| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
||| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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.
Nov 15 '05 #5

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]" <wi*************@skynet.be>
| References: <8c****************************@phx.gbl>
<7D**************@cpmsftngxa06.phx.gbl>
<u9*************@tk2msftngp13.phx.gbl>
<tW**************@cpmsftngxa06.phx.gbl>
| 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: <um**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.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/C...w.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]" <wi*************@skynet.be>
| ||| References: <8c****************************@phx.gbl>
| ||| <7D**************@cpmsftngxa06.phx.gbl> 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: <u9*************@tk2msftngp13.phx.gbl>
| ||| Newsgroups: microsoft.public.dotnet.languages.csharp
| ||| NNTP-Posting-Host: d5e01079.kabel.telenet.be 213.224.16.121
| ||| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.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.
|
|
|

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by KohlerTommy | last post: by
1 post views Thread by empika | last post: by
3 posts views Thread by george | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.