Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - WshShell.RegRead

Reply
 
Old 03-16-2009   #1 (permalink)
Fred


 
 

WshShell.RegRead

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



My System SpecsSystem Spec
Old 03-17-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: WshShell.RegRead


"Fred" <leavemealone@xxxxxx> wrote in message
news:ee5nGCppJHA.1248@xxxxxx
Quote:

>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
>
Let's have a look at your script!


My System SpecsSystem Spec
Old 03-17-2009   #3 (permalink)
Fred


 
 

Re: WshShell.RegRead

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]" <news@xxxxxx> wrote in message
news:e50ZKqtpJHA.5832@xxxxxx
Quote:

>
> "Fred" <leavemealone@xxxxxx> wrote in message
> news:ee5nGCppJHA.1248@xxxxxx
Quote:

>>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
>>
>
> Let's have a look at your script!
>

My System SpecsSystem Spec
Old 03-17-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: WshShell.RegRead

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@xxxxxx> wrote in message
news:e7zQrI0pJHA.1248@xxxxxx
Quote:

> 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]" <news@xxxxxx> wrote in message
> news:e50ZKqtpJHA.5832@xxxxxx
Quote:

>>
>> "Fred" <leavemealone@xxxxxx> wrote in message
>> news:ee5nGCppJHA.1248@xxxxxx
Quote:

>>>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
>>>
>>
>> Let's have a look at your script!
>>
>
>

My System SpecsSystem Spec
Old 03-17-2009   #5 (permalink)
Al Dunbar


 
 

Re: WshShell.RegRead


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%23iXcmy0pJHA.5332@xxxxxx
Quote:

> 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

Quote:

> "Fred" <leavemealone@xxxxxx> wrote in message
> news:e7zQrI0pJHA.1248@xxxxxx
Quote:

>> 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]" <news@xxxxxx> wrote in message
>> news:e50ZKqtpJHA.5832@xxxxxx
Quote:

>>>
>>> "Fred" <leavemealone@xxxxxx> wrote in message
>>> news:ee5nGCppJHA.1248@xxxxxx
>>>>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
>>>>
>>>
>>> Let's have a look at your script!
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 03-17-2009   #6 (permalink)
Pegasus [MVP]


 
 

Re: WshShell.RegRead


"Al Dunbar" <alandrub@xxxxxx> wrote in message
news:O1E%23Yb1pJHA.4108@xxxxxx
Quote:

>
>
> 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?


My System SpecsSystem Spec
Old 03-18-2009   #7 (permalink)
Al Dunbar


 
 

Re: WshShell.RegRead


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%23wr0mh1pJHA.4840@xxxxxx
Quote:

>
> "Al Dunbar" <alandrub@xxxxxx> wrote in message
> news:O1E%23Yb1pJHA.4108@xxxxxx
Quote:

>>
>>
>> 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?
The OP. Sorry for not stating that more explicitly...

/Al


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
WshShell.Exec workaround VB Script
some questions about 'username' and 'WSHshell.run' VB Script
Using WshShell.Exec and retrieving output VB Script
Need to read 64-bit registry from 32-bit RegRead VB Script
Supplying a variable to wshshell.Run VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46