473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8321
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.