473,412 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 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 19728

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maileen | last post by:
Hi, I have 2 questions : 1. - I want to print from my ASP page directly to my network printer. For this i use the following code, but everytime, my document to print is sent to local and...
0
by: KohlerTommy | last post by:
In my application I need to give the user the ability to print duplex if the selected printer supports duplex printing. Many of the printer options do not make much sense in my application, and...
0
by: Tessa | last post by:
Is there any security reason why you cannot print to a network printer from ASP.NET under IIS6 on Windows 2003 server? I'm using ASP.NET code to print to a server print queue using...
0
by: Dawn | last post by:
My application is coded to create a report using Excel and I would like to send it directly to a pre-determined network printer. (My intention is to pass the printer name in a variable.) However,...
1
by: empika | last post by:
Hi how do i open a network printer as a file in C#? I can open the printer and write a line to it in vb6 just like this: Open FindPrinter("\\ip123\thePrinter") For Output As #1 Print #1,...
3
by: george | last post by:
Hi group, I am new to this group and since I didn´t find anything on Internet related to my problem I wanted to know if anyone know or may help me. What I have to do is: - Monitor a Network...
0
by: jigsmshah | last post by:
i am working on a project (windows service using VB.Net and C#) which gets the check details and check images from the database and prints the check to a printer. Printer name is configured in a...
1
by: ekynox | last post by:
Hi guys, have been playing with WMI to add a network printer connection to a Windows XP pc. My environment consists of a server running Windows Server 2003 and Visual Studio 2005 and a test pc...
2
by: jawaid alam | last post by:
Hi all forum Member, I am facing very tropical type of problem. I devoloped a intranet web project. I want to print crystal report by selecting network printer. I accessed all network printer...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.