473,387 Members | 1,834 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,387 software developers and data experts.

SetPrinter for network printers

I have been trying to change printer settings thru SetPrinter API and it
works successfully for local printers, but with network printers i don't get
neither an error message or anything else.

Here is my code.

'Class DevMode

' Used to get data from PrintDialog thru a pointer using Marshal .NET
functionality.

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _

Friend Class DevMode

<VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)>
Public dmDeviceName As String

Public dmSpecVersion As Short

Public dmDriverVersion As Short

Public dmSize As Short

Public dmDriverExtra As Short

Public dmFields As Integer

Public dmOrientation As Short

Public dmPaperSize As Short

Public dmPaperLength As Short

Public dmPaperWidth As Short

Public dmScale As Short

Public dmCopies As Short

Public dmDefaultSource As Short

Public dmPrintQuality As Short

Public dmColor As Short

Public dmDuplex As Short

Public dmYResolution As Short

Public dmTTOption As Short

Public dmCollate As Short

<VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)>
Public dmFormName As String

Public dmUnusedPadding As Short

Public dmBitsPerPel As Short

Public dmPelsWidth As Integer

Public dmPelsHeight As Integer

Public dmDisplayFlags As Integer

Public dmDisplayFrequency As Integer

End Class

<Serializable(), StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
_

Friend Structure PRINTER_INFO_2

Public pServerName As String

Public pPrinterName As String

Public pShareName As String

Public pPortName As String

Public pDriverName As String

Public pComment As String

Public pLocation As String

Public pDevMode As IntPtr

Public pSepFile As String

Public pPrintProcessor As String

Public pDatatype As String

Public pParameters As String

Public pSecurityDescriptor As IntPtr

Public Attributes As System.UInt32

Public Priority As System.UInt32

Public DefaultPriority As System.UInt32

Public StartTime As System.UInt32

Public UntilTime As System.UInt32

Public Status As System.UInt32

Public cJobs As System.UInt32

Public AveragePPM As System.UInt32

End Structure

<StructLayout(LayoutKind.Sequential)> _

Private Structure PRINTER_DEFAULTS

Public pDatatype As IntPtr

Public pDevMode As IntPtr

Public DesiredAccess As Integer

End Structure

Private Enum PrinterAccessRights

PRINTER_ACCESS_ADMINISTER = &H4

PRINTER_ACCESS_USE = &H8

PRINTER_ALL_ACCESS = &HF000C

End Enum

'* DEVMODE collation selections

Private Const DMCOLLATE_FALSE As Short = 0

Private Const DMCOLLATE_TRUE As Short = 1

<DllImport("winspool.drv", EntryPoint:="OpenPrinterW", _

CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Private Shared Function OpenPrinter( _

ByVal pPrinterName As String, ByRef hPrinter As IntPtr, _

ByVal pDefault As IntPtr) As Boolean

End Function

<DllImport("winspool.drv", EntryPoint:="OpenPrinterW", _

CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Private Shared Function OpenPrinter( _

ByVal pPrinterName As String, ByRef hPrinter As IntPtr, _

ByRef pDefault As PRINTER_DEFAULTS) As Boolean

End Function

<DllImport("winspool.drv", CharSet:=CharSet.Auto, _

SetLastError:=True, ExactSpelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Private Shared Function ClosePrinter( _

ByVal hPrinter As IntPtr) As Boolean

End Function

<DllImport("winspool.drv", EntryPoint:="GetPrinterW", _

CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Private Shared Function GetPrinter( _

ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _

ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, _

ByRef pcbNeeded As Integer) As Boolean

End Function

<DllImport("winspool.drv", entrypoint:="SetPrinterW", _

CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True, _

CallingConvention:=CallingConvention.StdCall)> _

Private Shared Function SetPrinter( _

ByVal hPrinter As IntPtr, ByVal dwLevel As Integer, _

ByVal pPrinter As IntPtr, ByVal Command As Integer) As Boolean

End Function

Public Sub ChangeSettings()

Dim hPrinter As IntPtr

Dim pPrinterInfo As IntPtr

Dim PrinterInfo As PRINTER_INFO_2

Dim PrinterName As String

Dim MyPrintDialog As New PrintDialog

If MyPrintDialog.ShowDialog() = DialogResult.OK Then

PrinterName = MyPrintDialog.PrinterSettings.PrinterName

hPrinter = OpenPrinter(PrinterName)

pPrinterInfo = IntPtr.Zero

Try

Dim needed As Integer

GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed)

If needed <= 0 Then

Throw New Exception("It failed getting printer settings.")

End If

pPrinterInfo = Marshal.AllocHGlobal(needed)

Dim temp As Integer

If Not GetPrinter(hPrinter, 2, pPrinterInfo, needed, temp) Then

Throw New Win32Exception(Marshal.GetLastWin32Error())

End If

'* Marshalling it does in type

PrinterInfo = CType(Marshal.PtrToStructure(pPrinterInfo,
GetType(PRINTER_INFO_2)), PRINTER_INFO_2)

Dim Copies As Short = CType(New Random(Minute(Now) + Second(Now)).Next(1,
9999), Short)

Dim m_dvmode As New DevMode

m_dvmode = CType(Marshal.PtrToStructure(PrinterInfo.pDevMode,
GetType(DevMode)), DevMode)

With m_dvmode

..dmCopies = Copies

..dmCollate = CType(IIf(Copies Mod 2 = 0, DMCOLLATE_TRUE, DMCOLLATE_FALSE),
Short)

..dmOrientation = CType(IIf(Copies Mod 2 = 0, EOrientation.eoLandscape,
EOrientation.eoPortrait), Short)

End With

Marshal.StructureToPtr(m_dvmode, PrinterInfo.pDevMode, True)

If Not SetPrinter(hPrinter, 2, pPrinterInfo, 0) Then

Throw New Win32Exception(Marshal.GetLastWin32Error())

End If

Finally

Marshal.FreeHGlobal(pPrinterInfo)

End Try

'''Serialization.SaveObjectToFile("c:\temp\test.be fore.txt", pinfo)

ClosePrinter(hPrinter)

End If

End Sub

Private Function OpenPrinter(ByVal PrinterName As String) As IntPtr

Dim hPrinter As IntPtr

Dim pd As New PRINTER_DEFAULTS

Dim pPrinterDefaults As IntPtr = IntPtr.Zero

With pd

..DesiredAccess = PrinterAccessRights.PRINTER_ALL_ACCESS

..pDevMode = GetPrinterInfo(PrinterName).pDevMode

..pDatatype = IntPtr.Zero

End With

'Marshal.StructureToPtr(pd, pPrinterDefaults, True)

If Not OpenPrinter(PrinterName, hPrinter, pd) Then 'IntPtr.Zero) Then

Throw New Win32Exception(Marshal.GetLastWin32Error())

End If

Return hPrinter

End Function

Private Function GetPrinterInfo(ByVal PrinterName As String) As
PRINTER_INFO_2

Dim hPrinter As IntPtr

If Not OpenPrinter(PrinterName, hPrinter, IntPtr.Zero) Then

Throw New Win32Exception(Marshal.GetLastWin32Error())

End If

Dim pPrinterInfo As IntPtr = IntPtr.Zero

Try

Dim needed As Integer

GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed)

If needed <= 0 Then

Throw New Exception("It failed getting printer settings.")

End If

pPrinterInfo = Marshal.AllocHGlobal(needed)

Dim temp As Integer

If Not GetPrinter(hPrinter, 2, pPrinterInfo, needed, temp) Then

Throw New Win32Exception(Marshal.GetLastWin32Error())

End If

'* Marshalling it does in type

Dim printerInfo As PRINTER_INFO_2 = _

CType(Marshal.PtrToStructure( _

pPrinterInfo, GetType(PRINTER_INFO_2)), PRINTER_INFO_2)

Return printerInfo

Finally

ClosePrinter(hPrinter)

Marshal.FreeHGlobal(pPrinterInfo)

End Try

End Function
Nov 21 '05 #1
0 1638

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

Similar topics

6
by: Stephan Perschke | last post by:
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...
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: Mike Ripplinger | last post by:
Hi there. I'm trying to write a program that purges the print queue for a selected printer. I was hoping to do this by simply calling the external prnqctl.vbs script, but this program has to run...
1
by: Maileen | last post by:
Hi, We have a little problem in 1 of our ASP page :( on this page, we can add new printers (local or network) and to print a report on a selected (by default) printer. We we do the test on a...
0
by: Bhaskar | last post by:
Hi all, Problem: OS : Win XP Platform: VB.NET(Web Application) My application enumerates all the printers in the network. Currently there are only 2 network printers here. 1.EPSON TM...
0
by: prakashkoshti | last post by:
I have an applicaiton developed in ASP.Net. I have installed network printers on my machine. (Installed printers include both USB and COM) When I try to print from other interactive applications...
2
by: Tessa | last post by:
Hi, We have a .net web application, and are trying to use PrinterSettings.InstalledPrinters to list the printers installed on the webserver. (Windows 2003 server R2, IIS 6, .net framework 2.0.)...
5
by: lmttag | last post by:
ASP.NET 2.0 (C#) application Intranet application (not on the Internet) Using Windows authentication and impersonation Windows Server 2003 (IIS6) Server is a member server on a domain Logged...
2
by: calan_svc | last post by:
I have a dll I wrote in VB6. Basically, it can list all the printers it finds as well as print to a selected printer. I created a test app in VB6, added the dll as a reference, and added the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
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...

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.