> Anyway, it would be more helpful if we knew exactly what you were
trying to accomplish. It's pretty obvious that your trying to call a
Win32 api of some sort - but knowing which one would be helpful.
Working with API's is, IMHO, significantly different in VB.NET then
VB.CLASSIC.
--
Tom Shelton [MVP]
Thanks
I'm trying to use an old routine for setting the system default printer's
Landscape mode.
Any other suggestion?
Thanks again
Public Shared Sub SetPrinterSettings(ByRef windowHandle As IntPtr, ByRef
printerName As String, ByRef isLandscape As Boolean) ', ByRef aNewPaperSize
As Short, ByRef aNewDuplex As Short)
Dim lPrinterHandle As IntPtr
Dim lDevMode As GDI.DEVMODE
Dim lDevModeAsByte() As Byte
Dim lDevModeAsByteSize As Integer
Dim lPrtInfo2 As WinSpool.PRINTER_INFO_2
Dim lPrtInfo2AsByte() As Byte
Dim lPrtInfo2AsByteSize As Integer
Dim lFlag As Integer
Dim lPrinterDefaults As WinSpool.PRINTER_DEFAULTS
Dim lErrorCode As Integer
'You should probably look at the PtrToStructure and StructureToPtr methods
rather than Copy.
Kernel.ZeroMemory(lPrinterDefaults, Marshal.SizeOf(lPrinterDefaults))
lPrinterDefaults.DesiredAccess = WinSpool.PRINTER_ALL_ACCESS
'lPrinterDefaults.DesiredAccess = WinSpool.PRINTER_ACCESS_ADMINISTER
'Get a handle to the printer.
lFlag = WinSpool.OpenPrinter(printerName, lPrinterHandle, lPrinterDefaults)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
If lErrorCode = User.ERROR_ACCESS_DENIED Then
MessageBox.Show("Requires adminstrative rights to change default printer",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Exit Sub
End If
'This GetPrinter call determines the size needed for lPrtInfo2AsByte
lFlag = WinSpool.GetPrinter(lPrinterHandle, 2, 0, 0, lPrtInfo2AsByteSize)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
If lErrorCode <> 122 Then GoTo CLEAN_UP
End If
'Allocate space for lPrtInfo2AsByte
ReDim lPrtInfo2AsByte(lPrtInfo2AsByteSize - 1)
'The second GetPrinter call fills lPrtInfo2AsByte with the current settings
lFlag = WinSpool.GetPrinter(lPrinterHandle, 2, lPrtInfo2AsByte(0),
lPrtInfo2AsByteSize, lPrtInfo2AsByteSize)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
'Console.WriteLine("GetPrinter exited with code : {0}", lErrorCode)
End If
'Copy lPrtInfo2AsByte to the structure
Kernel.CopyMemory2(lPrtInfo2, lPrtInfo2AsByte(0), Marshal.SizeOf(lPrtInfo2))
'Get the total size of the DeviceMode bytes (lDevModeAsByte)
lDevModeAsByteSize = WinSpool.DocumentProperties(windowHandle,
lPrinterHandle, printerName, 0, 0, 0)
'Reserve memory for the total size of the DeviceMode bytes
ReDim lDevModeAsByte(lDevModeAsByteSize - 1)
'Fill the DeviceMode bytes from the printer.
lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lDevModeAsByte(0), 0, GDI.DM_OUT_BUFFER)
If lFlag < 0 Then
lErrorCode = Marshal.GetLastWin32Error()
Console.WriteLine("DocumentProperties exited with code: {0}", lErrorCode)
End If
'Copy the Public shared (predefined) portion of the DeviceMode bytes to the
structure
Kernel.CopyMemory3(lDevMode, lDevModeAsByte(0), Marshal.SizeOf(lDevMode))
'Set the dmFields bit flag to indicate what we are changing
lDevMode.dmFields = GDI.DM_ORIENTATION 'Or DM_DUPLEX Or DM_PAPERSIZE
If isLandscape Then
lDevMode.u.dmOrientation = GDI.DMORIENT_LANDSCAPE
Else
lDevMode.u.dmOrientation = GDI.DMORIENT_PORTRAIT
End If
'Set/Change PaperSize
'lDevMode.dmPaperSize = aNewPaperSize
'On Error Resume Next
'lDevMode.dmDuplex = aNewDuplex
'On Error GoTo 0
'Copy changed structure back to the bytes
Kernel.CopyMemory4(lDevModeAsByte(0), lDevMode, Marshal.SizeOf(lDevMode))
'Merge the printer driver's current print settings with the settings in the
lDevModeAsByte
lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lDevModeAsByte(0), lDevModeAsByte(0), GDI.DM_IN_BUFFER Or
GDI.DM_OUT_BUFFER)
' lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lDevModeAsByte(0), lDevModeAsByte(0), GDI.DM_IN_PROMPT)
'Copy address of lDevModeAsByte into lPrtInfo2 structure
' Kernel.CopyMemory1(lPrtInfo2.pDevMode, lDevModeAsByte(0),
Marshal.SizeOf(lDevMode))
Kernel.CopyMemory1(lPrtInfo2.pDevMode, lDevModeAsByte(0), 4)
'Update lPrinterInfoAsByte with the updated structure
Kernel.CopyMemory5(lPrtInfo2AsByte(0), lPrtInfo2, Marshal.SizeOf(lDevMode))
lFlag = WinSpool.SetPrinter(lPrinterHandle, 2, lPrtInfo2AsByte(0), 0)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
Console.WriteLine("SetPrinter exited with code : {0}", lErrorCode)
End If
CLEAN_UP:
WinSpool.ClosePrinter(lPrinterHandle) 'Close the handle
End Sub