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 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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |