Page 1 of 1

ComatePlus GetDwordValue Registry

Posted: Fri May 31, 2024 12:11 pm
by tatanas
Hi,

I'm trying to retrieve some values from registry (remotely) but I can't get Dword value (String is ok) :

Code: Select all

EnableExplicit

XIncludeFile "COMatePLUS.pbi"


; retourne le contenu de la valeur de type Reg_SZ fournie en paramètre
Procedure.s GetStringValue(ObjReg.COMateObject, HKey.l, KeyPath.s, ValueName.s)
	Protected varValue.VARIANT
	Protected Result.s = ""

	If ObjReg\Invoke("GetStringValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@varValue) + " AS variant BYREF)") = #S_OK
      If varValue\vt = #VT_BSTR And varValue\bstrVal ; 8 = #VT_BSTR, 1 = #VT_NULL
         Result = PeekS(varValue\bstrVal, -1, #PB_Unicode)   
         SysFreeString_(varValue\bstrVal)
      EndIf
	Else
		Debug "GetStringValue error : " + COMate_GetLastErrorDescription()
		Result = "error"
	EndIf

	ProcedureReturn Result
EndProcedure


; retourne le contenu de la valeur de type Reg_DWORD fournie en paramètre
Procedure.s GetDWORDValue(ObjReg.COMateObject, HKey.l, KeyPath.s, ValueName.s)
	Protected Value
	Protected Result.s = ""
	Protected Ret_GetDWORDValue

	; GetIntegerProperty() permet de retrouver la valeur de retour de la fonction appelée, ici GetDWORDValue() ce que ne permet pas Invoke()

; 	ObjReg\Invoke("GetDWORDValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@Value) + " BYREF)")
	Ret_GetDWORDValue = ObjReg\GetIntegerProperty("GetDWORDValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@Value) + " BYREF)")

	If Ret_GetDWORDValue = 0 ; valeur trouvée
		Debug "Trouvé"
		Result = Str(Value)
	ElseIf Ret_GetDWORDValue = 1 ; valeur inexistante
		Result = "Erreur registre"
	ElseIf Ret_GetDWORDValue = 2 ; clé inexistante
		Result = "Erreur registre"
	Else
		Debug "GetDWORDValue error : " + COMate_GetLastErrorDescription()
		Result = "Erreur"
	EndIf

	ProcedureReturn Result
EndProcedure



Define.s Hostname, login, password
Define.COMateObject objLocator, objService, objReg
Define hkey.l

Hostname = "localhost"
login = ""
password = ""
hkey = #HKEY_LOCAL_MACHINE


objLocator = COMate_CreateObject("WbemScripting.SWbemLocator")
If objLocator

   objService = objLocator\GetObjectProperty("ConnectServer(" + "'" + Hostname + "', '" + "root\default" + "', '" + Login + "', '" + Password + "')")
   If objService

      objReg = objService\GetObjectProperty("Get('StdRegProv')")
      If objReg

			Debug GetDWORDValue(objReg, hkey, "SYSTEM\CurrentControlSet\Control\Windows", "CSDBuildNumber") ; <====== WRONG VALUE

			Debug GetStringValue(objReg, hkey, "SYSTEM\CurrentControlSet\Control\Windows", "SystemDirectory") ; <====== OK

			objReg\Release()
		Else
			Debug "objReg error"
		EndIf

		objService\Release()
	Else
		Debug "objService error"
	EndIf
	objLocator\Release()
Else
	Debug "objLocator error"
EndIf


I'm note 100% sure but it may be since PB6.10.

Thanks for your time.

Re: ComatePlus GetDwordValue Registry

Posted: Fri May 31, 2024 12:41 pm
by boddhi
Hello,
tatanas wrote: I'm trying to retrieve some values from registry (remotely) but I can't get Dword value (String is ok) :
DWord is .l (long) type.
On Windows, to handle Registry, you can use specific API instruction set (RegKeyXXX) wiith these informations

Re: ComatePlus GetDwordValue Registry

Posted: Fri May 31, 2024 1:03 pm
by tatanas
I'm sorry, but I'm not looking for another way to get the registry value. ComatePlus is fine and I'm already using it with WMI request to get some informations.

EDIT : Finaly the fix was easy. Just add "as long" before the BYREF in GetDwordValue call.

Code: Select all

EnableExplicit

XIncludeFile "COMatePLUS.pbi"


; retourne le contenu de la valeur de type Reg_SZ fournie en paramètre
Procedure.s GetStringValue(ObjReg.COMateObject, HKey.l, KeyPath.s, ValueName.s)
	Protected varValue.VARIANT
	Protected Result.s = ""

	If ObjReg\Invoke("GetStringValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@varValue) + " AS variant BYREF)") = #S_OK
      If varValue\vt = #VT_BSTR And varValue\bstrVal ; 8 = #VT_BSTR, 1 = #VT_NULL
         Result = PeekS(varValue\bstrVal, -1, #PB_Unicode)   
         SysFreeString_(varValue\bstrVal)
      EndIf
	Else
		Debug "GetStringValue error : " + COMate_GetLastErrorDescription()
		Result = "error"
	EndIf

	ProcedureReturn Result
EndProcedure


; retourne le contenu de la valeur de type Reg_DWORD fournie en paramètre
Procedure.s GetDWORDValue(ObjReg.COMateObject, HKey.l, KeyPath.s, ValueName.s)
	Protected Value
	Protected Result.s = ""
	Protected Ret_GetDWORDValue

	; GetIntegerProperty() permet de retrouver la valeur de retour de la fonction appelée, ici GetDWORDValue() ce que ne permet pas Invoke()

; 	ObjReg\Invoke("GetDWORDValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@Value) + " BYREF)")
	Ret_GetDWORDValue = ObjReg\GetIntegerProperty("GetDWORDValue(" + Str(HKey) + ", '" + KeyPath + "', '" + ValueName + "', " + Str(@Value) + " as long BYREF)")

	If Ret_GetDWORDValue = 0 ; valeur trouvée
		Debug "Trouvé"
		Result = Str(Value)
	ElseIf Ret_GetDWORDValue = 1 ; valeur inexistante
		Result = "Erreur registre"
	ElseIf Ret_GetDWORDValue = 2 ; clé inexistante
		Result = "Erreur registre"
	Else
		Debug "GetDWORDValue error : " + COMate_GetLastErrorDescription()
		Result = "Erreur"
	EndIf

	ProcedureReturn Result
EndProcedure



Define.s Hostname, login, password
Define.COMateObject objLocator, objService, objReg
Define hkey.l

Hostname = "localhost"
login = ""
password = ""
hkey = #HKEY_LOCAL_MACHINE


objLocator = COMate_CreateObject("WbemScripting.SWbemLocator")
If objLocator

   objService = objLocator\GetObjectProperty("ConnectServer(" + "'" + Hostname + "', '" + "root\default" + "', '" + Login + "', '" + Password + "')")
   If objService

      objReg = objService\GetObjectProperty("Get('StdRegProv')")
      If objReg

			Debug GetDWORDValue(objReg, hkey, "SYSTEM\CurrentControlSet\Control\Windows", "CSDBuildNumber") ; <====== OK

			Debug GetStringValue(objReg, hkey, "SYSTEM\CurrentControlSet\Control\Windows", "SystemDirectory") ; <====== OK

			objReg\Release()
		Else
			Debug "objReg error"
		EndIf

		objService\Release()
	Else
		Debug "objService error"
	EndIf
	objLocator\Release()
Else
	Debug "objLocator error"
EndIf