🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #4263

Re: VB.NET & EnumJobs()

#4263
From: Pieter Philippaerts
Date: Thu, 04 Sep 2003 07:51
84 lines
5122 bytes
"SLE" <info@NOSPAMproit.be> wrote
>  Declare Auto Function GetPrinter Lib "winspool.drv" (ByVal hPrinter As
> IntPtr, ByVal Level As Integer, ByRef pPrinter As Byte, ByVal cbBuf As
> Integer, ByRef pcbNeeded As Integer) As Boolean

"ByRef pPrinter As Byte" moet "ByVal pPrinter As Byte()" worden.

>  <DllImport("winspool.drv", EntryPoint:="EnumPrintersA",
SetLastError:=True,
> CharSet:=CharSet.Unicode, ExactSpelling:=True,
> CallingConvention:=CallingConvention.StdCall)> Public Shared Function
> EnumPrinters(ByVal flags As Long, ByVal name As String, ByVal Level As
Long,
> ByVal pPrinterEnum As Byte, ByVal cdBuf As Long, ByVal pcbNeeded As Long,
> ByVal pcReturned As Long) As Long
>   End Function

Deze declaratie is compleet fout.
- Alle Longs in deze declaratie moeten Integers worden.
- "ByVal pPrinterEnum As Byte" moet  "ByVal pPrinterEnum As Byte()" worden.
- als EntryPoint geef he de ANSI versie van de functie aan, maar als CharSet
geef je Unicode aan. Ofwel moet je omschakelen naar "EnumPrintersW" als
entry point [maar deze functie is misschien niet aanwezig op Windows9x/ME]
ofwel Charset op Ansi zetten.

>  <DllImport("kernel32", EntryPoint:="lstrcpyA", SetLastError:=True,
> CharSet:=CharSet.Unicode, ExactSpelling:=True,
> CallingConvention:=CallingConvention.StdCall)> Public Shared Function
> lstrcpy(ByVal lpString1 As String, ByVal lpString2 As String) As Long
>   End Function

- Zelfde opmerking als hierboven: ANSI <=> Unicode
- return value is geen Long. Ofwel IntPtr ofwel String.

>  <DllImport("winspool.drv", EntryPoint:="ClosePrinter",
SetLastError:=True,
> CharSet:=CharSet.Unicode, ExactSpelling:=True,
> CallingConvention:=CallingConvention.StdCall)> Public Shared Function
> ClosePrinter(ByVal hPrinter As IntPtr) As Long
>   End Function

Return value is Boolean of IntPtr...

>  <DllImport("winspool.drv", EntryPoint:="EnumJobsA", SetLastError:=True,
> CharSet:=CharSet.Unicode, ExactSpelling:=True,
> CallingConvention:=CallingConvention.StdCall)> Public Shared Function
> EnumJobs(ByVal hPrinter As IntPtr, ByRef FirstJob As Long, ByVal NoJobs As
> Long, ByVal Level As Long, ByRef pJob As Byte, ByRef cdBuf As Long, ByVal
> pcbNeeded As Long, ByVal pcReturned As Long) As Long
>   End Function

- Zelfde opmerking als hierboven: ANSI <=> Unicode
- Longs moeten Integers worden
- "ByRef pJob As Byte" moet "ByVal pJob As Byte()" worden
- FirstJob, cdBuf  moeten ByVal zijn
- pcbNeeded, pcReturned moeten ByRef zijn

>  Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA"
(ByVal
> pPrinterName As String, ByRef phPrinter As IntPtr,
> <MarshalAsAttribute(UnmanagedType.AsAny)> ByVal o1 As Object) As Long

Het is natuurlijk mijn persoonlijke smaak, maar ik ben geen voorstander van:
"<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal o1 As Object"
Persoonlijk verkies ik "ByVal o1 As IntPtr" [vooral als die parameter toch
enkel gebruikt gaat worden om NULL -- IntPtr.Zero -- mee te geven].

> Wat me al onmiddellijk opvalt, is dat er nog "Long" declaraties zijn ...
ik
> dacht dat dit in .NET sowieso "Integer" aka "Int32" moest zijn?

Die declaraties zijn dan ook compleet fout. Het kan niet missen dat je
problemen hebt.
Voor CopyMemory, lstrcpy en dergelijke zijn er wel equivalente methodes in
de runtime en je bent best dat je die dan ook gebruikt. Als je gaat
interoppen dan moet je zeker en vast eens de 'Marshal' klasse bekijken -- je
zal vlug merken dat het een extreem handige interop klasse is. De meest
interessante methodes zijn PtrToStringAnsi, PtrToStringUni, PtrToStructure,
ReadInt32, ReadIntPtr, SizeOf, StructureToPtr en last but not least Copy. De
AllocHGlobal/FreeHGlobal combo kan ook zeer handig zijn bij ingewikkelde
structures.

Mvg,
Pieter Philippaerts
Technical Details
Message-ID: <1062661542.850418@seven.kulnet.kuleuven.ac.be>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!aotearoa.belnet.be!naxos.belnet.be!news.belnet.be!not-for-mail
References: <_hn5b.17396$Cq2.685084@phobos.telenet-ops.be> <1062621693.257394@seven.kulnet.kuleuven.ac.be> <3xB5b.18770$cb3.818313@phobos.telenet-ops.be>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fcea4,4b159b75c9d6aa36
X-Google-Attributes: gidfcea4,public
X-Google-ArrivalTime: 2003-09-04 00:46:27 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!aotearoa.belnet.be!naxos.belnet.be!news.belnet.be!not-for-mail
From: "Pieter Philippaerts" <Pieter@nospam.mentalis.org>
Newsgroups: be.comp.programming
Subject: Re: VB.NET & EnumJobs()
Date: Thu, 4 Sep 2003 09:51:31 +0200
Organization: KULeuvenNet
Lines: 86
Message-ID: <1062661542.850418@seven.kulnet.kuleuven.ac.be>
References: <_hn5b.17396$Cq2.685084@phobos.telenet-ops.be> <1062621693.257394@seven.kulnet.kuleuven.ac.be> <3xB5b.18770$cb3.818313@phobos.telenet-ops.be>
NNTP-Posting-Host: seven.kulnet.kuleuven.ac.be
X-Trace: naxos.belnet.be 1062661543 22801 134.58.127.12 (4 Sep 2003 07:45:43 GMT)
X-Complaints-To: abuse@belnet.be
NNTP-Posting-Date: Thu, 4 Sep 2003 07:45:43 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.0
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Cache-Post-Path: seven.kulnet.kuleuven.ac.be!unknown@localhost
X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/)