Code Conversion Nightmare

J

Jon Vaughan

I have been given some code examples for printer status monitoring from a
company. But they only have the code in vb or vc. im coding in vb.net and im
trying to convert from one to the other. The problem is that the code uses
the vb6 syntax of "as any" when declaring the api functions. But vb.net
doesnt support these. What im hoping is that someone who knows more than me
about vb.net and api calling can have a look and see whats going on.

The code is as follows :

Attribute VB_Name = "Module1"
'WIN32API

Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal
pPrinterName As String, _
phPrinter
As Long, _
ByVal
pDefault As Long) As Long

Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As
Long

Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA"
(ByVal Flags As Long, _
ByVal
pName As String, _
ByVal
Level As Long, _
pPrinterInfo
As Any, _
ByVal
cdBuf As Long, _
pcbNeeded
As Long, _
pcReturned
As Long) As Long

Declare Function EnumPrinterDrivers Lib "winspool.drv" Alias
"EnumPrinterDriversA" (ByVal pName As String, _

ByVal pEnvironment As String, _

ByVal Level As Long, _

pDriverInfo As Any, _

ByVal cdBuf As Long, _

pcbNeeded As Long, _

pcRetruned As Long) As Long

Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination
As Any, _
Source As
Any, _
ByVal
Length As Long)

Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (lpString1 As
Any, _
lpString2 As
Any) As Long

Type PRINTER_INFO_2
pServerName As Long
pPrinterName As Long
pShareName As Long
pPortName As Long
pDriverName As Long
pComment As Long
pLocation As Long
pDevMode As Long
pSepFile As Long
pPrintProcessor As Long
pDatatype As Long
pParameters As Long
pSecurityDescriptor As Long
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type

Public Type DRIVER_INFO_3
cVersion As Long
pName As Long
pEnvironment As Long
pDriverPath As Long
pDataFile As Long
pConfigFile As Long
pHelpFile As Long
pDependentFiles As Long
pMonitorName As Long
pDefaultDataType As Long
End Type

Public Const INVALID_HANDLE_VALUE = -1

Public Const PRINTER_ENUM_LOCAL = &H2

'SMJSMON.DLL

Declare Function SMJSMonGetStatus Lib "SMJSMon" (ByVal hPrinter As Long, _
lpBuffer As Byte, _
ByVal dwNumberOfBytesToRead
As Long, _
lpdwNumberOfBytesRead As
Long) As Long

Declare Function SMJSMonGetStatusEx Lib "SMJSMon" (ByVal hPrinter As Long, _
lpBuffer As Byte, _
ByVal
dwNumberOfBytesToRead As Long, _
lpdwNumberOfBytesRead As
Long) As Long

The calling code that is giving the error is as follows :

MoveMemory(PrinterInfo(0), byPrinterInfoBuffer(0), Len(PrinterInfo(0)) *
nPrinterInfoReturned)



Printer info being defined earlier as :



Dim PrinterInfo() As PRINTER_INFO_2



As PrinterInfo(0) is accessing

Dim pServerName As Integer

from the PRINTER_INFO_2 structure, So I changed the function as follows



Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination
As integer, _
Source As
Byte, _
ByVal
Length As Long)



But the error I get is Value of type 'Project1.Module1.PRINTER_INFO_2'
cannot be converted to 'Integer'."



Does anyone have an idea what is going on ? or where I can start to solve
this problem ?



Thanks
 
G

Guest

I would google on this one, as there may be an easier way to contact printers
in .NET (some base class). I have not done it.

To move from COM to .NET and still use API calls, you end up using PInovke.
There are a couple of sites that I know can help, if you understand the basic
concept:

http://www.pinvoke.net
http://www.webtropy.com/articles/Win32-API-DllImport-art9.asp?Windows+API

Of the two, the second is a bit easier to navigate, as the first is aimed
more at the experienced API programmer. If you use the webtropy site, you
will find code snippets that should help you with the API declarations. Once
declared the code that uses the items will be similar, which means you should
be able to port at least some of the VB code.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
L

Lloyd Dupont

what about:

using System.Drawing.Priniting;

foreach(PrinterSettings ps in PrinterSettings.InstalledPrinters)
{
IntPtr hDevMode = ps.GetHdevmode();
// something....
}
that should get you a long way ;-).....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top