S
Stephan Perschke
Hi,
I try to change some printer settings using the win32 api OpenPrinter,
GetPrinter and SetPrinter. I copied most of the code from this or
related newsgroups. However, OpenPrinter, GetPrinter and ClosePrinter
works so far but I cannot change the printer settings. SetPrinter
returns the error code 5 ACCESS DENIED. I know I have to deal with the
desired access mask in the _DEFAULT_PRINTER structure when calling
OpenPrinter. Unfortunaltely the declaration of the DEFAULT_PRINTER
structure or class I found on the internet doesn't work with the
OpenPrinter function.
Here is the code (Warning: don't proceed with reading if you find bad code
offending! I'm not a programer and I'm new to C#)
public class Printer
{
[DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern bool OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, PRINTER_DEFAULTS pDefaults);
[DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern bool GetPrinterA(IntPtr hPrinter, int dwLevel,
IntPtr pPrinter, int cbBuf, ref int pcbNeeded);
[DllImport("WinSpool.drv",SetLastError=true)]
public static extern bool SetPrinter (IntPtr hPrinter, int dwLevel,
ref PRINTER_INFO_8 pPrinter, uint Command);
public struct PRINTER_DEFAULTS
{
public string pDataType;
public DEVMODE pDevMode;
public ACCESS_MASK DesiredAccess;
}
public struct PRINTER_DEFAULTS
{
public string pDataType;
public DEVMODE pDevMode;
public ACCESS_MASK DesiredAccess;
}
[Flags] public enum ACCESS_MASK
{
PRINTER_ACCESS_ADMINISTER = 4,
PRINTER_ACCESS_USE = 8,
PRINTER_ALL_ACCESS = 983052
}
}
class MyClass
{
[STAThread]
static void Main(string[] args)
{
int dwLevel = 8;
bool retVal = false;
printer.PRINTER_INFO_8 printerInfo = new printer.PRINTER_INFO_8();
System.IntPtr lhPrinter= new System.IntPtr();
printer.PRINTER_DEFAULTS pd = new printer.PRINTER_DEFAULTS();
pd.pDataType = "";
pd.pDevMode = new DEVMODE();
pd.DesiredAccess = printer.ACCESS_MASK.PRINTER_ALL_ACCESS;
retVal = printer.OpenPrinter("Printer", ref lhPrinter, pd);
.....
I appreciate your help!
- Stephan
I try to change some printer settings using the win32 api OpenPrinter,
GetPrinter and SetPrinter. I copied most of the code from this or
related newsgroups. However, OpenPrinter, GetPrinter and ClosePrinter
works so far but I cannot change the printer settings. SetPrinter
returns the error code 5 ACCESS DENIED. I know I have to deal with the
desired access mask in the _DEFAULT_PRINTER structure when calling
OpenPrinter. Unfortunaltely the declaration of the DEFAULT_PRINTER
structure or class I found on the internet doesn't work with the
OpenPrinter function.
Here is the code (Warning: don't proceed with reading if you find bad code
offending! I'm not a programer and I'm new to C#)
public class Printer
{
[DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern bool OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, PRINTER_DEFAULTS pDefaults);
[DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
public static extern bool GetPrinterA(IntPtr hPrinter, int dwLevel,
IntPtr pPrinter, int cbBuf, ref int pcbNeeded);
[DllImport("WinSpool.drv",SetLastError=true)]
public static extern bool SetPrinter (IntPtr hPrinter, int dwLevel,
ref PRINTER_INFO_8 pPrinter, uint Command);
public struct PRINTER_DEFAULTS
{
public string pDataType;
public DEVMODE pDevMode;
public ACCESS_MASK DesiredAccess;
}
public struct PRINTER_DEFAULTS
{
public string pDataType;
public DEVMODE pDevMode;
public ACCESS_MASK DesiredAccess;
}
[Flags] public enum ACCESS_MASK
{
PRINTER_ACCESS_ADMINISTER = 4,
PRINTER_ACCESS_USE = 8,
PRINTER_ALL_ACCESS = 983052
}
}
class MyClass
{
[STAThread]
static void Main(string[] args)
{
int dwLevel = 8;
bool retVal = false;
printer.PRINTER_INFO_8 printerInfo = new printer.PRINTER_INFO_8();
System.IntPtr lhPrinter= new System.IntPtr();
printer.PRINTER_DEFAULTS pd = new printer.PRINTER_DEFAULTS();
pd.pDataType = "";
pd.pDevMode = new DEVMODE();
pd.DesiredAccess = printer.ACCESS_MASK.PRINTER_ALL_ACCESS;
retVal = printer.OpenPrinter("Printer", ref lhPrinter, pd);
.....
I appreciate your help!
- Stephan