AddPrinter using C#

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

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
 
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.
 
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;
}
 
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" <[email protected]>
| Sender: "Ken" <[email protected]>
| References: <[email protected]>
<#[email protected]>
| Subject: Re: AddPrinter using C#
| Date: Fri, 1 Aug 2003 08:24:06 -0700
| Lines: 141
| Message-ID: <[email protected]>
| 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.
| >.
| >
|
 
Back
Top