Windows API --AddPrinter Does not seem to work

K

ken

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 wrong. Do I have to (declare/or declare and
populate) a DevMode structure? Do I have to also create a printerconnection
using the printer pointer? I have found very little on the net or microsoft
about what is required. Some of what is below was sent to me through this
news group, but there are some inconsistancies. I found a scrap of code that
actual has an entry point for addprinter "AddPrinterA" , should I use that?
It amazes me that microsoft has spent so little time on knowledge base and
support issues about this subject.
Any help and direction on this problem would be greatly appreciated. I have
found myself going over the same ground and no change in the results.

Thank You in Advance for any 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;
}
 
W

Willy Denoyette [MVP]

ken wrote:
|| 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 wrong. Do I
|| have to (declare/or declare and populate) a DevMode structure? Do I
|| have to also create a printerconnection using the printer pointer? I
|| have found very little on the net or microsoft about what is
|| required. Some of what is below was sent to me through this news
|| group, but there are some inconsistancies. I found a scrap of code
|| that actual has an entry point for addprinter "AddPrinterA" , should
|| I use that? It amazes me that microsoft has spent so little time on
|| knowledge base and support issues about this subject.
|| Any help and direction on this problem would be greatly appreciated.
|| I have found myself going over the same ground and no change in the
|| results.
||
|| Thank You in Advance for any 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;
|| }


In general if you have to go down the PInvoke road take some time to read the API description in platform SDK documentation.
AddPrinter returns a NULL handle if the function failed, a non-null handle to be passed to the ClosePrinter API when succeeded, when
NULL is returned one should call "Marshal.GetLastWin32Error" to get the reason for the failure.
Nowhere in your code I see a check for the handle returned (IntPtr), nor a call to GetLastWin32Error.

Please add error checking to your code and search the SDK platform documentation for an error description.

Willy.
 
K

Ken

Hello Willy,

I am receiving no error messages, here is what I am using:
What would you sugest next? Also, what is the SDK Platform
CD you are refering too? I am using W2K.

Thank You for All Your Help,
Ken
P.S. If microsoft wants people to buy into C#, then they
need to have better documentation!!!!!!!!
Let's here it for Googles

catch
(System.Runtime.InteropServices.MarshalDirectiveException
exs)
{
MessageBox.Show ("Here is the error message" +
exs.Message);
}
catch ( System.Exception ex)
{
MessageBox.Show ("Here is the error message" + ex.Message);
}

The IntPtr = 1527664;
next time =1520616;
next time = 1528184;

-----Original Message-----
ken wrote:
|| 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 wrong. Do I
|| have to (declare/or declare and populate) a DevMode structure? Do I
|| have to also create a printerconnection using the printer pointer? I
|| have found very little on the net or microsoft about what is
|| required. Some of what is below was sent to me through this news
|| group, but there are some inconsistancies. I found a scrap of code
|| that actual has an entry point for
addprinter "AddPrinterA" , should
|| I use that? It amazes me that microsoft has spent so little time on
|| knowledge base and support issues about this subject.
|| Any help and direction on this problem would be greatly appreciated.
|| I have found myself going over the same ground and no change in the
|| results.
||
|| Thank You in Advance for any 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;
|| }


In general if you have to go down the PInvoke road take
some time to read the API description in platform SDK
documentation.
AddPrinter returns a NULL handle if the function failed,
a non-null handle to be passed to the ClosePrinter API
when succeeded, when
NULL is returned one should
call "Marshal.GetLastWin32Error" to get the reason for
the failure.
Nowhere in your code I see a check for the handle
returned (IntPtr), nor a call to GetLastWin32Error.
Please add error checking to your code and search the SDK
platform documentation for an error description.
 
G

Günter Prossliner

You could create a tempoary .vbs script that adds the printer via
AddPrinterConnection(), and execute it via Process.Start

ken said:
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 wrong. Do I have to (declare/or declare and
populate) a DevMode structure? Do I have to also create a printerconnection
using the printer pointer? I have found very little on the net or microsoft
about what is required. Some of what is below was sent to me through this
news group, but there are some inconsistancies. I found a scrap of code that
actual has an entry point for addprinter "AddPrinterA" , should I use that?
It amazes me that microsoft has spent so little time on knowledge base and
support issues about this subject.
Any help and direction on this problem would be greatly appreciated. I have
found myself going over the same ground and no change in the results.

Thank You in Advance for any 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;
}
 
W

Willy Denoyette [MVP]

Ken,
see inline ****

Ken wrote:
|| Hello Willy,
||
|| I am receiving no error messages, here is what I am using:
|| What would you sugest next? Also, what is the SDK Platform
|| CD you are refering too? I am using W2K.

**** The Platform SDK is available as a free download from : http://www.microsoft.com/msdownload/platformsdk/sdkupdate/ and is also
part of the MSDN Library CD.

You can also consult the MSDN library online : http://msdn.microsoft.com/library/default.asp
To search the MSDN site goto MSDN home page http://msdn.microsoft.com and search for the API description, f.i searchinf for
AddPrinter returns:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/prntspol_1po2.asp


||
|| Thank You for All Your Help,
|| Ken
|| P.S. If microsoft wants people to buy into C#, then they
|| need to have better documentation!!!!!!!!


**** The documentation is available, as I said what you are trying is to call a native Win32 API, this has nothing to do with C#.


||
|| The IntPtr = 1527664;
|| next time =1520616;
|| next time = 1528184;
||

**** You should call the Win32 "ClosePrinter API using this IntPtr (Handle) as argument.



Willy.
 
J

Jeffrey Tan[MSFT]

Hi Ken,

Yes, just as Gunter said you can add a printer via WSH.
Maybe you can get some information in the below links:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/ht
ml/wsmthaddwindowsprinterconnection.asp
and
http://www.aspemporium.com/aspEmporium/forum/display_message.asp?mid=620&fid
=1

Hope it 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.

--------------------
| From: "Günter Prossliner" <[email protected]>
| References: <[email protected]>
| Subject: Re: Windows API --AddPrinter Does not seem to work
| Date: Mon, 4 Aug 2003 18:16:15 +0200
| Lines: 109
| 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: <eJ#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 80.240.224.178
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:174024
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| You could create a tempoary .vbs script that adds the printer via
| AddPrinterConnection(), and execute it via Process.Start
|
| | > 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 wrong. Do I have to (declare/or declare and
| > populate) a DevMode structure? Do I have to also create a
| printerconnection
| > using the printer pointer? I have found very little on the net or
| microsoft
| > about what is required. Some of what is below was sent to me through
this
| > news group, but there are some inconsistancies. I found a scrap of code
| that
| > actual has an entry point for addprinter "AddPrinterA" , should I use
| that?
| > It amazes me that microsoft has spent so little time on knowledge base
and
| > support issues about this subject.
| > Any help and direction on this problem would be greatly appreciated. I
| have
| > found myself going over the same ground and no change in the results.
| >
| > Thank You in Advance for any 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;
| > }
| >
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DocumentProperties API 0

Top