Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WshShell.RegRead

281 views
Skip to first unread message

Fred

unread,
Mar 16, 2009, 7:39:52 PM3/16/09
to
I am trying to use WshShell.RegRead to get the driver and port that a
printer is connected to.
The Key in the registry is HKCU\Software\Microsoft\Windows
NT\CurrentVersion\PrinterPorts

The problem is that the value name is "\\my_server\PrinterName"
Of course if I use the Key with the valuename as the argument for the
RegRead it trys to use
HKCU\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts\\my_server\"
as the key and "PrinterName" as the value name and I get an error that the
key does not exist.

How can I read this registry value?

Thanks for any help
Fred


Pegasus [MVP]

unread,
Mar 17, 2009, 4:29:50 AM3/17/09
to

"Fred" <leavemealone@home> wrote in message
news:ee5nGCpp...@TK2MSFTNGP03.phx.gbl...

Let's have a look at your script!


Fred

unread,
Mar 17, 2009, 4:51:22 PM3/17/09
to
The VB procedure below tries to set the default printer but the RegRead
fails with "no such key". Obviously appending PrterName (which is the
registry's value name) to the Key actually changes the key. The Key becomes
"Original Key\\\myServer\" and value name becomes PrinterName.

How else can I read the registry value data at key:
"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices\"
and value name:
"\\myServer\PrinterName"

or is there another way I can set the default printer.

Thanks
Fred


Sub SetDefaultPrinter(PrterName As String)
'PrterName is the printer name in the form \\myServer\PrinterName
Dim FPrinter As String, WShell As Object

Set WShell = CreateObject("WScript.Shell")
FPrinter = WShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows
NT\CurrentVersion\Devices\" & PrterName)
If FPrinter <> "" Then
WShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows
NT\CurrentVersion\Windows\device", FPrinter, REG_SZ
End If
Set WShell = Nothing
End Sub


"Pegasus [MVP]" <ne...@microsoft.com> wrote in message
news:e50ZKqtp...@TK2MSFTNGP06.phx.gbl...

Pegasus [MVP]

unread,
Mar 17, 2009, 6:06:39 PM3/17/09
to
I'm getting a little confused. In your first post you mentioned this
registry value:

HKCU\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts
which has now become
HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices

I also note that your program treats REG_SZ as a variable or as a constant
whereas in fact it should be a string, to be surrounded with double quotes.

About your main problem of being unable to extract registry data when the
value name refers to a remote machine: AFAIK, the RegRead method of the
Wscript Shell object is incapable of dealing with such value names. You need
to use WMI instead, e.g. like so in VB Script:

sPrinter = "\\Server\HPLJ4"
Const HKCU = &H80000001
sKey = "Software\Microsoft\Windows NT\CurrentVersion\Devices"
Set oReg =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
iSuccess = oReg.GetStringValue(HKCU, sKey, sPrinter, sData)


"Fred" <leavemealone@home> wrote in message

news:e7zQrI0p...@TK2MSFTNGP03.phx.gbl...

Al Dunbar

unread,
Mar 17, 2009, 7:19:39 PM3/17/09
to

"Pegasus [MVP]" <ne...@microsoft.com> wrote in message
news:%23iXcmy0...@TK2MSFTNGP03.phx.gbl...

> I'm getting a little confused. In your first post you mentioned this
> registry value:
> HKCU\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts
> which has now become
> HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices
>
> I also note that your program treats REG_SZ as a variable or as a constant
> whereas in fact it should be a string, to be surrounded with double
> quotes.
>
> About your main problem of being unable to extract registry data when the
> value name refers to a remote machine: AFAIK, the RegRead method of the
> Wscript Shell object is incapable of dealing with such value names. You
> need to use WMI instead, e.g. like so in VB Script:
>
> sPrinter = "\\Server\HPLJ4"
> Const HKCU = &H80000001
> sKey = "Software\Microsoft\Windows NT\CurrentVersion\Devices"
> Set oReg =
> GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
> iSuccess = oReg.GetStringValue(HKCU, sKey, sPrinter, sData)

Also, your program seems to be written in VB or VBA rather than VBScript.
Are you sure you are in the right newsgroup?

/Al

Pegasus [MVP]

unread,
Mar 17, 2009, 7:30:46 PM3/17/09
to

"Al Dunbar" <alan...@hotmail.com> wrote in message
news:O1E%23Yb1pJ...@TK2MSFTNGP06.phx.gbl...

>
>
> Also, your program seems to be written in VB or VBA rather than VBScript.
> Are you sure you are in the right newsgroup?

Replying to me or to the OP?


Al Dunbar

unread,
Mar 18, 2009, 12:49:17 AM3/18/09
to

"Pegasus [MVP]" <ne...@microsoft.com> wrote in message
news:%23wr0mh1...@TK2MSFTNGP06.phx.gbl...

The OP. Sorry for not stating that more explicitly...

/Al


0 new messages