EnumPrinters gives system.nullreferencedexception

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)
 
K

Ken Tucker [MVP]

Hi,

I usually use the WMI to get that info. Add a reference to
system.management.dll.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printer.asp


Private Sub getPrinters()

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_Printer")

moReturn = moSearch.Get

For Each mo In moReturn

ComboBox1.Items.Add(mo("Name"))

Next

End Sub



Ken
 
M

Mattias Sjögren

Steve,
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

How did you decare PRINTER_INFO_2 and PRINTER_INFO_5?

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

flags should be an Integer.



Mattias
 
S

Steven Armstrong

Mattias,

Thank you very much for your help - that worked fine.

As you guessed I set all the entries in the structure to strings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top