Search code examples
c++winapiregistry

Unexplainable bug in my code


So I'm trying to write a small application that changes a line in registry to enable a certain thing..

Here is my code.

#include <iostream>
#include <Windows.h>

DWORD transparenton = 0x00000001;
DWORD transparentoff = 0x00000000;

using namespace std;

void pause();
void act(PHKEY key);
void enableTransparency();
void disableTransparency();


int main()
{
    cout << "\tStart Menu Blurrier\n";
    cout << "Make your Windows 10 start menu background blurry like in Windows 7\nAutomatic On/Off\n";
    pause();
    PHKEY result;
    RegOpenKeyA(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\\EnableBlurBehind", result);
    act(result);
    //enableTransparency();
    RegCloseKey(HKEY_CURRENT_USER);
    pause();
}

void pause()
{
    cout << "Press [ENTER] to continue...";
    cin.get();
    system("cls");
}

void act(PHKEY key)
{
    DWORD l = (DWORD)key;
    if(l==transparenton){
        disableTransparency(); 
    }
    else{
        enableTransparency(); 
    }
}

void disableTransparency()
{
    RegSetKeyValueA(HKEY_CURRENT_USER,
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\\",
        "EnableBlurBehind",
        REG_DWORD, 
        &transparentoff,
        sizeof(transparentoff));
}

void enableTransparency()
{
    RegSetKeyValueA(HKEY_CURRENT_USER, 
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\\",
        "EnableBlurBehind", 
        REG_DWORD, 
        &transparenton,
        sizeof(transparenton));
}

Ok, the bug is in the void act

void act(PHKEY key)
{
    DWORD l = (DWORD)key;
    if(l==transparenton){
        disableTransparency(); 
    }
    else{
        enableTransparency(); 
    }
}

It can detect when it's on and turn it off but it can't detect when it's off and turn it on.

1) The enableTransparency function works fine, because if I call it directly it works. 2) I've also tried two seperate if's (one for on and another for off) with no results! also tried equaling to NULL instead of transparentoff or using an else.. Nothing works.

What the hell are these c++-only bugs I seem to have every now and then.


Solution

  • There are several issues with your code (using legacy APIs, using bad parameters, missing logic, etc).

    Try something more like this instead:

    #include <iostream>
    #include <Windows.h>
    
    const DWORD transparenton = 0x00000001;
    const DWORD transparentoff = 0x00000000;
    
    using namespace std;
    
    void pause();
    void act(HKEY key);
    bool getTransparency(HKEY key, DWORD &value);
    void setTransparency(HKEY key, DWORD value);
    
    int main()
    {
        cout << "\tStart Menu Blurrier\n";
        cout << "Make your Windows 10 start menu background blurry like in Windows 7\nAutomatic On/Off\n";
        pause();
    
        HKEY hKey;
        LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);
        if (result == 0)
        {
            act(hKey);
            RegCloseKey(hKey);
        }
        return 0;
    }
    
    void pause()
    {
        cout << "Press [ENTER] to continue...";
        cin.get();
        system("cls");
    }
    
    void act(HKEY key)
    {
        DWORD value;
        if (getTransparency(key, value))
        {
            if (value == transparenton) {
                setTransparency(key, transparentoff);
            }
            else {
                setTransparency(key, transparenton);
            }
        }
    }
    
    bool getTransparency(HKEY key, DWORD &value)
    {
        DWORD size = sizeof(value);
        LONG result = RegQueryValueExW(key, L"EnableBlurBehind", NULL, NULL, (BYTE*)&value, &size);
        if (result == ERROR_FILE_NOT_FOUND)
        {
            value = transparentoff;
            result = 0;
        }
        return (result == 0);
    }
    
    void setTransparency(HKEY key, DWORD value)
    {
        RegSetValueExW(key, L"EnableBlurBehind", 0, REG_DWORD, (BYTE*)&value, sizeof(value));
    }