Page 1 of 1 1
Topic Options
#209293 - 2014-07-29 03:31 PM Can functions have an OUT parameter
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
In all the years I've used KiXtart, I don't think I've ever tried to use an OUT parameter for a function. All function parameters I've ever defined are IN parameters. Does KiXtart not support this?

I'm trying to manipulate the registry using WMI because the KiXtart method will not work against the remote computer. However, all the WMI registry GETs put the data in the last parameter (variable) specified in the function, and I cannot get this to work no matter what I try.

Here's an example:

 Code:
$oWMIReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\COMP1\root\default:StdRegProv")
$HKLM = 2147483650
$RegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$RegValue = "Open"
$Result = $oWMIReg.GetStringValue($HKLM, $RegKey, $RegValue, $OUTValue)
$ = MessageBox($OUTValue, "GetStringValue")


In theory (and in vbscript) the registry value will be in the variable $OUTValue.

Top
#209294 - 2014-07-29 03:43 PM Re: Can functions have an OUT parameter [Re: Bonji]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
This won't work?


 Code:
$RegKey = "\\" + $Computername + "\HKLM\System\CurrentControlSet\Services\TCPIP\Performance"
$RegValue = "Open"
$result=readvalue($regkey,$regvalue)

Top
#209295 - 2014-07-29 03:48 PM Re: Can functions have an OUT parameter [Re: Allen]
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
 Quote:
I'm trying to manipulate the registry using WMI because the KiXtart method will not work against the remote computer.


Sadly, no.

It's a problem with the remote computer's setup, but there's nothing I can do about that. I have to use WMI to manipulate the registry because WMI is about the only thing I can use against it remotely.

Top
#209296 - 2014-07-29 03:51 PM Re: Can functions have an OUT parameter [Re: Bonji]
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
This is an interesting general question as well about KiXtart supporting OUT parameters in functions, but if I can't get a native KiXtart solution I'll just call a vbscript to get that part done and move on.
Top
#209297 - 2014-07-29 03:52 PM Re: Can functions have an OUT parameter [Re: Bonji]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
If no one comes up with a solution, there is always the scriptcontrol object. If you search posts by my name and keyword scriptcontrol you will find a couple of examples.
Top
#209298 - 2014-07-29 05:21 PM Re: Can functions have an OUT parameter [Re: Allen]
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
It's not a pure solution, but it keeps everything inside my KiX script and most importantly: IT WORKS!!

That's a handy object to keep in mind for future needs. Thanks!

 Code:
	$VBScriptCode = '

Dim strData
Sub MySub()
	Const HKEY_LOCAL_MACHINE = &H80000002
	strComputer = "' + $txtComp.Text + '"
	strRegPath = "System\CurrentControlSet\Services\TCPIP\Linkage"
	strRegVal = "Bind"

	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

	Return = oReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strRegPath,strRegVal,arrValues)
	If (Return = 0) And (Err.Number = 0) Then
		For Each strValue In arrValues
			strData = strData & "{}{}" & strValue & vbCrLf
		Next
	Else
	End If
End Sub'

	$VBScript = CreateObject("ScriptControl")
	$VBScript.Language = "VBScript"
	$VBScript.AddCode($VBScriptCode)
	$ = $VBScript.Run("MySub")
	$aryReg = Split($VBScript.CodeObject.strData, "{}{}")
	For Each $Element In $aryReg
		$ = messagebox($Element, "Element")
	Next

Top
#209299 - 2014-07-29 05:46 PM Re: Can functions have an OUT parameter [Re: Bonji]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Glad it works, although I'm surprised you didn't run into quoting issues (especially the single quote).
Top
#209300 - 2014-07-29 06:48 PM Re: Can functions have an OUT parameter [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
On the out param. Has been in the wish list for some 10 years. Don't hold your breath:)
_________________________
!

download KiXnet

Top
#209302 - 2014-07-29 10:03 PM Re: Can functions have an OUT parameter [Re: Lonkero]
Bonji Offline
Starting to like KiXtart

Registered: 2001-09-28
Posts: 169
Loc: Virginia
 Quote:
Glad it works, although I'm surprised you didn't run into quoting issues (especially the single quote).


I have the entirety of the vbs code inside single quotes because I use double quotes in the vbs code. There is one point where I close off the string so I can concatenate a variable I use elsewhere in my KiX script, but then I start it back again with another single quote. I'm not exactly sure what you're referring to as this is standard stuff I do all the time.

Top
#209304 - 2014-07-29 10:06 PM Re: Can functions have an OUT parameter [Re: Bonji]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
My eyes deceived me. Apologies.
Top
#209305 - 2014-07-29 11:21 PM Re: Can functions have an OUT parameter [Re: Lonkero]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
hello,

WMI gives a solution to get "out" value. This is not very easy but it is possible.
Here is an example of code "full KiXtart" :
 Code:
break on

$=SetOption( "Explicit", "ON" )
$=SetOption( "NoVarsInStrings", "ON" )
$=SetOption( "NoMacrosInStrings", "ON" )

global $namespace, $objWMIService, $objRegistry, $wmiclass, $wmimethod
global $objInParams, $objOutParams

global $HKEY_CLASSES_ROOT, $HKEY_CURRENT_USER, $HKEY_LOCAL_MACHINE
global $HKEY_USERS, $HKEY_CURRENT_CONFIG
$HKEY_CLASSES_ROOT              = (CDBL(2147483647)+1)	; &80000000
$HKEY_CURRENT_USER              = (CDBL(2147483647)+2)	; &80000001
$HKEY_LOCAL_MACHINE             = (CDBL(2147483647)+3)	; &80000002
$HKEY_USERS                     = (CDBL(2147483647)+4)	; &80000003
$HKEY_CURRENT_CONFIG            = (CDBL(2147483647)+6)	; &80000005

global $strComputer, $strRegKey, $strRegValueName, $RegValue, $byte
$strComputer = "."
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"

;-- Connect to WMI --
$namespace = "root\default"
$objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate,(debug,security)}!\\'+$strComputer+'\'+$namespace)

$wmiclass = "StdRegProv"

$objRegistry = $objWMIService.Get($wmiclass)

;-------------------------------------------------------------------------------
; example to read a value in the registry with WMI with GetStringValue method
;
; uint32 GetStringValue(
;   [in]  uint32 hDefKey = HKEY_LOCAL_MACHINE,
;   [in]  string sSubKeyName,
;   [in]  string sValueName,
;   [out] string sValue
; )
;-------------------------------------------------------------------------------
"First read (REG_SZ)" ?
$strRegValueName = "Open"
$wmimethod = "GetStringValue"

$objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
$objInParams.hDefKey = $HKEY_LOCAL_MACHINE
$objInParams.sSubKeyName = $strRegKey
$objInParams.sValueName = $strRegValueName

;-- Execute the GetStringValue method to read value in the registry --
$objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

;-- get the sValue parameter that is returned by GetStringValue method --
$RegValue = $objOutParams.sValue

". SubKeyName : " $objInParams.sSubKeyName ?
". ValueName  : " $objInParams.sValueName ?
". value      : " $RegValue ?
?


;-------------------------------------------------------------------------------
; example to read a value in the registry with WMI with GetDwordValue method
;
; uint32 GetDwordValue(
;   [in]  uint32 hDefKey = HKEY_LOCAL_MACHINE,
;   [in]  string sSubKeyName,
;   [in]  string sValueName,
;   [out] uint32 uValue
; )
;-------------------------------------------------------------------------------
"Second read (REG_DWORD)" ?
$strRegValueName = "WbemAdapStatus"
$wmimethod = "GetDwordValue"

$objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
$objInParams.hDefKey = $HKEY_LOCAL_MACHINE
$objInParams.sSubKeyName = $strRegKey
$objInParams.sValueName = $strRegValueName

;-- Execute the GetDwordValue method to read value in the registry --
$objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

;-- get the uValue parameter that is returned by GetDwordValue method --
$RegValue = $objOutParams.uValue

". SubKeyName : " $objInParams.sSubKeyName ?
". ValueName  : " $objInParams.sValueName ?
". value      : " $RegValue ?
?


;-------------------------------------------------------------------------------
; example to read a value in the registry with WMI with GetBinaryValue method
;
; uint32 GetBinaryValue(
;   [in]  uint32 hDefKey = HKEY_LOCAL_MACHINE,
;   [in]  string sSubKeyName,
;   [in]  string sValueName,
;   [out] uint8  uValue[]
; )
;-------------------------------------------------------------------------------
"Third read (REG_BINARY)" ?
$strRegValueName = "WbemAdapFileSignature"
$wmimethod = "GetBinaryValue"

$objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
$objInParams.hDefKey = $HKEY_LOCAL_MACHINE
$objInParams.sSubKeyName = $strRegKey
$objInParams.sValueName = $strRegValueName

;-- Execute the GetDwordValue method to read value in the registry --
$objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

;-- get the uValue parameter that is returned by GetDwordValue method --
$RegValue = $objOutParams.uValue

". SubKeyName : " $objInParams.sSubKeyName ?
". ValueName  : " $objInParams.sValueName ?
". value      : "
for each $byte in $regvalue
	DecToHex($byte) " "
next
?
In this code, i read locally because $strComputer = "."
To test remotely, just change the line $strComputer = "<remote computername>"

For more information, look at Microsoft MSDN Web Site (WMI StdRegProv)and see all methods GetXxxValue.
_________________________
Christophe

Top
#209306 - 2014-07-30 01:16 AM Re: Can functions have an OUT parameter [Re: ChristopheM]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Nice Christophe. I'm not sure where I would use this in my enviroment, but that's some nice code you got there.
Top
#209307 - 2014-07-30 09:15 AM Re: Can functions have an OUT parameter [Re: Allen]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
I have made a function using WMI to read value in the registry.
Here is the code with examples.
 Code:
break on

$=SetOption( "Explicit", "ON" )
$=SetOption( "NoVarsInStrings", "ON" )
$=SetOption( "NoMacrosInStrings", "ON" )

global $HKEY_CLASSES_ROOT, $HKEY_CURRENT_USER, $HKEY_LOCAL_MACHINE
global $HKEY_USERS, $HKEY_CURRENT_CONFIG
$HKEY_CLASSES_ROOT    = (CDBL(2147483647)+1)        ; &80000000
$HKEY_CURRENT_USER    = (CDBL(2147483647)+2)        ; &80000001
$HKEY_LOCAL_MACHINE   = (CDBL(2147483647)+3)        ; &80000002
$HKEY_USERS           = (CDBL(2147483647)+4)        ; &80000003
$HKEY_CURRENT_CONFIG  = (CDBL(2147483647)+6)        ; &80000005

global $strComputer, $strRegKey, $strRegValueName, $strRegValueType, $RegValue, $tmp
$strComputer = "."

?
";--------------------------------------" ?
"; example 1 : REG_SZ" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "Open"
$strRegValueType = "REG_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?

$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?


?
";--------------------------------------" ?
"; example 2 : REG_DWORD" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "WbemAdapStatus"
$strRegValueType = "REG_DWORD"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?


?
";--------------------------------------" ?
"; example 3 : REG_BINARY" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "WbemAdapFileSignature"
$strRegValueType = "REG_BINARY"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    DecToHex($tmp) " "
next
?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    DecToHex($tmp) " "
next
?


?
";--------------------------------------" ?
"; example 4 : REG_EXPAND_SZ" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Parameters"
$strRegValueName = "DataBasePath"
$strRegValueType = "REG_EXPAND_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?


?
";--------------------------------------" ?
"; example 5 : REG_MULTI_SZ" ?
";--------------------------------------" ?
$strRegKey = "SYSTEM\CurrentControlSet\Services\Eventlog\Security"
$strRegValueName = "Sources"
$strRegValueType = "REG_MULTI_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    ? "  . " $tmp
next
?

$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    ? "  . " $tmp
next
?

exit 0



;-------------------------------------------------------------------------------
;  function to read value in the registry (locally or remotely)
;-------------------------------------------------------------------------------
function ReadRegistryValue( $Hive, $strRegkey, $strRegValueName, $strRegType, optional $strComputer )
    dim $namespace, $objWMIService, $objRegistry, $hDefKey, $wmiclass, $wmimethod
    dim $objInParams, $objOutParams

    select
        case vartype($hive)=8
            select
                case $Hive = "HKEY_CLASSES_ROOT"    $hDefKey = (CDBL(2147483647)+1)    ; &80000000
                case $Hive = "HKCR"                 $hDefKey = (CDBL(2147483647)+1)    ; &80000000

                case $Hive = "HKEY_CURRENT_USER"    $hDefKey = (CDBL(2147483647)+2)    ; &80000001
                case $Hive = "HKCU"                 $hDefKey = (CDBL(2147483647)+2)    ; &80000001

                case $Hive = "HKEY_LOCAL_MACHINE"   $hDefKey = (CDBL(2147483647)+3)    ; &80000002
                case $Hive = "HKLM"                 $hDefKey = (CDBL(2147483647)+3)    ; &80000002

                case $Hive = "HKEY_USERS"           $hDefKey = (CDBL(2147483647)+4)    ; &80000003
                case $Hive = "HKUS"                 $hDefKey = (CDBL(2147483647)+4)    ; &80000003

                case $Hive = "HKEY_CURRENT_CONFIG"  $hDefKey = (CDBL(2147483647)+6)    ; &80000005
                case $Hive = "HKCC"                 $hDefKey = (CDBL(2147483647)+6)    ; &80000005

                case 1                              exit -1
            endselect

        case vartype($hive)=5
            $hDefKey = $hive

        case 1                                      exit -1
    endselect

    select
        case $strRegType = "REG_SZ"          $wmimethod = "GetStringValue"
        case $strRegType = "REG_EXPAND_SZ"   $wmimethod = "GetExpandedStringValue"
        case $strRegType = "REG_MULTI_SZ"    $wmimethod = "GetMultiStringValue"
        case $strRegType = "REG_DWORD"       $wmimethod = "GetDwordValue"
        case $strRegType = "REG_QWORD"       $wmimethod = "GetQwordValue"
        case $strRegType = "REG_BINARY"      $wmimethod = "GetBinaryValue"

        case 1                               exit -2
    endselect

    ;-- Connect to WMI --
    $namespace = "root\default"
    $objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate,(debug,security)}!\\'+$strComputer+'\'+$namespace)
    $wmiclass = "StdRegProv"
    $objRegistry = $objWMIService.Get($wmiclass)

    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    ;-- get the Value parameter that is returned by method --
    select
        case $strRegType = "REG_SZ"         $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_EXPAND_SZ"  $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_MULTI_SZ"   $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_DWORD"      $ReadRegistryValue = $objOutParams.uValue
        case $strRegType = "REG_QWORD"      $ReadRegistryValue = $objOutParams.uValue
        case $strRegType = "REG_BINARY"     $ReadRegistryValue = $objOutParams.uValue
    endselect
endfunction

it is possible to find dynamically the type of a value but with wmi, it is not optimized. I should enum all values of a key to get value name AND type. Then find the requested value in the array of value names and the read type in the array of value type.
may be in a new version of the function (like ReadRegistryValueEx) !!!
_________________________
Christophe

Top
#209308 - 2014-07-30 01:03 PM Re: Can functions have an OUT parameter [Re: ChristopheM]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
If someone would write a brief description as to where this could be used, I'd certainly document it in the Script Vault. Look at other articles in the vault to get an idea of what would be appropriate.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209309 - 2014-07-30 05:51 PM Re: Can functions have an OUT parameter [Re: Glenn Barnas]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
 Quote:
it is possible to find dynamically the type of a value but with wmi, it is not optimized


Could you post what you have. I was looking for this but it all comes back to the Out parameters again.

Top
#209310 - 2014-07-30 07:11 PM Re: Can functions have an OUT parameter [Re: Allen]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
Allen,

Here is the code with EnumValues. I implement this in the ReadRegistryValueEx function.
I also have updated the examples.
 Code:
break on

$=SetOption( "Explicit", "ON" )
$=SetOption( "NoVarsInStrings", "ON" )
$=SetOption( "NoMacrosInStrings", "ON" )

global $HKEY_CLASSES_ROOT, $HKEY_CURRENT_USER, $HKEY_LOCAL_MACHINE
global $HKEY_USERS, $HKEY_CURRENT_CONFIG
$HKEY_CLASSES_ROOT    = (CDBL(2147483647)+1)        ; &80000000
$HKEY_CURRENT_USER    = (CDBL(2147483647)+2)        ; &80000001
$HKEY_LOCAL_MACHINE   = (CDBL(2147483647)+3)        ; &80000002
$HKEY_USERS           = (CDBL(2147483647)+4)        ; &80000003
$HKEY_CURRENT_CONFIG  = (CDBL(2147483647)+6)        ; &80000005

global $strComputer, $strRegKey, $strRegValueName, $strRegValueType, $RegValue, $tmp
$strComputer = "."

?
";--------------------------------------" ?
"; example 1 : REG_SZ" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "Open"
$strRegValueType = "REG_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValueEx( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strComputer )
". value      : " $RegValue ?

?
";--------------------------------------" ?
"; example 2 : REG_DWORD" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "WbemAdapStatus"
$strRegValueType = "REG_DWORD"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValueEx( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strComputer )
". value      : " $RegValue ?

?
";--------------------------------------" ?
"; example 3 : REG_BINARY" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Performance"
$strRegValueName = "WbemAdapFileSignature"
$strRegValueType = "REG_BINARY"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    DecToHex($tmp) " "
next
?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    DecToHex($tmp) " "
next
?
$RegValue = ReadRegistryValueEx( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strComputer )
". value      : "
for each $tmp in $regvalue
    DecToHex($tmp) " "
next
?


?
";--------------------------------------" ?
"; example 4 : REG_EXPAND_SZ" ?
";--------------------------------------" ?
$strRegKey = "System\CurrentControlSet\Services\TCPIP\Parameters"
$strRegValueName = "DataBasePath"
$strRegValueType = "REG_EXPAND_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : " $RegValue ?
$RegValue = ReadRegistryValueEx( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strComputer )
". value      : " $RegValue ?


?
";--------------------------------------" ?
"; example 5 : REG_MULTI_SZ" ?
";--------------------------------------" ?
$strRegKey = "SYSTEM\CurrentControlSet\Services\Eventlog\Security"
$strRegValueName = "Sources"
$strRegValueType = "REG_MULTI_SZ"

". SubKeyName : " $strRegKey ?
". ValueName  : " $strRegValueName ?

$RegValue = ReadRegistryValue( "HKLM", $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    ? "  . " $tmp
next
?

$RegValue = ReadRegistryValue( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strRegValueType, $strComputer )
". value      : "
for each $tmp in $regvalue
    ? "  . " $tmp
next
?

$RegValue = ReadRegistryValueEx( $HKEY_LOCAL_MACHINE, $strRegkey, $strRegValueName, $strComputer )
". value      : "
for each $tmp in $regvalue
    ? "  . " $tmp
next
?

exit 0



;-------------------------------------------------------------------------------
;  function to read value in the registry (locally or remotely)
;-------------------------------------------------------------------------------
function ReadRegistryValue( $Hive, $strRegkey, $strRegValueName, $strRegType, optional $strComputer )
    dim $namespace, $objWMIService, $objRegistry, $hDefKey, $wmiclass, $wmimethod
    dim $objInParams, $objOutParams

    $ReadRegistryValue = ""
    select
        case vartype($hive)=8
            select
                case $Hive = "HKEY_CLASSES_ROOT"    $hDefKey = (CDBL(2147483647)+1)    ; &80000000
                case $Hive = "HKCR"                 $hDefKey = (CDBL(2147483647)+1)    ; &80000000

                case $Hive = "HKEY_CURRENT_USER"    $hDefKey = (CDBL(2147483647)+2)    ; &80000001
                case $Hive = "HKCU"                 $hDefKey = (CDBL(2147483647)+2)    ; &80000001

                case $Hive = "HKEY_LOCAL_MACHINE"   $hDefKey = (CDBL(2147483647)+3)    ; &80000002
                case $Hive = "HKLM"                 $hDefKey = (CDBL(2147483647)+3)    ; &80000002

                case $Hive = "HKEY_USERS"           $hDefKey = (CDBL(2147483647)+4)    ; &80000003
                case $Hive = "HKUS"                 $hDefKey = (CDBL(2147483647)+4)    ; &80000003

                case $Hive = "HKEY_CURRENT_CONFIG"  $hDefKey = (CDBL(2147483647)+6)    ; &80000005
                case $Hive = "HKCC"                 $hDefKey = (CDBL(2147483647)+6)    ; &80000005

                case 1                              exit -1
            endselect

        case vartype($hive)=5
            $hDefKey = $hive

        case 1                                      exit -1
    endselect

    select
        case $strRegType = "REG_SZ"          $wmimethod = "GetStringValue"
        case $strRegType = "REG_EXPAND_SZ"   $wmimethod = "GetExpandedStringValue"
        case $strRegType = "REG_MULTI_SZ"    $wmimethod = "GetMultiStringValue"
        case $strRegType = "REG_DWORD"       $wmimethod = "GetDwordValue"
        case $strRegType = "REG_QWORD"       $wmimethod = "GetQwordValue"
        case $strRegType = "REG_BINARY"      $wmimethod = "GetBinaryValue"

        case 1                               exit -2
    endselect

    ;-- Connect to WMI --
    $namespace = "root\default"
    $objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate,(debug,security)}!\\'+$strComputer+'\'+$namespace)
    $wmiclass = "StdRegProv"
    $objRegistry = $objWMIService.Get($wmiclass)

    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    if $objOutParams.ReturnValue <> 0
        exit $objOutParams.ReturnValue
    endif

    ;-- get the Value parameter that is returned by method --
    select
        case $strRegType = "REG_SZ"         $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_EXPAND_SZ"  $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_MULTI_SZ"   $ReadRegistryValue = $objOutParams.sValue
        case $strRegType = "REG_DWORD"      $ReadRegistryValue = $objOutParams.uValue
        case $strRegType = "REG_QWORD"      $ReadRegistryValue = $objOutParams.uValue
        case $strRegType = "REG_BINARY"     $ReadRegistryValue = $objOutParams.uValue
    endselect

    exit 0
endfunction

;-------------------------------------------------------------------------------
;  function to read value in the registry (locally or remotely)
;-------------------------------------------------------------------------------
function ReadRegistryValueEx( $Hive, $strRegkey, $strRegValueName, optional $strComputer )
    dim $namespace, $objWMIService, $objRegistry
    dim $hDefKey, $arrValuesName, $arrValuesType, $RegType, $wmiclass, $wmimethod
    dim $objInParams, $objOutParams

    $ReadRegistryValueEx = ""
    select
        case vartype($hive)=8
            select
                case $Hive = "HKEY_CLASSES_ROOT"    $hDefKey = (CDBL(2147483647)+1)    ; &80000000
                case $Hive = "HKCR"                 $hDefKey = (CDBL(2147483647)+1)    ; &80000000

                case $Hive = "HKEY_CURRENT_USER"    $hDefKey = (CDBL(2147483647)+2)    ; &80000001
                case $Hive = "HKCU"                 $hDefKey = (CDBL(2147483647)+2)    ; &80000001

                case $Hive = "HKEY_LOCAL_MACHINE"   $hDefKey = (CDBL(2147483647)+3)    ; &80000002
                case $Hive = "HKLM"                 $hDefKey = (CDBL(2147483647)+3)    ; &80000002

                case $Hive = "HKEY_USERS"           $hDefKey = (CDBL(2147483647)+4)    ; &80000003
                case $Hive = "HKUS"                 $hDefKey = (CDBL(2147483647)+4)    ; &80000003

                case $Hive = "HKEY_CURRENT_CONFIG"  $hDefKey = (CDBL(2147483647)+6)    ; &80000005
                case $Hive = "HKCC"                 $hDefKey = (CDBL(2147483647)+6)    ; &80000005

                case 1                              exit -1
            endselect

        case vartype($hive)=5
            $hDefKey = $hive

        case 1                                      exit -1
    endselect

    ;-- Connect to WMI --
    $namespace = "root\default"
    $objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate,(debug,security)}!\\'+$strComputer+'\'+$namespace)
    $wmiclass = "StdRegProv"
    $objRegistry = $objWMIService.Get($wmiclass)

    $wmimethod = "EnumValues"
    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    if $objOutParams.ReturnValue <> 0
        exit -3
    endif

    ;-- search $strRegValueName in the array of value name returned --
    $arrValuesName = $objOutParams.sNames
    $arrValuesType = $objOutParams.Types

    dim $i, $imax, $found
    $found = 0
    $i = -1
    $imax = UBound($arrValuesName)
    while  $i < $imax
        $i = $i + 1
        if $strRegValueName = $arrValuesName[$i]
            $RegType = $arrValuesType[$i]

            select
                case $RegType = 1   $wmimethod = "GetStringValue"
                case $RegType = 2   $wmimethod = "GetExpandedStringValue"
                case $RegType = 7   $wmimethod = "GetMultiStringValue"
                case $RegType = 4   $wmimethod = "GetDwordValue"
                case $RegType = 11  $wmimethod = "GetQwordValue"
                case $RegType = 3   $wmimethod = "GetBinaryValue"
            endselect
            $i = $imax
            $found = 1
        endif
    loop

    if not $found
        exit -4
    endif


    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    if $objOutParams.ReturnValue <> 0
        exit $objOutParams.ReturnValue
    endif

    ;-- get the Value parameter that is returned by method --
    select
        case $RegType = 1  $ReadRegistryValueEx = $objOutParams.sValue
        case $RegType = 2  $ReadRegistryValueEx = $objOutParams.sValue
        case $RegType = 7  $ReadRegistryValueEx = $objOutParams.sValue
        case $RegType = 4  $ReadRegistryValueEx = $objOutParams.uValue
        case $RegType = 11 $ReadRegistryValueEx = $objOutParams.uValue
        case $RegType = 3  $ReadRegistryValueEx = $objOutParams.uValue
    endselect

    exit 0
endfunction
I hope this will be helpful for you.
_________________________
Christophe

Top
#209314 - 2014-07-30 09:44 PM Re: Can functions have an OUT parameter [Re: ChristopheM]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Here's a modified version that works exactly like the built in ReadValue(). (Not throughly tested)

 Code:
function WMIReadValue($strRegkey, $strRegValueName )
    dim $namespace, $objWMIService, $objRegistry
    dim $hDefKey, $arrValuesName, $arrValuesType, $RegType, $wmiclass, $wmimethod
    dim $objInParams, $objOutParams, $Parts, $counter, $tempkey
	
    $Parts=Split($strRegKey,"\")
    if $parts[0]="" and $parts[1]=""
      $strComputer=$parts[2]
      $counter=3
    else
      $strComputer= "."
    endif
    
    $hive=$parts[0+$counter]
    
    for $i=$counter+1 to ubound($parts)
      $tempkey=$tempkey + $parts[$i] + "\"
    next
    $strRegkey=left($tempkey,-1)

    $WMIReadValue = ""
    select
        case vartype($hive)=8
            select
                case $Hive = "HKEY_CLASSES_ROOT"    $hDefKey = (CDBL(2147483647)+1)    ; &80000000
                case $Hive = "HKCR"                 $hDefKey = (CDBL(2147483647)+1)    ; &80000000

                case $Hive = "HKEY_CURRENT_USER"    $hDefKey = (CDBL(2147483647)+2)    ; &80000001
                case $Hive = "HKCU"                 $hDefKey = (CDBL(2147483647)+2)    ; &80000001

                case $Hive = "HKEY_LOCAL_MACHINE"   $hDefKey = (CDBL(2147483647)+3)    ; &80000002
                case $Hive = "HKLM"                 $hDefKey = (CDBL(2147483647)+3)    ; &80000002

                case $Hive = "HKEY_USERS"           $hDefKey = (CDBL(2147483647)+4)    ; &80000003
                case $Hive = "HKUS"                 $hDefKey = (CDBL(2147483647)+4)    ; &80000003

                case $Hive = "HKEY_CURRENT_CONFIG"  $hDefKey = (CDBL(2147483647)+6)    ; &80000005
                case $Hive = "HKCC"                 $hDefKey = (CDBL(2147483647)+6)    ; &80000005

                case 1                              exit -1
            endselect

        case vartype($hive)=5
            $hDefKey = $hive

        case 1                                      exit -1
    endselect

    ;-- Connect to WMI --
    $namespace = "root\default"
    $objWMIService = GetObject('winmgmts:{impersonationLevel=impersonate,(debug,security)}!\\'+$strComputer+'\'+$namespace)
    $wmiclass = "StdRegProv"
    $objRegistry = $objWMIService.Get($wmiclass)

    $wmimethod = "EnumValues"
    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    if $objOutParams.ReturnValue <> 0
        exit -3
    endif

    ;-- search $strRegValueName in the array of value name returned --
    $arrValuesName = $objOutParams.sNames
    $arrValuesType = $objOutParams.Types

    dim $i, $imax, $found
    $found = 0
    $i = -1
    $imax = UBound($arrValuesName)
    while  $i < $imax
        $i = $i + 1
        if $strRegValueName = $arrValuesName[$i]
            $RegType = $arrValuesType[$i]

            select
                case $RegType = 1   $wmimethod = "GetStringValue"
                case $RegType = 2   $wmimethod = "GetExpandedStringValue"
                case $RegType = 7   $wmimethod = "GetMultiStringValue"
                case $RegType = 4   $wmimethod = "GetDwordValue"
                case $RegType = 11  $wmimethod = "GetQwordValue"
                case $RegType = 3   $wmimethod = "GetBinaryValue"
            endselect
            $i = $imax
            $found = 1
        endif
    loop

    if not $found
        exit -4
    endif


    $objInParams = $objRegistry.Methods_($wmimethod).InParameters.SpawnInstance_
    $objInParams.hDefKey = $hDefKey
    $objInParams.sSubKeyName = $strRegKey
    $objInParams.sValueName = $strRegValueName

    ;-- Execute the method to read value in the registry --
    $objOutParams = $objWMIService.ExecMethod( $wmiclass, $wmimethod, $objInParams )

    if $objOutParams.ReturnValue <> 0
        exit $objOutParams.ReturnValue
    endif

    ;-- get the Value parameter that is returned by method --
    select
        case $RegType = 1  $WMIReadValue = $objOutParams.sValue
        case $RegType = 2  $WMIReadValue = $objOutParams.sValue
        case $RegType = 7  $WMIReadValue = $objOutParams.sValue
        case $RegType = 4  $WMIReadValue = $objOutParams.uValue
        case $RegType = 11 $WMIReadValue = $objOutParams.uValue
        case $RegType = 3  $WMIReadValue = $objOutParams.uValue
    endselect

    exit 0
endfunction

Top
#209326 - 2014-07-31 08:51 PM Re: Can functions have an OUT parameter [Re: Allen]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
I'm guessing here that you're using Windows 7 and Remote Registry is disabled by default. You can enable it on the local computer while logged in with Admin rights or you can enable it with a policy. Then you can use KiXtart to manage registry entries pretty easily. Though cool that this discussion and coding has been posted.


http://technet.microsoft.com/en-us/library/cc754820.aspx

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 758 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.064 seconds in which 0.039 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org