Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show available printer port addresses 3

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
How can I show all the available printer port address on my computer, using Vb6?
Thanks in advance.
 
You could maybe have a loop to try to print to them all and you'd get an error if there wasnt one?
 
Tedsmith: Thanks, but I need to know the printer port address, not the printers attached.

Sberthold:
Thanks.
I cant get that script to run in VB6, also it appears to be for network printers.
I need to be able to show what is on the computer as local printer ports (LPT1, LPT2, etc). I need the address, like what is displayed in Device Manager.
I am writing a program that uses the printer port for I/O.
I would like to display the available local printer port addresses so that the user can choose which one to use in this program.
I currently have the option of requiring the user to enter the address in a box, but if he enters the wrong address the program doesnt work right.
I could also use the ability to check to see if the entered address is good.
 
Be careful. Most new motherboards have dropped parallel ports. For an old parallel printer, you have to buy a USB to LPT adapter that works like a network device and abandons the old DOS &h378 ports method.

Better to use a USB I/O adapter.

You can even use a cheap USB to serial adapter for input by echoing the tx back to the rx to sense a contact closure or use the RTS etc. pins.
 
>I cant get that script to run in VB6, also it appears to be for network printers.

You need to tweak it a little, creating the object variables and Debug instead of WScript echo.

You can enum the ports (com1,com2, LPT1, LPT2,etc) using the EnumPorts API, but, I am not sure about how you want the address for a local port.
 
Thanks SBerthold, but I cannot get the WScript code to work.
I found this bit of code:

Dim P As Object
For Each P In Printers
List1.AddItem P.Port
Next P

But all this does is list the installed ports as LPT1, etc.
I need the actual address, such as 888 or &H378.
I would think that it would be pretty simple, since the address is listed in device manager, but maybe Im wrong.
 
As there are only a few of them, perhaps you could have a couple of buttons for the user to press that call up the addresses from alist Eg Port 1, Port 2
or
try them all out invisibly when you start the app to see if you get a response?
 
Thanks Tedsmith,
The problem with calling them all is that I know there are 3 standard addresses, but if the user needs to add a PCI printer port card, then I have no way of knowing what the address is for that one.
The way i do it now is offer the 3 standard addresses as options, and a 4th option as "custom".
The user can enter the custom address in a textbox and use it.
I just need a way to test the address for correctness.
Im using inpout32.dll to access the port.
I have tried this, and it seems to be working:

dim portaddress as long
out (portaddress),2
if inp(portaddress) <> 2 then 'error routine here
out (portaddress), 0

Im just worried about someone entering an address the computer cant handle, perhaps locking it up. (This has happened to me once while testing the above logic by entering different numbers for portaddress).
Pulsing output pin #3 on is not a problem with my program (yet).
It would be nice if I could list the available addresses as options, so user cant mistype an address.
 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ParallelPort")

For Each objItem In colItems
Debug.Print objItem.Name
Set colPNP = objWMIService.ExecQuery("SELECT * FROM Win32_PNPAllocatedResource")
For Each lptport In colPNP
If InStr(Replace(lptport.dependent, "\\", "\"), objItem.PNPDeviceID) > 0 Then
Debug.Print lptport.antecedent
End If
Next
Next
 
I know you get a lot of compliments in these forums strongM but you truly are a master.
[thumbsup2]

 
Thanks strongm!
That works great.
I need to get just the address (such as 888).
I used this to strip off everything but the address.

---Your code snipped to here---
Debug.Print lptport.antecedent
For x = (Len(lptport.antecedent) - 1) To 1 Step -1
If Mid(lptport.antecedent, x, 1) = Chr(34) Then Exit For
Next x
MsgBox Val(Mid(lptport.antecedent, x + 1))

Im sure there is a better way of doing that..
You get a star, my man.. and i totally agree with three57m.
 
Sberthold, I gave you a star too, cause you pointed me in the right direction.. I just wasnt smart enough to fill in the blanks.
Thanks to all!
And if anyone knows of a better way to strip just the address out of lptport.antecedent, I'm all ears.
 
>I need to get just the address (such as 888).

Well, I wasn't going to do it all for you ... ;-)
 
I hate to bring this to the front again, but here goes.
I tested stongm's method on my PC, which only has one port, built in. It works great.
I have a friend halfway around the world to test it for me.
He said his PC doesnt have a built in port, but he added a PCI card with address of H9F00.
He said the code is not working for him.
It works on his wife's PC, which has a built in port, but not his add-on card.
Heres strongm's code, with my revisions:

Private Sub Command1_Click()
'this lists available printer port addies
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ParallelPort")

For Each objItem In colItems
Debug.Print objItem.Name
Set colPNP = objWMIService.ExecQuery("SELECT * FROM Win32_PNPAllocatedResource")
For Each lptport In colPNP
If InStr(Replace(lptport.dependent, "\\", "\"), objItem.PNPDeviceID) > 0 Then
'Debug.Print lptport.antecedent
For x = (Len(lptport.antecedent) - 1) To 1 Step -1
If Mid(lptport.antecedent, x, 1) = Chr(34) Then Exit For
Next x
List1.AddItem Val(Mid(lptport.antecedent, x + 1))
End If
Next
Next
End Sub

Does anyone have an addon card they can test this with?
 
Could this line be kicking me out of the first for-next loop?

If Mid(lptport.antecedent, x, 1) = Chr(34) Then Exit For
 
Ok, I think I give up on trying to find all available printer port addresses.
I did come up with this function to text for valid printer port address, placed in the main module:

Public Function check_address()
Out (portaddress), 1
If Inp(portaddress) <> 1 Then check_address = False Else check_address = True
Out (portaddress), 0
End Function

I just call the check_address function when a user enters in a portaddress. It returns a false if the inp does not return a 1. The value 1 is Pin 2 on the port. It will go high for a moment, if the address is good.
I dont like that part of this code, but I cant find any other way to do detect an add-on PCI printer port card.
Thanks for everyones help!
 
The problem here is that WMI doesn't see the new card as an instance of Win32_ParallelPort ...

Hmmm ... Needs a teeny bit of thought ...
 
It would be nice if I could get your method to work for me.
In my check_address routine, entering a (dec) 500 returns a true, however a 5000 returns a false, so this plan is by no means full proof. Im not even sure it is desirable, cause who knows what address Im setting to 1, if it's not a printer port address?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top