ComatePlus GetDwordValue Registry

Just starting out? Need help? Post your questions and find answers here.
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

ComatePlus GetDwordValue Registry

Post 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.
Windows 10 Pro x64
PureBasic 6.20 x64
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: ComatePlus GetDwordValue Registry

Post 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
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
tatanas
Enthusiast
Enthusiast
Posts: 260
Joined: Wed Nov 06, 2019 10:28 am
Location: France

Re: ComatePlus GetDwordValue Registry

Post 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


Windows 10 Pro x64
PureBasic 6.20 x64
Post Reply