Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a Registry Value Using Fox v5.0 2

Status
Not open for further replies.

PBREP

MIS
Mar 14, 2003
75
US
Hi Guys:

Here I have to use "VFP 5.0" to code in.

A)What am I atepeting to do:

Change the value from 2 to 3.
Target: Registry Path = HKEY_LOCAL_MACHINE\SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable
Registry Key: Value Name = CustomerUpgradable

LOGIC:
If the Value Data = 2 Then
Value Data = '3' Else
Quit
EndElse

Or I can overwrite the value (as shown below) which might be better in case the value was other than 2:

The following sniplet I'm generating an error during compile time - "Too Few Arguments"

Thanks Guys!
~PM

SNIPLET:
lnResult = RegSetValueEx( ;
m.lhKey, ;
"CustomerUpgradable", ;
REG_OPTION_RESERVED, ;
REG_SZ, ;
"3")

FULL CODE BELOW:

*// PURPOSE:
*// Creates a value called
*// CustomerUpgradable under the HHKEY_LOCAL_MACHINE*// SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable, and sets
*// the value to "3"

#include registry.h

* Declare the API functions:

declare integer RegCreateKeyEx in Win32API ;
integer nhKey, ;
string @cSubKey, ;
integer nReserved, ;
string cKeyClass, ;
integer nOptions, ;
integer nSecurityAccessMask, ;
integer nSecurityAttributes, ;
integer @nKeyHandle, ;
integer @nDisposition

declare integer RegSetValueEx in Win32API ;
integer nKeyHandle, ;
string cValueName, ;
integer nReserved, ;
integer nType, ;
string cBuffer, ;
integer nBufferSize

declare integer RegCloseKey in Win32API ;
integer nKeyHandle

* Open the key:

local lhKey, lnDisposition, lnResult
store 0 to lhKey, lnDisposition

lnResult = RegCreateKeyEx( ;
HKEY_LOCAL_MACHINE, ;
'SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable', ;
REG_OPTION_RESERVED, ;
REG_CLASS_DEFAULT, ;
REG_OPTION_NON_VOLATILE, ;
KEY_ALL_ACCESS, ;
REG_SECURITY_DEFAULT, ;
@lhKey, ;
@lnDisposition )

* Writes the new value:

lnResult = RegSetValueEx( ;
m.lhKey, ;
"CustomerUpgradable", ;
REG_OPTION_RESERVED, ;
REG_SZ, ;
"3")

=RegCloseKey( m.lhKey )


 
Thanks Mike.

Wow...Windows Script is a breeze considering for what I was trying to accomplished.

~PM :)
WSHShell = CreateObject("WScript.Shell")
*//Edit Reg Value
WSHShell.Popup( "Set value HKEY_LOCAL_MACHINE\SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable to REG_SZ 3")
WSHShell.RegWrite ("HKEY_LOCAL_MACHINE\SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable", 3)
 
Something to add: Some network and system administrators unload Windows Scripting Host (WSH) on their machines because of the viruses using this tool. So if any of you have another way in doing this please post it. It would benifit us all.

Thanks,
~PM
 

The second link shows you how to do it with API calls, which does not require Windows Script.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hi Gang:

Well I tried it the Foxpro API way, no compile errors but nothing happens.

Any suggestions would be much apreciated.

Thanks,
~PM

*// PURPOSE:
*// Creates a value called in
*// CustomerUpgradable under the HHKEY_LOCAL_MACHINE*// SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable, and sets
*// the value to "3"

#include registry.h

* Declare the API functions:

declare integer RegSetValueEx in Win32API ;
integer nKeyHandle, ;
string cValueName, ;
integer nReserved, ;
integer nType, ;
string cBuffer, ;
integer nBufferSize

declare integer RegCloseKey in Win32API ;
integer nKeyHandle

* Open the key:

local lhKey, lnDisposition, lnResult
store 0 to lhKey, lnDisposition

* Writes the new value:

lcValueToSet = "3"+chr(0) && null terminate value to set
lnValueSize = len(m.lcValueToSet)

lnResult = RegSetValueEx( ;
m.lhKey, ;
"HKEY_LOCAL_MACHINE, ;
SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable", ;
REG_OPTION_RESERVED, ;
REG_SZ, ;
m.lcValueToSet, m.lnValueSize)
=RegCloseKey( m.lhKey )
 
How can I get a registry key value? If someone have a sample, please send it to me!, thanx!!!
 
WalterJolon

If you choose to post a question in an already existing thread, rather than creating a new thread for your question, you may want to look at the suggestions made in the thread. If you look at the suggestions I made, the first link points you to a solution that shows you how to Read, write and delete a registry key. But next time you might want to start your own thread if you have a question.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
OK Guys:

The Window Scripting works as a breeze that Mike showed me but has some limitation as mentioned above. The code below does not error but does not do anything. I didsome research that you all pointed out and found on my own, I'm perplexed )and I know it must be something I overlooked). What am I missing.

PS - Where's Steve B. ? :)

Thanks,
~PM

&& Latest code
*// PURPOSE:
*// Creates a value called in
*// CustomerUpgradable under the HHKEY_LOCAL_MACHINE*// SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable, and sets
*// the value to "3"

#include registry.h

* Declare the API functions:

declare integer RegSetValueEx in Win32API ;
integer nKeyHandle, ;
string cValueName, ;
integer nReserved, ;
integer nType, ;
string cBuffer, ;
integer nBufferSize

declare integer RegCloseKey in Win32API ;
integer nKeyHandle

DECLARE INTEGER RegOpenKeyEx IN Win32API ;
local lhKey, lnDisposition, lnResult
store 0 to lhKey, lnDisposition
LOCAL nErrCode && Error Code returned from Registry functions
LOCAL nKeyHandle && Handle to Key that is opened in the Registry

* Open the key:

m.lhKey = "HKEY_LOCAL_MACHINE\SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable"
nErrCode = RegOpenKey(HKEY_LOCAL_MACHINE, m.lhKey, @nKeyHandle)
* If the error code isn't 0, then the key doesn't exist or can't be opened.
IF (nErrCode # 0) THEN
RETURN .F.
ENDIF

* Writes the new value:

lcValueToSet = "3"+chr(0) && null terminate value to set
lnValueSize = len(m.lcValueToSet)

lnResult = RegSetValueEx( ;
m.lhKey, ;
"HKEY_LOCAL_MACHINE, ;
SOFTWARE\PitneyBowes\Ascent\CustomerUpgradable", ;
REG_OPTION_RESERVED, ;
REG_SZ, ;
m.lcValueToSet, m.lnValueSize)
=RegCloseKey( m.lhKey )
 
BPREP

This seems to be a wrong API declaration. The extra semicolon.

Code:
DECLARE INTEGER RegOpenKeyEx IN Win32API[COLOR=red][b] ;[/b][/color]
local lhKey, lnDisposition, lnResult

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
BPREP

I also get an error with this line

Code:
nErrCode = RegOpenKey(HKEY_LOCAL_MACHINE, m.lhKey, @nKeyHandle)

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
PBREP, I think that you're trying to set a value to a registry key value, but in my tests I found out that you have to create the key again and set the new value!, that way works to me!

I use this:

* DECLARACION DE DLLs
DO prcDlls

CreaKey("SOFTWARE\WLT_APPS")
GetKeyValue("WltName")

Wait WINDOW a

*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*
*!* Función Para Crear y Asignar Valores *!*
*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*

Function CreaKey(vKey)
#DEFINE HKEY_LOCAL_MACHINE 2147483650 && (HKEY) 0x80000002
#DEFINE SECURITY_ACCESS_MASK 983103 && SAM value KEY_ALL_ACCESS

Local Result, Display, vKey

Result = 0
Display= 0

RegCreateKeyEx(HKEY_LOCAL_MACHINE,vKey,0,"REG_SZ",0,SECURITY_ACCESS_MASK,0,@RESULT,@DISPLAY) && Retuna .T. si fue exitoso

RegSetValueEx(RESULT,"WltName",0,1,"Walter v2.0",12)

*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*
*!* Función Para Leer Valores *!*
*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*

FUNCTION GetKeyValue(lcValue) && Leyendo datos de LOCAL_MACHINE
#DEFINE HKEY_LOCAL_MACHINE 0x80000002
#DEFINE ccKey "SOFTWARE\WLT_APPS"
#DEFINE ERROR_SUCCESS 0
#DEFINE KEY_READ 131097

LOCAL hKey, lcData, lnSize, lcResult
hKey = 0

* Abriendo una Key específica
IF RegOpenKeyEx(HKEY_LOCAL_MACHINE,;
ccKey, 0, KEY_READ, @hKey) <> ERROR_SUCCESS
RETURN "#error#"
ENDIF

lnSize = 250
lcData = SPACE(lnSize)

* Seleccionando el Valor de la Key
IF RegQueryValueEx(hKey, lcValue, 0,0, @lcData, @lnSize) = ERROR_SUCCESS
lcResult = Left(lcData, lnSize-1)
ELSE
lcResult = "#no encontrado#"
ENDIF

= RegCloseKey (hKey)
RETURN lcResult


PROCEDURE prcDlls

DECLARE RegCreateKeyEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,STRING,INTEGER,;
INTEGER,INTEGER,INTEGER @, INTEGER @

DECLARE RegSetValueEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,INTEGER,STRING,INTEGER

DECLARE INTEGER RegCloseKey IN advapi32 INTEGER hKey

DECLARE INTEGER RegOpenKeyEx IN advapi32;
INTEGER hKey, STRING lpSubKey, INTEGER ulOptions,;
INTEGER samDesired, INTEGER @phkResult

DECLARE INTEGER RegQueryValueEx IN advapi32;
INTEGER hKey, STRING lpValueName, INTEGER lpReserved,;
INTEGER @lpType, STRING @lpData, INTEGER @lpcbData


I hope this helps!

Walter Geovanny Jolon Ruiz
Guatemala, C.A.
 
Thanks guys!!!
Awesome input all. How about trying this one:

Note a get an error during compule time "Unrecognized command ver" for line:'Integer fdwType, String lpbData, Integer cbData'

Any sugestions?


* Declare the API functions:
#Define HKEY_CLASSES_ROOT           -2147483648
#Define REG_SZ                          1          && String
#Define ERROR_SUCCESS                   0          && OK

Declare Integer RegOpenKey In Win32API ;
Integer nHkey, String @cSubkey, Integer @nResult
Declare Integer RegCloseKey In Win32API ;
Integer nHkey
Declare Integer RegSetValueEx In Win32API;
Integer hKey, String lpsValueName, Integer dwReserved,; 
Integer fdwType, String lpbData, Integer cbData

SetRegistryOption_String(;
            HKEY_CLASSES_ROOT, ;
            "SOFTWARE\PitneyBowes\Ascent", ;
            "CustomerUpgradable", ;
            "3" )

Function SetRegistryOption_String
Lparameters tnRegRoot, tcKey, tcOption, tcValueToSet
Local lnKeyHandle,lpcbData,lpbData

m.lpbData = m.tcValueToSet + Chr(0) && Null terminate
m.lpcbData = Len(m.lpbData) && Data size

If ( RegOpenKey(m.tnRegRoot, m.tcSubKey, @lnKeyHandle) = ERROR_SUCCESS )
  RegSetValueEx(m.lnKeyHandle,m.tcOption, 0,REG_SZ,@lpbData,@lpcbData)
  RegCloseKey(m.lnKeyHandle)
endif
 
Try this please!

The first value is "0"
RegSetValueEx(RESULT,"CustomerUpgradable",0,1,"0",1)
Then change the value to "3" and run the program again, that works!



* DECLARACION DE DLLs
DO prcDlls

CreaKey("SOFTWARE\PitneyBowes\Ascent")
a=GetKeyValue("CustomerUpgradable")

Wait WINDOW a

*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*
*!* Función Para Crear y Asignar Valores *!*
*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*

Function CreaKey(vKey)
#DEFINE HKEY_LOCAL_MACHINE 2147483650 && (HKEY) 0x80000002
#DEFINE SECURITY_ACCESS_MASK 983103 && SAM value KEY_ALL_ACCESS

Local Result, Display, vKey

*Set Step On

Result = 0
Display= 0

RegCreateKeyEx(HKEY_LOCAL_MACHINE,vKey,0,"REG_SZ",0,SECURITY_ACCESS_MASK,0,@RESULT,@DISPLAY) && Retuna .T. si fue exitoso

RegSetValueEx(RESULT,"CustomerUpgradable",0,1,"0",1)

*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*
*!* Función Para Leer Valores *!*
*!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!* *!*

FUNCTION GetKeyValue(lcValue) && Leyendo datos de LOCAL_MACHINE
#DEFINE HKEY_LOCAL_MACHINE 0x80000002
#DEFINE ccKey "SOFTWARE\PitneyBowes\Ascent"
#DEFINE ERROR_SUCCESS 0
#DEFINE KEY_READ 131097

LOCAL hKey, lcData, lnSize, lcResult
hKey = 0

* Abriendo una Key específica
IF RegOpenKeyEx(HKEY_LOCAL_MACHINE,;
ccKey, 0, KEY_READ, @hKey) <> ERROR_SUCCESS
RETURN "#error#"
ENDIF

lnSize = 250
lcData = SPACE(lnSize)

* Seleccionando el Valor de la Key
IF RegQueryValueEx(hKey, lcValue, 0,0, @lcData, @lnSize) = ERROR_SUCCESS
lcResult = Left(lcData, lnSize-1)
ELSE
lcResult = "#no encontrado#"
ENDIF

= RegCloseKey (hKey)
RETURN lcResult


PROCEDURE prcDlls

DECLARE RegCreateKeyEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,STRING,INTEGER,;
INTEGER,INTEGER,INTEGER @, INTEGER @

DECLARE RegSetValueEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,INTEGER,STRING,INTEGER

DECLARE INTEGER RegCloseKey IN advapi32 INTEGER hKey

DECLARE INTEGER RegOpenKeyEx IN advapi32;
INTEGER hKey, STRING lpSubKey, INTEGER ulOptions,;
INTEGER samDesired, INTEGER @phkResult

DECLARE INTEGER RegQueryValueEx IN advapi32;
INTEGER hKey, STRING lpValueName, INTEGER lpReserved,;
INTEGER @lpType, STRING @lpData, INTEGER @lpcbData

Walter Geovanny Jolon Ruiz
Guatemala, C.A.
 
Hi Walter:

I had to comment line (ref. duplicate) "*#DEFINE HKEY_LOCAL_MACHINE 0x80000002 " and added a "3" to RegSetValueEx(RESULT,"CustomerUpgradable",0,1,"3",1). Works fine thanks. I just thought there was a way yo just *edit* my key value instead of overwriting/recreating it.

Thanks again.

~PM
 
Ok, I hope that helps you a little. I try to do what you tried but doesn't work, I don't know why but I think that if something works in other ways we have to use it.

Regards!

Walter Geovanny Jolon Ruiz
Guatemala, C.A.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top