I can't see anything wrong with what you have there - however just in case
it is of use, my code is as follows:-
#Region "EnumPrinters"
<DllImport("winspool.drv", EntryPoint:="EnumPrinters", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function EnumPrinters(<InAttribute()> ByVal Flags As
EnumPrinterFlags, _
<InAttribute()> ByVal Name As String, _
<InAttribute()> ByVal Level As Int32, _
<OutAttribute()> ByVal lpBuf As IntPtr, _
<InAttribute()> ByVal cbBuf As Int32, _
<OutAttribute()> ByRef pcbNeeded As Int32,
_
<OutAttribute()> ByRef pcbReturned As
Int32) As Boolean
End Function
<DllImport("winspool.drv", EntryPoint:="EnumPrinters", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=False, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function EnumPrinters(<InAttribute()> ByVal Flags As
EnumPrinterFlags, _
<InAttribute()> ByVal Name As IntPtr, _
<InAttribute()> ByVal Level As Int32, _
<OutAttribute()> ByVal lpBuf As IntPtr, _
<InAttribute()> ByVal cbBuf As Int32, _
<OutAttribute()> ByRef pcbNeeded As Int32, _
<OutAttribute()> ByRef pcbReturned As Int32) As
Boolean
End Function
#End Region
and is used thus:-
Public Sub New()
Dim pcbNeeded As Int32 '\\ Holds the requires size of the output
buffer (in bytes)
Dim pcReturned As Int32 '\\ Holds the returned size of the output
buffer
Dim pPrinters As IntPtr
Dim pcbProvided As Int32 = 0
If Not EnumPrinters(EnumPrinterFlags.PRINTER_ENUM_NAME,
String.Empty, 1, pPrinters, 0, pcbNeeded, pcReturned) Then
If pcbNeeded > 0 Then
pPrinters = Marshal.AllocHGlobal(pcbNeeded)
pcbProvided = pcbNeeded
If Not EnumPrinters(EnumPrinterFlags.PRINTER_ENUM_NAME,
String.Empty, 1, pPrinters, pcbProvided, pcbNeeded, pcReturned) Then
Throw New Win32Exception
End If
End If
End If
If pcReturned > 0 Then
'\\ Get all the monitors for the given server
Dim ptNext As IntPtr = pPrinters
While pcReturned > 0
Dim pi1 As New PRINTER_INFO_1
Marshal.PtrToStructure(ptNext, pi1)
If Not pi1.pName Is Nothing Then
Me.Add(New PrinterInformation(pi1.pName,
PrinterAccessRights.PRINTER_ACCESS_USE, False)) ', pi2.pLocation,
pi2.pComment, pi2.pServerName, 1))
End If
ptNext = New IntPtr(ptNext.ToInt32 +
Marshal.SizeOf(GetType(PRINTER_INFO_1)))
pcReturned -= 1
End While
End If
'\\ Free the allocated buffer memory
If pPrinters.ToInt32 > 0 Then
Marshal.FreeHGlobal(pPrinters)
End If
End Sub
(The full code is in CodePlex :
http://www.codeplex.com/PrintQueueWatch in
case I have missed anything in this cut-and-paste)
Hope this helps,
Duncan