473,473 Members | 4,297 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

EnumPrinter Madness

More EnumPrinters help wanted. I wrote the following code from an example I
found on .NET 247. Anyhow, look at the For/Next loop. The first iteration,
it works fine. The messagebox shows me the share name of a printer. But on
the 2nd iteration, the Marshal.PtrToStructure line throws an
AccessViolationException was unhandled (Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.). Can
anyone see what may be wrong in the code?

Thanks.

Imports System.Runtime.InteropServices

Module EnumPrinters

Public Function GetPrintShares(ByVal Server As String) As ArrayList
Dim pcbNeeded As Int32
Dim pcbReturned As Int32
Dim outC As IntPtr = IntPtr.Zero
Dim tmp As Integer = (EnumPrinters(PrinterEnum.peNAME Or
PrinterEnum.peSHARED, _
"", 2, outC, 0, pcbNeeded, pcbReturned))
outC = Marshal.AllocHGlobal(pcbNeeded)
tmp = EnumPrinters(PrinterEnum.peNAME Or PrinterEnum.peSHARED, _
"", 2, outC, pcbNeeded, pcbNeeded, pcbReturned)
MsgBox(tmp & " - " & pcbNeeded & " - " & pcbReturned)
Dim manyPR(pcbReturned) As PRINTER_INFO_2
Dim currentP As IntPtr = outC
Dim MyAL As New ArrayList
For i As Integer = 1 To pcbReturned
manyPR(i) = Marshal.PtrToStructure(currentP, manyPR(i).GetType)
MsgBox(manyPR(i).pShareName)
MyAL.Add(manyPR(i).pShareName)
currentP = IntPtr.op_Explicit(currentP.ToInt32 +
Marshal.SizeOf(manyPR(i).GetType))
Next
Marshal.FreeHGlobal(outC)
Return MyAL
End Function

Private Declare Auto Function EnumPrinters Lib "winspool.drv" Alias
"EnumPrintersA" _
(ByVal Flags As Int32, _
ByVal Name As String, _
ByVal Level As Int32, _
ByVal pPrinterEnum As IntPtr, _
ByVal cbBuf As Int32, _
ByRef pcbNeeded As Int32, _
ByRef pcReturned As Int32) As Integer

Private Structure PRINTER_INFO_2
<MarshalAs(UnmanagedType.LPStr)> Dim pServerName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pPrinterName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pShareName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pPortName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pDriverName As String
<MarshalAs(UnmanagedType.LPStr)> Dim pComment As String
<MarshalAs(UnmanagedType.LPStr)> Dim pLocation As String
Dim pDevMode As IntPtr
<MarshalAs(UnmanagedType.LPStr)> Dim pSepFile As String
<MarshalAs(UnmanagedType.LPStr)> Dim pPrintProcessor As String
<MarshalAs(UnmanagedType.LPStr)> Dim pDatatype As String
<MarshalAs(UnmanagedType.LPStr)> Dim pParameters As String
Dim pSecurityDescriptor As IntPtr
Dim Attributes As Long
Dim Priority As Long
Dim DefaultPriority As Long
Dim StartTime As Long
Dim UntilTime As Long
Dim Status As Long
Dim cJobs As Long
Dim AveragePPM As Long
End Structure

Private Enum PrinterEnum
peDEFAULT = &H1
peLOCAL = &H2
peCONNECTIONS = &H4
peNAME = &H8
peREMOTE = &H10
peSHARED = &H20
peNETWORK = &H40
End Enum

End Module
Nov 21 '05 #1
4 1973
Terry,
Private Structure PRINTER_INFO_2 .... Dim Attributes As Long
Dim Priority As Long
Dim DefaultPriority As Long
Dim StartTime As Long
Dim UntilTime As Long
Dim Status As Long
Dim cJobs As Long
Dim AveragePPM As Long
End Structure


All these Longs should be Integers.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Terry,
Private Structure PRINTER_INFO_2 .... Dim Attributes As Long
Dim Priority As Long
Dim DefaultPriority As Long
Dim StartTime As Long
Dim UntilTime As Long
Dim Status As Long
Dim cJobs As Long
Dim AveragePPM As Long
End Structure


All these Longs should be Integers.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3
Thanks. That did the trick...nothing like stumbling through something...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...
Terry,
Private Structure PRINTER_INFO_2

...
Dim Attributes As Long
Dim Priority As Long
Dim DefaultPriority As Long
Dim StartTime As Long
Dim UntilTime As Long
Dim Status As Long
Dim cJobs As Long
Dim AveragePPM As Long
End Structure


All these Longs should be Integers.
Mattias

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

Nov 21 '05 #4
Thanks. That did the trick...nothing like stumbling through something...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...
Terry,
Private Structure PRINTER_INFO_2

...
Dim Attributes As Long
Dim Priority As Long
Dim DefaultPriority As Long
Dim StartTime As Long
Dim UntilTime As Long
Dim Status As Long
Dim cJobs As Long
Dim AveragePPM As Long
End Structure


All these Longs should be Integers.
Mattias

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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: S. Rhodes | last post by:
Hello everyone. I am TOTALLY baffled. PLEASE, I need some help here. In the event that one must deal with a name including an apostrophe...I am looking for an appropriate solution. The...
1
by: Lane LiaBraaten | last post by:
I am developing a GUI that uses multiple threading.Threads, and Queue.Queues for communicating between the threads, and threading.Timers for checking the queues. Everything works well (runs to...
4
by: Afanasiy | last post by:
Can I easily convert from ISO to Gregorian calendars in Python 2.3? For example, what Gregorian date is year 2004, week 1, day 1? If possible, I'd greatly prefer to do this with the standard...
59
by: Philipp Lenssen | last post by:
I've tested some of the new Nokia 6600 functionality. It ships with WAP2 and XHTML Support (it says). What it does is check the Doctype -- if it's not the XHTML Mobile Profile Doctype, but a...
0
by: Abubakar | last post by:
Hi, Am having trouble in solving some problems. Thses are (1). how can i call Readprinter API fuction using C#.Net or VB.Net (2). Am writting a program that will enumerate the printers installed...
0
by: Terry Olsen | last post by:
More EnumPrinters help wanted. I wrote the following code from an example I found on .NET 247. Anyhow, look at the For/Next loop. The first iteration, it works fine. The messagebox shows me the...
1
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
1
by: =?Utf-8?B?SmFja2Rhdw==?= | last post by:
Hi! I just up-graded my "Dell Help & Support Center" and now every time I start upthe Dell folder is open on the desk top. How do I make it GO AWAY, lest madness overtake me. Thanks! -- DAW
20
by: raylopez99 | last post by:
Took a look at all the fuss about "lambda expressions" from Jon Skeet's excellent book "C# in Depth". Jon has an example, reproduced below (excerpt) on lambda expressions. My n00b take: it's...
0
by: Pavel Minaev | last post by:
On Aug 14, 1:37 pm, raylopez99 <raylope...@yahoo.comwrote: You are, of course, entitled to your opinion, but lambdas are a part of C# 3.0, and any C# developer has to know their ins and outs if...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.