S
Steve Armstrong
I use the following code and can get all the correct printer
information back for Printer_Info_1.
However, when I try the same code for Printer_Info_2 or 5 I get a
System.NullReferenceException. Can someone please help
<DllImport("winspool.drv", EntryPoint:="EnumPrintersA",
SetLastError:=True, CharSet:=CharSet.Unicode)> Private Shared Function
EnumPrinters(ByVal flags As Int32, ByVal pName As String, ByVal Level
As Int32, ByVal pPrinterEnum As IntPtr, ByVal cbBuf As Int32, ByRef
pcbNeeded As Int32, ByRef pcReturned As Int32) As Int32
End Function
Public Structure PRINTER_INFO_1
<MarshalAs(UnmanagedType.LPStr)> Dim flags As String
<MarshalAs(UnmanagedType.LPStr)> Dim pDescription As String
<MarshalAs(UnmanagedType.LPStr)> Dim pName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pComment As String
End Structure
Dim pcbNeeded As Integer = 0
Dim pcReturned As Integer = 0
Dim outC As IntPtr = IntPtr.Zero
Dim flag As Integer = PRINTER_ENUM_LOCAL
EnumPrinters(flag, "", 1, outC, 0, pcbNeeded, pcReturned)
outC = Marshal.AllocHGlobal(pcbNeeded + 1)
EnumPrinters(flag, "", 1, outC, pcbNeeded, pcbNeeded,
pcReturned)
MsgBox(pcReturned)
Dim manyPr(pcReturned) As PRINTER_INFO_1
Dim currentP As IntPtr = outC
Dim p As New PRINTER_INFO_1()
Dim i As Integer
For i = 1 To pcReturned
manyPr(i) = Marshal.PtrToStructure(currentP, p.GetType)
MsgBox(manyPr(i).pPrinterName)
ComboBox1.Items.Add(manyPr(i).pPrinterName)
currentP = IntPtr.op_Explicit(currentP.ToInt32 +
Marshal.SizeOf(p.GetType))
Next
Marshal.FreeHGlobal(outC)
information back for Printer_Info_1.
However, when I try the same code for Printer_Info_2 or 5 I get a
System.NullReferenceException. Can someone please help
<DllImport("winspool.drv", EntryPoint:="EnumPrintersA",
SetLastError:=True, CharSet:=CharSet.Unicode)> Private Shared Function
EnumPrinters(ByVal flags As Int32, ByVal pName As String, ByVal Level
As Int32, ByVal pPrinterEnum As IntPtr, ByVal cbBuf As Int32, ByRef
pcbNeeded As Int32, ByRef pcReturned As Int32) As Int32
End Function
Public Structure PRINTER_INFO_1
<MarshalAs(UnmanagedType.LPStr)> Dim flags As String
<MarshalAs(UnmanagedType.LPStr)> Dim pDescription As String
<MarshalAs(UnmanagedType.LPStr)> Dim pName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pComment As String
End Structure
Dim pcbNeeded As Integer = 0
Dim pcReturned As Integer = 0
Dim outC As IntPtr = IntPtr.Zero
Dim flag As Integer = PRINTER_ENUM_LOCAL
EnumPrinters(flag, "", 1, outC, 0, pcbNeeded, pcReturned)
outC = Marshal.AllocHGlobal(pcbNeeded + 1)
EnumPrinters(flag, "", 1, outC, pcbNeeded, pcbNeeded,
pcReturned)
MsgBox(pcReturned)
Dim manyPr(pcReturned) As PRINTER_INFO_1
Dim currentP As IntPtr = outC
Dim p As New PRINTER_INFO_1()
Dim i As Integer
For i = 1 To pcReturned
manyPr(i) = Marshal.PtrToStructure(currentP, p.GetType)
MsgBox(manyPr(i).pPrinterName)
ComboBox1.Items.Add(manyPr(i).pPrinterName)
currentP = IntPtr.op_Explicit(currentP.ToInt32 +
Marshal.SizeOf(p.GetType))
Next
Marshal.FreeHGlobal(outC)