473,465 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

AddPrinter using C#

Ken
Hello All,

I am hoping someone has used the windows GDI AddPrinter
API. What I need to know is how the
[dllimport("winspool.drv",etc looks like
and the
private addprinter() looks like.
There is nothing on the open web that has this infomation.
Here is what works for GetPrinter. What do I need for
AddPrinter?

[DllImport("winspool.Drv", EntryPoint="GetPrinterA",
SetLastError=true,
CharSet=CharSet.Ansi,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]

private static extern bool GetPrinter(IntPtr hPrinter,
Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32
dwNeeded);

Thank You in Advance for Your Help,
Ken
Nov 15 '05 #1
5 9077
ken
Hello Mattias,

Thank You , I will try this code tommorrow!!
I have two questions about the code.
First, the pDevMode-- There is a huge structure for DevMode -- do I need to
build that or will it default to what is already defined in the system?
Second, I read that for pSecurityDescriptor if you set it to null that it
will take the security that has been defined for the system. - Could you
show me the line of code that will set the pointer to null? Believe it or
not I can't get it to take null so there must be a special null type for
IntPtr.

Thank You for All Your Help,
Ken
P.S. When I am successful with this printer install I will post it.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:#Z**************@TK2MSFTNGP12.phx.gbl...
Ken,

Try this

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern IntPtr AddPrinter(string pName, uint Level, [In] ref
PRINTER_INFO_2 pPrinter);

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #2
ken
Hello Mattias,

Thank You , I will try this code tommorrow!!
I have two questions about the code.
First, the pDevMode-- There is a huge structure for DevMode -- do I need to
build that or will it default to what is already defined in the system?
Second, I read that for pSecurityDescriptor if you set it to null that it
will take the security that has been defined for the system. - Could you
show me the line of code that will set the pointer to null? Believe it or
not I can't get it to take null so there must be a special null type for
IntPtr.

Thank You for All Your Help,
Ken
P.S. When I am successful with this printer install I will post it.

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:#Z**************@TK2MSFTNGP12.phx.gbl...
Ken,

Try this

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern IntPtr AddPrinter(string pName, uint Level, [In] ref
PRINTER_INFO_2 pPrinter);

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #3
Ken
Hello Mattias,

I took your suggestion , but I still cannot see the
printer on my machine. Here is what I am using:
Any suggestions?

Thank You in Advance for All Your Help,
Ken

[DllImport("winspool.drv",CharSet=CharSet.Auto)]
private static extern IntPtr AddPrinter(string myserver,
uint dwLevel, ref PRINTER_INFO_2 pi );

private void menuItem4_Click(object sender,
System.EventArgs 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.pPrintProcessor = "WinPrint";
pi.pDatatype = "RAW";
pi.pParameters = "";
pi.pSecurityDescriptor =
mystrptr;
//pi.Attributes = mystrptr;
//pi.Priority = mystrptr;
//pi.DefaultPriority = mystrptr;
//pi.StartTime = mystrptr;
//pi.UntilTime = mystrptr;
//pi.Status = mystrptr;
//pi.cJobs = mystrptr;
//pi.AveragePPM = mystrptr;
mysend2 = AddPrinter(mynull,2, ref
pi);
}

[StructLayout(LayoutKind.Sequential,
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
pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint
DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}

-----Original Message-----
Ken,

Try this

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern IntPtr AddPrinter(string pName, uint Level, [In] refPRINTER_INFO_2 pPrinter);

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Nov 15 '05 #4
Ken
Hello Mattias,

I took your suggestion , but I still cannot see the
printer on my machine. Here is what I am using:
Any suggestions?

Thank You in Advance for All Your Help,
Ken

[DllImport("winspool.drv",CharSet=CharSet.Auto)]
private static extern IntPtr AddPrinter(string myserver,
uint dwLevel, ref PRINTER_INFO_2 pi );

private void menuItem4_Click(object sender,
System.EventArgs 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.pPrintProcessor = "WinPrint";
pi.pDatatype = "RAW";
pi.pParameters = "";
pi.pSecurityDescriptor =
mystrptr;
//pi.Attributes = mystrptr;
//pi.Priority = mystrptr;
//pi.DefaultPriority = mystrptr;
//pi.StartTime = mystrptr;
//pi.UntilTime = mystrptr;
//pi.Status = mystrptr;
//pi.cJobs = mystrptr;
//pi.AveragePPM = mystrptr;
mysend2 = AddPrinter(mynull,2, ref
pi);
}

[StructLayout(LayoutKind.Sequential,
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
pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint
DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}

-----Original Message-----
Ken,

Try this

[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern IntPtr AddPrinter(string pName, uint Level, [In] refPRINTER_INFO_2 pPrinter);

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Nov 15 '05 #5

Hi Ken,

In Windows NT/2000/XP, you should use AddPrinterConnection().
You can get more information in my reply in the post: "Does .Netframework
have a way to add a network printer"

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.

--------------------
| Content-Class: urn:content-classes:message
| From: "Ken" <kk*****@mindspring.com>
| Sender: "Ken" <kk*****@mindspring.com>
| References: <00****************************@phx.gbl>
<#Z**************@TK2MSFTNGP12.phx.gbl>
| Subject: Re: AddPrinter using C#
| Date: Fri, 1 Aug 2003 08:24:06 -0700
| Lines: 141
| Message-ID: <00****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: quoted-printable
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcNYQPJkVi2sbwtIQ8+XzsyNmovLTQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:173567
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hello Mattias,
| I took your suggestion , but I still cannot see the
| printer on my machine. Here is what I am using:
| Any suggestions?
| Thank You in Advance for All Your Help,
| Ken
| [DllImport("winspool.drv",CharSet=CharSet.Auto)]
| private static extern IntPtr AddPrinter(string myserver,
| uint dwLevel, ref PRINTER_INFO_2 pi );
| private void menuItem4_Click(object sender,
| System.EventArgs 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.pPrintProcessor = "WinPrint";
| pi.pDatatype = "RAW";
| pi.pParameters = "";
| pi.pSecurityDescriptor =
| mystrptr;
| //pi.Attributes = mystrptr;
| //pi.Priority = mystrptr;
| //pi.DefaultPriority = mystrptr;
| //pi.StartTime = mystrptr;
| //pi.UntilTime = mystrptr;
| //pi.Status = mystrptr;
| //pi.cJobs = mystrptr;
| //pi.AveragePPM = mystrptr;
| mysend2 = AddPrinter(mynull,2, ref
| pi);
| }
|
| [StructLayout(LayoutKind.Sequential,
| 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
| pSecurityDescriptor;
| public uint Attributes;
| public uint Priority;
| public uint
| DefaultPriority;
| public uint StartTime;
| public uint UntilTime;
| public uint Status;
| public uint cJobs;
| public uint AveragePPM;
| }
| >-----Original Message-----
| >Ken,
| >
| >Try this
| >
| >[DllImport("winspool.drv", CharSet=CharSet.Auto)]
| >static extern IntPtr AddPrinter(string pName, uint Level,
| [In] ref
| >PRINTER_INFO_2 pPrinter);
| >
| >[StructLayout(LayoutKind.Sequential,
| CharSet=CharSet.Auto)]
| >struct PRINTER_INFO_2
| >{
| > public string pServerName,
| > pPrinterName,
| > pShareName,
| > pPortName,
| > pDriverName,
| > pComment,
| > pLocation;
| > public IntPt pDevMode;
| > public string pSepFile,
| > pPrintProcessor,
| > pDatatype,
| > pParameters;
| > public IntPtr pSecurityDescriptor;
| > public uint Attributes,
| > Priority,
| > DefaultPriority,
| > StartTime,
| > UntilTime,
| > Status,
| > cJobs,
| > AveragePPM;
| >}
| >
| >
| >
| >Mattias
| >
| >--
| >Mattias Sjögren [MVP] mattias @ mvps.org
| >http://www.msjogren.net/dotnet/
| >Please reply only to the newsgroup.
| >.
| >
|

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
5
by: ken | last post by:
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...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.