473,520 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ 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 9087
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
5880
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 X509Certificate ( *.cer file ) so that it can be used to email as attachment. The real part is Encrypting using X509Certificate and CryptoServiceProvider.
1
567
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 want to use a callback from the client side to update webcontrols based on user input without using postback. I am seeking a way to stop the...
10
2633
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 | don't, I need to know. Flames welcome.
17
3483
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 using std::cin; using std::cout;
8
4894
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 namespace std;' unless the std namespace was declared first. This triggered my curiosity, and I tried to find out what the ANSI C++ standard had...
14
2126
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 too: using std::vector<int>; Both seem to "work" on the compiler I have and I can't find any documentation saying which is correct, or are both...
5
5688
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. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
5
4420
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 followed the code and it has no problem, however, when I go to start-- settings--printers there is no new printer. I need to know what I am doing...
3
8220
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and...
0
7231
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7463
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7626
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7587
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5153
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3295
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3295
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
527
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.