Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

Code Conversion Nightmare

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Vaughan - 09 Sep 2005 12:10 GMT
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
Cowboy (Gregory A. Beamer) - MVP - 09 Sep 2005 14:07 GMT
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.

Signature

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

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

> 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
[quoted text clipped - 146 lines]
>
> Thanks
Lloyd Dupont - 09 Sep 2005 16:23 GMT
what about:

using System.Drawing.Priniting;

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.