Hej det kan finnas en lösning här... Löste det och har nu gjort en liten enkel fungerande klass istället. Delar med mig den ifall andra kan ha nytta av det.Skapa nyklar i registret - hur?
Har förljande kod som delvis fungerar
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HKEY reg;
if( RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\My Company\\My Application"), 0, NULL, 0, REG_OPTION_NON_VOLATILE, NULL, ®, NULL) == ERROR_SUCCESS)
{
cout << "Error Creating The Key" << endl;
return 0;
}
char lpdata[] = "Value";
if( RegSetValueEx (reg, "Test", 0, REG_SZ, (LPBYTE)lpdata, sizeof(lpdata)) )
{
cout << "Error Setting The Value" << endl;
return 0;
}
RegCloseKey(reg);
return 0;
}
Nyklen skapas - Men när man ska sätta värdet så går det inte. Hur ska jag lösa det?
Har googlat men inte lyckas hitta någon enkel lösning =/
Sv: Skapa nyklar i registret - hur?
http://www.cprogramming.com/tutorial.htmlSv:Skapa nyklar i registret - hur?
const int CHAR_LIMIT = 255;
class regedit
{
private:
HKEY regKey;
LONG keyStatus;
public:
void CreateStringItem(LPCSTR keyPath, LPCSTR itemName, string itemValue);
void CreateDwordItem(LPCSTR keyPath, LPCSTR itemName, int itemValue);
string GetStringValue(LPCSTR keyPath, LPCSTR itemName);
DWORD GetDwordValue(LPCSTR keyPath, LPCSTR itemName);
void DeleteItem(LPCSTR keyPath, LPCSTR itemName);
void DeleteKey(LPCSTR keyPath);
};
void regedit::CreateStringItem(LPCSTR keyPath, LPCSTR itemName, string itemValue)
{
keyStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, ®Key, NULL);
if(keyStatus == ERROR_SUCCESS)
{
char lpdata[CHAR_LIMIT];
strcpy_s(lpdata, itemValue.c_str());
RegSetValueEx (regKey, itemName, 0, REG_SZ, (LPBYTE)lpdata, sizeof(lpdata));
RegCloseKey(regKey);
}
}
void regedit::CreateDwordItem(LPCSTR keyPath, LPCSTR itemName, int itemValue)
{
keyStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, ®Key, NULL);
if(keyStatus == ERROR_SUCCESS)
{
DWORD dwVal = itemValue;
RegSetValueEx (regKey, itemName, 0, REG_DWORD, (BYTE*)&dwVal, sizeof(DWORD));
RegCloseKey(regKey);
}
}
string regedit::GetStringValue(LPCSTR keyPath, LPCSTR itemName)
{
char lszValue[CHAR_LIMIT];
DWORD dwType = REG_SZ;
DWORD dwSize = CHAR_LIMIT;
keyStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_ALL_ACCESS, ®Key);
if (keyStatus == ERROR_SUCCESS)
{
keyStatus = RegQueryValueEx(regKey, itemName, NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
if (keyStatus == ERROR_SUCCESS)
{
return lszValue;
}
RegCloseKey(regKey);
}
return false;
}
DWORD regedit::GetDwordValue(LPCSTR keyPath, LPCSTR itemName)
{
DWORD dwVersion;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
keyStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_ALL_ACCESS, ®Key);
if (keyStatus == ERROR_SUCCESS)
{
keyStatus = RegQueryValueEx(regKey, itemName, NULL, &dwType, (LPBYTE)&dwVersion, &dwSize);
if (keyStatus == ERROR_SUCCESS)
{
return dwVersion;
}
RegCloseKey(regKey);
}
return false;
}
void regedit::DeleteItem(LPCSTR keyPath, LPCSTR itemName)
{
keyStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyPath, 0, KEY_ALL_ACCESS, ®Key);
if (keyStatus == ERROR_SUCCESS)
{
RegDeleteValue(regKey, itemName);
}
}
void regedit::DeleteKey(LPCSTR keyPath)
{
RegDeleteKey(HKEY_LOCAL_MACHINE, keyPath);
}