none
Error to read DevMode data with VB.NET 2005 RRS feed

  • Question

  • I try to read DevMode with GethDevMode method but data is wrong !!!

    I found declare DevMode structure  on MSDN, but it don't work !!!

     

    When I read DevMode Structure I get :

    dmDeviceName = "@Ç ¨"
    dmSpecVersion = 0
    dmDriverVersion = 0
    dmSize =  200
    dmDriverExtra =  562

     

    When I change the structure to declare dmDeviceName as LPWStr I get :

    dmDeviceName = "Canon iR2230"
    dmSpecVersion = 3
    dmDriverVersion = 0
    dmSize = -12216
    dmDriverExtra = 1680

     

    dmDeviceName is right, but other data is wrong !!!

     

    Where is the problem ???

    Thanks for your help.

     

     

    Code Snippet

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

    Friend Structure DEVMODE

    <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 dmPositionX As Integer

    Public dmPositionY As Integer

    Public dmDisplayOrientation As Integer

    Public dmDisplayFixedOutput As Integer

    Public dmColor As Short

    Public dmDuplex As Short

    Public dmYResolution As Short

    Public dmTTOption As Short

    Public dmCollate As Short

    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _

    Public dmFormName As String

    Public dmLogPixels As Short

    Public dmBitsPerPel As Short

    Public dmPelsWidth As Integer

    Public dmPelsHeight As Integer

    Public dmDisplayFlags As Integer

    Public dmDisplayFrequency As Integer

    Public dmICMMethod As Integer

    Public dmICMIntent As Integer

    Public dmMediaType As Integer

    Public dmDitherType As Integer

    Public dmReserved1 As Integer

    Public dmReserved2 As Integer

    Public dmPanningWidth As Integer

    Public dmPanningHeight As Integer

    End Structure

     

    Private Function Test_ReadDevMode()

    Dim lo_DevMode As New DEVMODE

    Dim li_hDevMode As IntPtr

     

    If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    li_hDevMode = Me.PrintDialog1.PrinterSettings.GetHdevmode()

    'Marshal.AllocHGlobal(li_hDevMode)

    lo_DevMode = Marshal.PtrToStructure(li_hDevMode, GetType(DEVMODE))

    'Marshal.FreeHGlobal(li_hDevMode)

    Console.WriteLine(lo_DevMode.dmDeviceName)

    End If

    End Function

     

     

    Thursday, August 2, 2007 2:54 PM

Answers

  •  

    Aha, sneaky, this is their declaration. And it's a pointer to a pointer...

     

    Code Snippet
    Imports System.Runtime.InteropServices

    Public Class Form1

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
        
    Public Class DEVMODE
            <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=&H20)> _
            
    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
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H20)> _
            
    Public dmFormName As String
            Public dmLogPixels As Short
            Public dmBitsPerPel As Integer
            Public dmPelsWidth As Integer
            Public dmPelsHeight As Integer
            Public dmDisplayFlags As Integer
            Public dmDisplayFrequency As Integer
            Public dmICMMethod As Integer
            Public dmICMIntent As Integer
            Public dmMediaType As Integer
            Public dmDitherType As Integer
            Public dmICCManufacturer As Integer
            Public dmICCModel As Integer
            Public dmPanningWidth As Integer
            Public dmPanningHeight As Integer
            Public Overrides Function ToString() As String
                Return String.Concat(New Object() {"[DEVMODE: dmDeviceName=", Me.dmDeviceName, ", dmSpecVersion=", Me.dmSpecVersion, ", dmDriverVersion=", Me.dmDriverVersion, ", dmSize=", Me.dmSize, ", dmDriverExtra=", Me.dmDriverExtra, ", dmFields=", Me.dmFields, ", dmOrientation=", Me.dmOrientation, ", dmPaperSize=", Me.dmPaperSize, ", dmPaperLength=", Me.dmPaperLength, ", dmPaperWidth=", Me.dmPaperWidth, ", dmScale=", Me.dmScale, ", dmCopies=", Me.dmCopies, ", dmDefaultSource=", Me.dmDefaultSource, ", dmPrintQuality=", Me.dmPrintQuality, ", dmColor=", Me.dmColor, ", dmDuplex=", Me.dmDuplex, ", dmYResolution=", Me.dmYResolution, ", dmTTOption=", Me.dmTTOption, ", dmCollate=", Me.dmCollate, ", dmFormName=", Me.dmFormName, ", dmLogPixels=", Me.dmLogPixels, ", dmBitsPerPel=", Me.dmBitsPerPel, ", dmPelsWidth=", Me.dmPelsWidth, ", dmPelsHeight=", Me.dmPelsHeight, ", dmDisplayFlags=", Me.dmDisplayFlags, ", dmDisplayFrequency=", Me.dmDisplayFrequency, ", dmICMMethod=", Me.dmICMMethod, ", dmICMIntent=", Me.dmICMIntent, ", dmMediaType=", Me.dmMediaType, ", dmDitherType=", Me.dmDitherType, ", dmICCManufacturer=", Me.dmICCManufacturer, ", dmICCModel=", Me.dmICCModel, ", dmPanningWidth=", Me.dmPanningWidth, ", dmPanningHeight=", Me.dmPanningHeight, "]"})
            
    End Function
        End Class

        Private Sub Test_ReadDevMode()

            
    Dim lo_DevMode As New DEVMODE
            
    Dim li_hDevMode As IntPtr

            
    If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                li_hDevMode = Me.PrintDialog1.PrinterSettings.GetHdevmode()
                
    ' li_hDevMode points to a pointer:
                Dim ptr As IntPtr = Marshal.ReadIntPtr(li_hDevMode)
                
    ' which points to the structure...
                lo_DevMode = Marshal.PtrToStructure(ptr, GetType(DEVMODE))
                Console.WriteLine(lo_DevMode.ToString)
            
    End If
            Me.Text = Marshal.SizeOf(GetType(DEVMODE))
        
    End Sub

        Sub New()
            InitializeComponent()
            Test_ReadDevMode()
        
    End Sub

    End
    Class

     

     

    Friday, August 3, 2007 12:57 AM

All replies

  • There's a declaration here:

    http://forums.microsoft.com/msdn/showpost.aspx?pageindex=2&siteid=1&postid=1921454&sb=0&d=1&at=7&ft=11&tf=0&pageid=0

     

    *scratches head*

    Hmm, they look the same...

     

    Thursday, August 2, 2007 11:07 PM
  • The thing at that intPtr isn't the same as the DEVMODE structure declared in the msdn library. The strucuture should have a wide char array as the first field, but instead it has a pointer to a wide char array. This throws the offsets for all the other fields off. Are you sure that those values you get when you have the junk name are correct? If I set the number of pages, or collate then it doesn't appear in the structure.

     

    (edit: ignore this nonsense.)

    Friday, August 3, 2007 12:47 AM
  •  

    Aha, sneaky, this is their declaration. And it's a pointer to a pointer...

     

    Code Snippet
    Imports System.Runtime.InteropServices

    Public Class Form1

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
        
    Public Class DEVMODE
            <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=&H20)> _
            
    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
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H20)> _
            
    Public dmFormName As String
            Public dmLogPixels As Short
            Public dmBitsPerPel As Integer
            Public dmPelsWidth As Integer
            Public dmPelsHeight As Integer
            Public dmDisplayFlags As Integer
            Public dmDisplayFrequency As Integer
            Public dmICMMethod As Integer
            Public dmICMIntent As Integer
            Public dmMediaType As Integer
            Public dmDitherType As Integer
            Public dmICCManufacturer As Integer
            Public dmICCModel As Integer
            Public dmPanningWidth As Integer
            Public dmPanningHeight As Integer
            Public Overrides Function ToString() As String
                Return String.Concat(New Object() {"[DEVMODE: dmDeviceName=", Me.dmDeviceName, ", dmSpecVersion=", Me.dmSpecVersion, ", dmDriverVersion=", Me.dmDriverVersion, ", dmSize=", Me.dmSize, ", dmDriverExtra=", Me.dmDriverExtra, ", dmFields=", Me.dmFields, ", dmOrientation=", Me.dmOrientation, ", dmPaperSize=", Me.dmPaperSize, ", dmPaperLength=", Me.dmPaperLength, ", dmPaperWidth=", Me.dmPaperWidth, ", dmScale=", Me.dmScale, ", dmCopies=", Me.dmCopies, ", dmDefaultSource=", Me.dmDefaultSource, ", dmPrintQuality=", Me.dmPrintQuality, ", dmColor=", Me.dmColor, ", dmDuplex=", Me.dmDuplex, ", dmYResolution=", Me.dmYResolution, ", dmTTOption=", Me.dmTTOption, ", dmCollate=", Me.dmCollate, ", dmFormName=", Me.dmFormName, ", dmLogPixels=", Me.dmLogPixels, ", dmBitsPerPel=", Me.dmBitsPerPel, ", dmPelsWidth=", Me.dmPelsWidth, ", dmPelsHeight=", Me.dmPelsHeight, ", dmDisplayFlags=", Me.dmDisplayFlags, ", dmDisplayFrequency=", Me.dmDisplayFrequency, ", dmICMMethod=", Me.dmICMMethod, ", dmICMIntent=", Me.dmICMIntent, ", dmMediaType=", Me.dmMediaType, ", dmDitherType=", Me.dmDitherType, ", dmICCManufacturer=", Me.dmICCManufacturer, ", dmICCModel=", Me.dmICCModel, ", dmPanningWidth=", Me.dmPanningWidth, ", dmPanningHeight=", Me.dmPanningHeight, "]"})
            
    End Function
        End Class

        Private Sub Test_ReadDevMode()

            
    Dim lo_DevMode As New DEVMODE
            
    Dim li_hDevMode As IntPtr

            
    If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                li_hDevMode = Me.PrintDialog1.PrinterSettings.GetHdevmode()
                
    ' li_hDevMode points to a pointer:
                Dim ptr As IntPtr = Marshal.ReadIntPtr(li_hDevMode)
                
    ' which points to the structure...
                lo_DevMode = Marshal.PtrToStructure(ptr, GetType(DEVMODE))
                Console.WriteLine(lo_DevMode.ToString)
            
    End If
            Me.Text = Marshal.SizeOf(GetType(DEVMODE))
        
    End Sub

        Sub New()
            InitializeComponent()
            Test_ReadDevMode()
        
    End Sub

    End
    Class

     

     

    Friday, August 3, 2007 12:57 AM
  • It work !!! Now I've right data in DevMode Structure.

    Thanks a lot Jo0ls.

     

    Friday, August 3, 2007 7:05 AM