我的代码中无法解释的错误
Unexplainable bug in 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));
}
好的,bug在void act
void act(PHKEY key)
{
DWORD l = (DWORD)key;
if(l==transparenton){
disableTransparency();
}
else{
enableTransparency();
}
}
可以检测开机和关机,但不能检测关机和开机。
1) enableTransparency 函数工作正常,因为如果我直接调用它它就可以工作。
2)我也尝试了两个单独的if(一个用于打开另一个用于关闭)但没有结果!还尝试等于 NULL 而不是 transparentoff 或使用 else..
没有任何效果。
我时不时遇到的这些仅限 C++ 的错误到底是什么。
您的代码存在多个问题(使用遗留 API、使用错误参数、缺少逻辑等)。
尝试更像这样的东西:
#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));
}
所以我正在尝试编写一个小应用程序来更改注册表中的一行以启用特定功能..
这是我的代码。
#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));
}
好的,bug在void act
void act(PHKEY key)
{
DWORD l = (DWORD)key;
if(l==transparenton){
disableTransparency();
}
else{
enableTransparency();
}
}
可以检测开机和关机,但不能检测关机和开机。
1) enableTransparency 函数工作正常,因为如果我直接调用它它就可以工作。 2)我也尝试了两个单独的if(一个用于打开另一个用于关闭)但没有结果!还尝试等于 NULL 而不是 transparentoff 或使用 else.. 没有任何效果。
我时不时遇到的这些仅限 C++ 的错误到底是什么。
您的代码存在多个问题(使用遗留 API、使用错误参数、缺少逻辑等)。
尝试更像这样的东西:
#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));
}