Connecting Tech Pros Worldwide Help | Site Map

Does .Netframework have a way to add a network printer

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2005, 05:47 AM
Ken
Guest
 
Posts: n/a
Default Does .Netframework have a way to add a network printer

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

  #2  
Old November 15th, 2005, 05:50 AM
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default RE: Does .Netframework have a way to add a network printer


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" <kkoski1@mindspring.com>
| Sender: "Ken" <kkoski1@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: <8cb201c35b56$197f8b10$a001280a@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
|

  #3  
Old November 15th, 2005, 05:51 AM
Willy Denoyette [MVP]
Guest
 
Posts: n/a
Default Re: Does .Netframework have a way to add a network printer

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.


  #4  
Old November 15th, 2005, 05:54 AM
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default Re: Does .Netframework have a way to add a network printer


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]" <willy.denoyette@skynet.be>
| References: <8cb201c35b56$197f8b10$a001280a@phx.gbl>
<7D#Y3Z#WDHA.1688@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: <u99CHPAXDHA.652@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.
|
|
|

  #5  
Old November 15th, 2005, 05:54 AM
Willy Denoyette [MVP]
Guest
 
Posts: n/a
Default Re: Does .Netframework have a way to add a network printer

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]" <willy.denoyette@skynet.be>
||| References: <8cb201c35b56$197f8b10$a001280a@phx.gbl>
||| <7D#Y3Z#WDHA.1688@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: <u99CHPAXDHA.652@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.


  #6  
Old November 15th, 2005, 05:58 AM
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default Re: Does .Netframework have a way to add a network printer


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]" <willy.denoyette@skynet.be>
| References: <8cb201c35b56$197f8b10$a001280a@phx.gbl>
<7D#Y3Z#WDHA.1688@cpmsftngxa06.phx.gbl>
<u99CHPAXDHA.652@tk2msftngp13.phx.gbl>
<tWxMvjLXDHA.2096@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: <um2A7vMXDHA.1896@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]" <willy.denoyette@skynet.be>
| ||| References: <8cb201c35b56$197f8b10$a001280a@phx.gbl>
| ||| <7D#Y3Z#WDHA.1688@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: <u99CHPAXDHA.652@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.
|
|
|

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.