Re: How to copy an address into a pointer
- From: " Just Me" <groups@xxxxxxxxxx>
- Date: Sat, 4 Jun 2005 20:26:56 -0400
Thanks
I took your eariler advise and got rid of the RtlMoveMemory's
I'll keep plugging and learning as I go along.
Thanks
BTW this is where I am now:
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 lFlag As Integer
Dim lPrinterDefaults As WinSpool.PRINTER_DEFAULTS
Dim lErrorCode As Integer
Dim lDevMode As GDI.DEVMODE
Dim lDevModeSize As Integer
Dim lPointerToDevMode As IntPtr
Dim lPrtInfo2 As WinSpool.PRINTER_INFO_2 = New WinSpool.PRINTER_INFO_2
Dim lPrtInfo2Size As Integer
Dim lPointerToPrinterInfo2 As IntPtr
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 lPointerToPrinterInfo2
lFlag = WinSpool.GetPrinter(lPrinterHandle, 2, IntPtr.Zero, 0,
lPrtInfo2Size)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
If lErrorCode <> 122 Then GoTo CLEAN_UP
End If
'Allocate space for lPointerToPrinterInfo2 data
lPointerToPrinterInfo2 = Marshal.AllocCoTaskMem(lPrtInfo2Size)
'The second GetPrinter call fills lPointerToPrinterInfo2 data with the
current settings
lFlag = WinSpool.GetPrinter(lPrinterHandle, 2, lPointerToPrinterInfo2,
lPrtInfo2Size, lPrtInfo2Size)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
'Console.WriteLine("GetPrinter exited with code : {0}", lErrorCode)
End If
'Copy lPointerToPrinterInfo2 data to the structure
lPrtInfo2 = Marshal.PtrToStructure(lPointerToPrinterInfo2,
lPrtInfo2.GetType)
'Get the total size of the DeviceMode
lDevModeSize = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, 0, 0, 0)
'Reserve lPointerToDevMode data memory for the total size of the DeviceMode
lPointerToDevMode = Marshal.AllocCoTaskMem(lDevModeSize)
'Fill the lPointerToDevMode data from the printer driver
lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lPointerToDevMode, IntPtr.Zero, GDI.DM_OUT_BUFFER)
If lFlag < 0 Then
lErrorCode = Marshal.GetLastWin32Error()
Console.WriteLine("DocumentProperties exited with code: {0}", lErrorCode)
End If
'Copy the lPointerToDevMode data to the structure
lDevMode = Marshal.PtrToStructure(lPointerToDevMode, lDevMode.GetType)
'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.dmOrientation = GDI.DMORIENT_LANDSCAPE
Else
lDevMode.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 lPointerToDevMode data
Marshal.StructureToPtr(lDevMode, lPointerToDevMode, True)
'Merge the printer driver's current print settings with the settings in the
lPointerToDevMode data
' lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lPointerToDevMode, lPointerToDevMode, GDI.DM_IN_BUFFER Or
GDI.DM_OUT_BUFFER)
lFlag = WinSpool.DocumentProperties(windowHandle, lPrinterHandle,
printerName, lPointerToDevMode, lPointerToDevMode, GDI.DM_IN_BUFFER Or
GDI.DM_OUT_BUFFER Or GDI.DM_IN_PROMPT)
lDevMode = Marshal.PtrToStructure(lPointerToDevMode, lDevMode.GetType)
'Copy address of lPointerToDevMode into lPrtInfo2 structure
'Dim objHandle As GCHandle = GCHandle.Alloc(lPointerToDevMode,
GCHandleType.Pinned)
Dim objHandle As GCHandle = GCHandle.Alloc(lDevMode, GCHandleType.Pinned)
Dim objectAddress As IntPtr = objHandle.AddrOfPinnedObject()
lPrtInfo2.pDevMode = objectAddress
lFlag = WinSpool.SetPrinter(lPrinterHandle, 2, lPointerToPrinterInfo2, 0)
If lFlag = 0 Then
lErrorCode = Marshal.GetLastWin32Error()
Console.WriteLine("SetPrinter exited with code : {0}", lErrorCode)
If lErrorCode = 5 Then
MessageBox.Show("Access denied", "Unable To Set Printer",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
lFlag = WinSpool.GetPrinter(lPrinterHandle, 2, lPointerToPrinterInfo2,
lPrtInfo2Size, lPrtInfo2Size)
Marshal.StructureToPtr(lDevMode, lPointerToDevMode, True)
CLEAN_UP:
objHandle.Free()
If (lPrinterHandle.ToInt32 <> 0) Then WinSpool.ClosePrinter(lPrinterHandle)
End Sub
.
- References:
- How to copy an address into a pointer
- From: Just Me
- Re: How to copy an address into a pointer
- From: Tom Shelton
- Re: How to copy an address into a pointer
- From: Just Me
- Re: How to copy an address into a pointer
- From: Tom Shelton
- Re: How to copy an address into a pointer
- From: Just Me
- Re: How to copy an address into a pointer
- From: Tom Shelton
- How to copy an address into a pointer
- Prev by Date: Re: Question about VB.Net connecting to a webserver
- Next by Date: RE: Listbox SelectedItem & Dragging
- Previous by thread: Re: How to copy an address into a pointer
- Next by thread: Re: How to copy an address into a pointer
- Index(es):