registry removing

hi everyone :)

Ive got problem with func RegDeleteKeyA(). Ive created some registry but when I try to remove it with this dunc I get error, returing 2, which means file not found. This is strange cause i use the same path.
This is my code (Im using QtSDK library):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void SettingsDialog::on_buttonSave_clicked(){
    QString regPath ="Software\\Microsoft\\Windows\\Currentversion\\Run";
    QString text="C:\\Program Files\\Aspect\\NoteBlock\\NoteBlock.exe";
    LPCSTR subkey = "NoteBlock";
    if(ui->startUp->isChecked()){  //if the checkbox 'turn on on startUp' is checked
        HKEY hkey;

        if(RegOpenKeyExA(HKEY_CURRENT_USER, const_cast<char *>(regPath.toStdString().c_str()),0,KEY_ALL_ACCESS,&hkey)!=ERROR_SUCCESS){
             ...
        }

        if(RegSetValueExA(hkey, subkey,0,REG_SZ,(BYTE*)(text.toStdString().c_str()),strlen(text.toStdString().c_str()))!=ERROR_SUCCESS){
            ...
        }

        if(RegCloseKey(hkey)!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error closing registry files.");
            ...
        }
    }
    else{
        HKEY hkey;

        if(RegOpenKeyExA(HKEY_CURRENT_USER, const_cast<char *>(regPath.toStdString().c_str()),0,KEY_ALL_ACCESS,&hkey)!=ERROR_SUCCESS){
            ...
        }

        long res = RegDeleteKeyA(hkey, subkey);
        if(res!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error removing registry file.");
            QMessageBox::critical(parent, "Error", QString::number(res));
            return;
            here is where the two messageboxes popup, and the second one shows value of 'res' is 2
        }
        if(RegCloseKey(hkey)!=ERROR_SUCCESS){
            ...
        }
        s->setStartUp(false);
    }
    ...
}


The registry is already created, Ive checked that its where it should be (HKCU\software\microsoft\windows\currentversion\run), theres registry called NoteBlock with string value C:\Program Files\Aspect\NoteBlock\NoteBlock.exe.

Im stuck here for some time now so I think someone can help me? .)

thanks for every reply.
From WinError.h

ERROR_FILE_NOT_FOUND = 2

It's reused by the Registry API to say that a key or value cannot be found.

Anyway, your code is creating a value and then trying to remove a subkey. As there's no subkey with the right name, just a value, it fails.

Did you want a value? Then use RegDeleteValue to delete it. Otherwise modify your code to create a subkey.

Andy
Last edited on
All those casts. Are you sure about all that stuff?

Let's take one:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms724897(v=vs.85).aspx
1
2
3
4
5
6
7
LONG WINAPI RegOpenKeyEx(
  _In_        HKEY hKey,
  _In_opt_    LPCTSTR lpSubKey,
  _Reserved_  DWORD ulOptions,
  _In_        REGSAM samDesired,
  _Out_       PHKEY phkResult
);

Then you're code:
 
RegOpenKeyExA(HKEY_CURRENT_USER, const_cast<char *>(regPath.toStdString().c_str()),0,KEY_ALL_ACCESS,&hkey)
Why that cast?

The delete is failing, but what's the error code?

Can you fix all that stuff before moving on please. Are you aware of the responsibility you take on when you use a cast in C++?
first of all thank you both for spending time by replying me :)
then
to andywestken (1808):
in the if block Im creating registry
in the else block I want that registry to be fully removed not just its value. removing the value will made the thing but I dont want the user to have useless registry with no value so Id rather remove the whole registry.

for example the user installs my app then go through the settings and see option start on startup. Since this application is made for runnig all the time the computer is turned on he decide to use this feature and he check the checkbox start on startup and hits 'set' button (thats when the if block proceeds). After some time he realizes that too many apps are starting with his system and he want that my application start with system no more. he opens the setting again and uncheck the start on startup checkbox, and the else block proceeds but it popups with this error that registry cannot be removed, which says file not found. But the registry is created so i dont see the problem :).

for kbw (5260)
I just realised that these casts here are useless so just changed the code a little bit :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void SettingsDialog::on_buttonSet_clicked(){
    const char *regPath ="Software\\Microsoft\\Windows\\Currentversion\\Run";
    const char *text="C:\\Program Files\\Aspect\\NoteBlock\\NoteBlock.exe /minimized";
    const char *minimizedtext="C:\\Program Files\\Aspect\\NoteBlock\\NoteBlock.exe";
    LPCSTR subkey = "NoteBlock";

    if(ui->startUp->isChecked()){
        HKEY hkey;

        if(RegOpenKeyExA(HKEY_CURRENT_USER, regPath,0,KEY_ALL_ACCESS,&hkey)!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error opening registry files.");
            return;
        }

        if(ui->minimized->isChecked()){
            if(RegSetValueExA(hkey, subkey,0,REG_SZ,(BYTE*)text,strlen(text))!=ERROR_SUCCESS){
                QMessageBox::critical(parent, "Error", "Error creating registry file.");
                return;
            }
        }
        else{
            if(RegSetValueExA(hkey, subkey,0,REG_SZ,(BYTE*)minimizedtext,strlen(minimizedtext))!=ERROR_SUCCESS){
                QMessageBox::critical(parent, "Error", "Error creating registry file.");
                return;
            }
        }

        if(RegCloseKey(hkey)!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error closing registry files.");
            return;
        }
        s->setStartUp(true);
    }
    else{
        HKEY hkey;

        if(RegOpenKeyExA(HKEY_CURRENT_USER, regPath,0,KEY_ALL_ACCESS,&hkey)!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error opening registry files.");
            return;
        }

        long res = RegDeleteKeyA(hkey, subkey);
        if(res!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error removing registry file.");
            QMessageBox::critical(parent, "Error", QString::number(res));
            return;
        }
        if(RegCloseKey(hkey)!=ERROR_SUCCESS){
            QMessageBox::critical(parent, "Error", "Error closing registry files.");
            return;
        }
        s->setStartUp(false);
    }
    s->setMinimized(ui->minimized->isChecked());

    s->save();
    QMessageBox::information(parent, "Success", "Settings were sucessfuly set.");

    this->hide();
}

the error code is ERROR_FILE_NOT_FOUND
Last edited on
The phrase "remove the whole registry" is a bit frightening. If you removed the whole of the registry, you'd take out Windows!

As it stands, your code is opening the (exisiting) Run key and adding a value. So all you need to do later is remove this value. You have not created a subkey, so there's nothing else you need to do to clean up. And you shouldn't remove the Run key, as that's a standard Windows key.

As I said before, you're creating a value not a subkey. The call

RegSetValueExA(hkey, subkey, ...

is creating a value, not a subkey, even though your variable is named subkey. The second param of this API call is the value name.

1
2
3
4
5
6
7
8
LONG WINAPI RegSetValueEx(
  _In_        HKEY hKey,
  _In_opt_    LPCTSTR lpValueName,
  _Reserved_  DWORD Reserved,
  _In_        DWORD dwType,
  _In_        const BYTE *lpData,
  _In_        DWORD cbData
);


(nicked from MSDN)

As you've create a value, what you need to delete is also a value!

Andy
Last edited on
oh thanks, now I understand. I got some mess in my registry knowledge :D That thing about removing windows is a bit funny :D.

Now I think I get it so thanks again :)

Tried that and work perfectly.
Topic archived. No new replies allowed.