News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Deleting Registry Value

Started by timertik, March 23, 2007, 08:34:10 AM

Previous topic - Next topic

timertik

I have a dialog and a push button I need to delete a value from the registry when its clicked
I tried this and it compiles but it doesnt delete the value
http://msdn2.microsoft.com/en-us/library/ms538724.aspx
invoke SHDeleteValue, HKEY_CURRENT_USER, ADDR subkey, ADDR value
I also tried
http://msdn2.microsoft.com/en-us/library/aa914740.aspx
invoke RegDeleteValue, HKEY_CURRENT_USER, ADDR  value

RegDeleteValue doesn't go into subkey's so it will not work

Im sure the first one should delete the value but it doesn't  :red

here is the rest of the code for SHDel..
include shlwapi.inc ;Include file
includelib shlwapi.lib ;Include lib

.data
subkey         db "SOFTWARE\Microsoft\Windows\CurrentVersion\Run",0 ; subkey
value       db "test",0                                      ; Value name

.elseif eax==IDC_DELVALUE
invoke SHDeleteValue, HKEY_CURRENT_USER, ADDR subkey, ADDR value



::)

sinsi

As far as RegDeleteValue goes, you need to open the subkey first, e.g.
.data?
    hkey DWORD ?
.code
   ...
    INVOKE RegOpenKeyEx,HKEY_CURRENT_USER, ADDR subkey,0,KEY_ALL_ACCESS,ADDR hkey
    or eax,eax
    jnz error    ;usually that the subkey doesn't exist, or the current profile is denied access

    INVOKE RegDeleteValue,hkey, ADDR value
    or eax,eax
    jnz error    ;usually that the value doesn't exist, or the current profile is denied access


Don't forget a "RegCloseKey" if the open succeeds (even if the delete fails) - leaving handles to the registry open can cause problems.
Light travels faster than sound, that's why some people seem bright until you hear them.

timertik

Thanks I'll try it know this is my first time really using the registry for anything so it should be interesting  :green2
EDIT:
I just tried it out and I get the same results
here is what the code looks like:
.elseif eax==1002
INVOKE RegOpenKeyEx,HKEY_CURRENT_USER, ADDR subkey,0,KEY_ALL_ACCESS,ADDR hkey
    or eax,eax
    jnz error    ;usually that the subkey doesn't exist, or the current profile is denied access

    INVOKE RegDeleteValue,hkey, ADDR value
    or eax,eax
    jnz error    ;usually that the value doesn't exist, or the current profile is denied access
error:
invoke MessageBox, NULL, addr error2, addr error1, MB_ICONSTOP
invoke  RegCloseKey, hkey

am I missing something or is it that everything i try fails  ::)

PBrennick

The first think that you want to do is to put a messagebox after the line that contains the 1002 to make sure that the program is getting to that code. Once you are sure of that, start putting the various variable contents into the messagebox statement so as to ensure that the contents are correct. Once all that is proved THEN ask questions. Okay?

Also, SOFTWARE\Microsoft\Windows\CurrentVersion\Run

should be

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Hope that helps,
Paul
The GeneSys Project is available from:
The Repository or My crappy website

sinsi

Well after making a value called "test" in the HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run key I ran this code
include \asm\win32.inc

.data?
hkey    DWORD ?
.data
subkey  db "SOFTWARE\Microsoft\Windows\CurrentVersion\Run",0
value   db "test",0  

.code

start proc public
  INVOKE RegOpenKeyEx,HKEY_CURRENT_USER, ADDR subkey,0,KEY_ALL_ACCESS,ADDR hkey
  or eax,eax
  jnz error    ;usually that the subkey doesn't exist, or the current profile is denied access

  INVOKE RegDeleteValue,hkey, ADDR value
  or eax,eax
  jnz error1    ;usually that the value doesn't exist, or the current profile is denied access

error1:
  invoke RegCloseKey,hkey
  ;show "couldn't delete value"
  jmp done

error:
  ;show "couldn't open run key"

 done:
   push eax
   jmp ExitProcess
start ENDP
end start
pressed F5 in regedit and "test" disappeared from the list of values, and the debugger jumped straight to done:
Looking at your code, you need to show a MessageBox if it succeeds - at the moment, your code ALWAYS ends up at error:

Quote from: PBrennick on March 23, 2007, 09:56:56 AM
Also, SOFTWARE\Microsoft\Windows\CurrentVersion\Run

should be

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Hope that helps,
Paul

Um, no - one is the root key(HKEY_CURRENT_USER predefined), one is the subkey to open off the root(SOFTWARE\Microsoft\Windows\CurrentVersion\Run)
Light travels faster than sound, that's why some people seem bright until you hear them.

timertik

 :green2 I feel stupid now I placed the code for the button after a certain endif which caused it to not even be run
I found this out by adding a msgbox the the first line I got it working now it deletes fine
Thanks!

It works for push buttons but not in a check box  :eek
.elseif eax==1436
if autostart
.if autost
mov checked1,FALSE
invoke  RegCreateKeyEx, HKEY_CURRENT_USER, ADDR run_subkey, 0, 0, 0, KEY_WRITE, 0, ADDR key1, 0; Create Value
    invoke  RegSetValueEx, key1, ADDR run_name, 0, REG_SZ, ADDR run_path, sizeof run_path
    invoke  RegCloseKey, key1
.else
INVOKE RegOpenKeyEx,HKEY_CURRENT_USER, ADDR subkey,0,KEY_ALL_ACCESS,ADDR hkey
  or eax,eax
  jnz error

  INVOKE RegDeleteValue,hkey, ADDR value; Delete Value
  or eax,eax
  jnz errorous

error:
invoke MessageBox, NULL, addr error2, addr error1, MB_ICONSTOP
errorous:
invoke MessageBox, NULL, addr error2, addr error1, MB_ICONSTOP
invoke  RegCloseKey, hkey

mov checked1,TRUE
.endif
endif


it will create the value on check but it will not delete the value when unchecked I have been trying this but fail is it something simple I need to change?
and its the same code run in the buttons so it works it must be my check boxes  :red

PBrennick

sinsi,
I was not referring to YOUR code, I was referring to his at the top of the topic. Your code is very nice.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

timertik

actually if I add the key to the beggining of the subkey it will fail because i have already defined it
I assume you are talking about this
invoke SHDeleteValue, HKEY_CURRENT_USER, ADDR subkey, ADDR Value
In this HKEY_CURRENT_USER i defined after SHDeleteValue so redefining it in the subkey would result in a crash
because it would be trying to delete a value from :
HKEY_CURRENT_USER\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

I discovered that the first snippet of code i was trying also works and is much smaller then the other method

I still cannot get the check box to work though I stopped trying and removed it I like the new way I done it better.
so never mind the reply about the checkbox