Click to See Complete Forum and Search --> : Marshalling error - getting DEVMODE from JOB_INFO_2


Clearcode
March 22nd, 2003, 07:54 AM
Right - the VB.Net spooler watch component (http://www.merrioncomputing.com/Programming/VBDotNet/WatchPrinter.htm) is about 70% done but I am totally stuck on getting the DEVMODE structure as held in the pointer in JOB_INFO_2.

I keep getting the error:

An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll

Additional information:
Can not marshal field DeviceMode of type PrinterQueueWatch.JOB_INFO_2:
The type definition of this field has no layout information.


I have the DEVMODE structure defined as a class thus:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Friend Class DEVMODE
<VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public pDeviceName As String 'CCHDEVICENAME
Public dmSpecversion As Int16
Public dmSize As Int16
Public dmDriverExtra As Int16
Public dmFields As Int32
<MarshalAs(UnmanagedType.U2)> Public dmOrientation As PrintJob.DeviceOrientations
<MarshalAs(UnmanagedType.U2)> Public dmPaperSize As PrintJob.PrinterPaperSizes
Public dmPaperLength As Int16
Public dmPaperWidth As Int16
Public dmScale As Int16
Public dmCopies As Int16
Public dmDefaultSource As Int16
Public dmPrintQuality As Int16
<MarshalAs(UnmanagedType.U2)> Public dmColor As PrintJob.DeviceColourModes
<MarshalAs(UnmanagedType.U2)> Public dmDuplex As PrintJob.DeviceDuplexSettings
Public dmYResolution As Int16
Public dmTTOption As Int16
Public dmCollate As Int16
<VBFixedString(32), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Public dmFormName As String
Public dmUnusedPadding As Int16
Public dmBitsPerPel As Int16
Public dmPelsWidth As Int32
Public dmPelsHeight As Int32
Public dmDisplayFlags As Int32
Public dmDisplayFrequency As Int32

End Class


and the JOB_INFO_2 defined thus:-

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Friend Class JOB_INFO_2
Public JobId As Int32
<MarshalAs(UnmanagedType.LPStr)> Public pPrinterName As String
<MarshalAs(UnmanagedType.LPStr)> Public pMachineName As String
<MarshalAs(UnmanagedType.LPStr)> Public pUserName As String
<MarshalAs(UnmanagedType.LPStr)> Public pDocument As String
<MarshalAs(UnmanagedType.LPStr)> Public pNotifyName As String
<MarshalAs(UnmanagedType.LPStr)> Public pDatatype As String
<MarshalAs(UnmanagedType.LPStr)> Public pPrintProcessor As String
<MarshalAs(UnmanagedType.LPStr)> Public pParameters As String
<MarshalAs(UnmanagedType.LPStr)> Public pDriverName As String
<MarshalAs(UnmanagedType.LPStruct)> Public DeviceMode As DEVMODE
<MarshalAs(UnmanagedType.LPStr)> Public pStatus As String
<MarshalAs(UnmanagedType.U4)> Public Status As PrintJob.Print_Job_Statuses
Public Priority As Int32
Public Position As Int32
Public TotalPage As Int32
Public PagesPrinted As Int32
<MarshalAs(UnmanagedType.Struct)> Public Submitted As SystemTime
End Class


Any ideas what I have wrong here? I'm pretty sure the problem is in the DEVMODE class because if I return lpDevMode as an Int32, cast it to an IntPtr and use

Marshal.PtrToStructue(lpDevModePtr, dmThis)

I get the same error.

Thanks in advance,
Duncan

Clearcode
March 24th, 2003, 05:46 AM
My bad - i was marshalling an Int16 to an enumerated type - but (as any fool knows) these are held internally as Int32 so it doesnae work.

Replaced:

<MarshalAs(UnmanagedType.U2)> Public dmOrientation As PrintJob.DeviceOrientations


With:

Private mdmOrientation As Int16

'...
Public ReadOnly Property dmOrientation() As PrintJob.DeviceOrientations
Get
Return CType(mdmOrientation, PrintJob.DeviceOrientations)
End get
End property


Thanks all,
Duncan

Cimperiali
March 24th, 2003, 10:30 AM
...the OnlyOneGuru...which can resolve his own questions...;)