S
Stephanie Doherty
Hello World,
I am trying to read an intptr() returned from a call to EnumPrinters into a
structure and keep getting an exception. The relevant code looks like:
Public Structure pInfo
<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
<DllImport("winspool.drv", EntryPoint:="EnumPrinters", _
SetLastError:=True, CharSet:=CharSet.Unicode)> Public 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
selectedItem = "\\" & CmB_Servers.SelectedItem
server = selectedItem.ToString()
EnumPrinters(flag, server, PRINTER_LEVEL_1, outC, 0, pcbNeeded,
pcReturned)
outC = Marshal.AllocHGlobal(pcbNeeded + 1)
If EnumPrinters(flag, server, PRINTER_LEVEL_1, outC, pcbNeeded,
pcbNeeded, pcReturned) > 0 Then
Dim manyPr As pInfo
Dim currentP As IntPtr = outC
Dim i As Integer
For i = 1 To pcReturned
manyPr = Marshal.PtrToStructure(currentP, manyPr.GetType())
CmB_Printers.Items.Add(manyPr.pName)
currentP = IntPtr.op_Explicit(currentP.ToInt32 +
Marshal.SizeOf(manyPr.GetType))
Next
Else
MsgBox("unable to get printers")
End If
Marshal.FreeHGlobal(outC)
----
The exception thrown is: Object reference not set to an instance of an object
I assume that is referring to the manyPr variable, but am not sure what I
should be doing about it.
Help!
Thanks,
Stephanie
I am trying to read an intptr() returned from a call to EnumPrinters into a
structure and keep getting an exception. The relevant code looks like:
Public Structure pInfo
<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
<DllImport("winspool.drv", EntryPoint:="EnumPrinters", _
SetLastError:=True, CharSet:=CharSet.Unicode)> Public 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
selectedItem = "\\" & CmB_Servers.SelectedItem
server = selectedItem.ToString()
EnumPrinters(flag, server, PRINTER_LEVEL_1, outC, 0, pcbNeeded,
pcReturned)
outC = Marshal.AllocHGlobal(pcbNeeded + 1)
If EnumPrinters(flag, server, PRINTER_LEVEL_1, outC, pcbNeeded,
pcbNeeded, pcReturned) > 0 Then
Dim manyPr As pInfo
Dim currentP As IntPtr = outC
Dim i As Integer
For i = 1 To pcReturned
manyPr = Marshal.PtrToStructure(currentP, manyPr.GetType())
CmB_Printers.Items.Add(manyPr.pName)
currentP = IntPtr.op_Explicit(currentP.ToInt32 +
Marshal.SizeOf(manyPr.GetType))
Next
Else
MsgBox("unable to get printers")
End If
Marshal.FreeHGlobal(outC)
----
The exception thrown is: Object reference not set to an instance of an object
I assume that is referring to the manyPr variable, but am not sure what I
should be doing about it.
Help!
Thanks,
Stephanie