By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,641 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

EnumPrinters gives system.nullreferencedexception

P: n/a
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)
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

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

http://msdn.microsoft.com/library/de...32_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

-----------------------------

"Steve Armstrong" <sa********@tally.co.uk> wrote in message
news:db**************************@posting.google.c om...
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)

Nov 20 '05 #2

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #3

P: n/a
Mattias,

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

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.