473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using PRINTER_DEFAULTS with OpenPrinter fails

Any idea why this fails with an
OpenPrinter exited with code : 5
Message is: Access is denied.

I have Adminstrative rights

Public Structure PRINTER_DEFAULTS
Dim pDatatype As Integer 'String I"VE TRIED BOTH
Dim pDevMode As Integer
Dim DesiredAccess As Integer
End Structure
....snip

PrinterDefaults.pDatatype = 0
PrinterDefaults.pDevMode = 0
PrinterDefaults.DesiredAccess = PRINTER_ALL_ACCESS
ALSO TRIED PRINTER_ACCESS_ADMINISTER
Flag = OpenPrinter(PrinterName, PrinterHandle, PrinterDefaults)

If Flag = 0 Then
ret = Marshal.GetLastWin32Error()
Console.WriteLine("OpenPrinter exited with code : {0}", ret)
Console.WriteLine("Message is: {0}", UtilityCs.GetErrorMessage(ret))
If ret = ERROR_ACCESS_DENIED Then
MsgBox("Requires Adminstrative rights to change default
printer", _
MsgBoxStyle.Exclamation Or MsgBoxStyle.OKOnly)
End If
Exit Sub
End If
Nov 20 '05 #1
1 8306
If you're running win2K and above, try the SetDefaultPrinter function in
winspool.drv:

<DllImport("winspool.drv")> _
Private Shared Function SetDefaultPrinter(ByVal printerName As String) As
Boolean
End Function

Public Shared Sub SetPrinterAsDefault(ByVal PrinterName As String)
If Not SetDefaultPrinter(PrinterName) Then
Throw New Exception("Unable to set printer")
End If
End Sub

Note that the printer name is the printer name, driver, and port, like
what is returned from GetDefaultPrinter or EnumPrinters.

" active" <ac****@REMOVEa-znet.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Any idea why this fails with an
OpenPrinter exited with code : 5
Message is: Access is denied.

I have Adminstrative rights

Public Structure PRINTER_DEFAULTS
Dim pDatatype As Integer 'String I"VE TRIED BOTH
Dim pDevMode As Integer
Dim DesiredAccess As Integer
End Structure
...snip

PrinterDefaults.pDatatype = 0
PrinterDefaults.pDevMode = 0
PrinterDefaults.DesiredAccess = PRINTER_ALL_ACCESS
ALSO TRIED PRINTER_ACCESS_ADMINISTER
Flag = OpenPrinter(PrinterName, PrinterHandle, PrinterDefaults)

If Flag = 0 Then
ret = Marshal.GetLastWin32Error()
Console.WriteLine("OpenPrinter exited with code : {0}", ret)
Console.WriteLine("Message is: {0}", UtilityCs.GetErrorMessage(ret))
If ret = ERROR_ACCESS_DENIED Then
MsgBox("Requires Adminstrative rights to change default
printer", _
MsgBoxStyle.Exclamation Or MsgBoxStyle.OKOnly)
End If
Exit Sub
End If

Nov 20 '05 #2

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

Similar topics

4
by: Umesh | last post by:
Hi all I am trying to change the printer(like paper source ,etc) setting in C#. But i am unable to do it. I am not sure how to do it. should i have to use WIN API like Openprinter , getprinter ,...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
2
by: ken | last post by:
Hello Everyone, I am trying to find out how to add a printer to a windows 2000 advance server by using C# code. I cannot believe how bad the MSDN subscription is when it comes to searching for...
10
by: Joe M | last post by:
I was wondering if someone could lend me a hand with a C# problem I am having I am trying to use the “setPrinter” api to change the duplex setting (under printing preferences on printer...
0
by: active | last post by:
Any idea why this fails with an OpenPrinter exited with code : 5 Message is: Access is denied. I have Adminstrative rights Public Structure PRINTER_DEFAULTS Dim pDatatype As Integer 'String ...
13
by: Susan Beebe | last post by:
I have downloaded the code described in Microsoft article Q154078. I am trying to send raw ZPL (zebra barcode printer) code from Microsoft access. It works just fine if I hard code the actual...
1
by: sathyp | last post by:
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _ ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean Dim Prn As Printer Dim hPrinter As Long Dim pd As...
2
by: Raja | last post by:
Hi, Attached is the code . I want my program to save the current printer job properties and , when I reconnect the printer at a latter date , i need to print the saved job . Can you please help...
2
by: Flying Kite | last post by:
Hi All, I want to know how to print chinese characters on Zebra Printer, following code working fine with English string, but it's not working for Chinese string. It shows ASCII characters instead...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.