• Articles
  • Helpful Function for the Registry
Published by
Dec 2, 2013 (last update: Dec 14, 2013)

Helpful Function for the Registry

Score: 2.8/5 (38 votes)
*****
Note: const char* PATH means the Path inclusive the file name
Note: HKEY hKey means the property, such as HKEY_LOCAL_MACHINE or HKEY_USERS
2013 Jetkey Nature

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <windows.h>

#ifndef REGEDIT_H
#define REGEDIT_H

//Prototypes
void createreg(const char* NAME);
void createreg(const char* NAME,const char* PATH);
void createreg(const char* NAME,HKEY hKey);
void createreg(const char* NAME,const char* PATH,HKEY hKey);
//Prototypes

void createreg(const char* NAME)	{

        //path variable
        TCHAR szPath[MAX_PATH];

        //get the path of the running app
        GetModuleFileName(NULL, szPath,MAX_PATH);

        //new HKEY
        HKEY newValue;

        //Open the registry tree
	RegOpenKey(HKEY_USERS, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue);

        //Set new registry value
	RegSetValueEx(newValue, NAME,0,REG_SZ, (LPBYTE)szPath, sizeof(szPath));

        //close the registry
	RegCloseKey(newValue);	
}

void createreg(const char* NAME, const char* PATH)	{

        //new HKEY
        HKEY newValue;

        //Open the registry tree
	RegOpenKey(HKEY_USERS, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue);

        //Set new registry value
	RegSetValueEx(newValue, NAME,0,REG_SZ, (LPBYTE)szPath, sizeof(szPath));

        //close the registry
	RegCloseKey(newValue);	
}


void createreg(const char* NAME, HKEY hKey)	{

        //path variable
        TCHAR szPath[MAX_PATH];

        //get the path of the running app
        GetModuleFileName(NULL, szPath,MAX_PATH);

        //new HKEY
        HKEY newValue;

        //Open the registry tree
	RegOpenKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue);

        //Set new registry value
	RegSetValueEx(newValue, NAME,0,REG_SZ, (LPBYTE)szPath, sizeof(szPath));

        //close the registry
	RegCloseKey(newValue);	
}

void createreg(const char* NAME, const char* PATH, HKEY hKey)	{

        //new HKEY
        HKEY newValue;

        //Open the registry tree
	RegOpenKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue);

        //Set new registry value
	RegSetValueEx(newValue, NAME,0,REG_SZ, (LPBYTE)szPath, sizeof(szPath));

        //close the registry
	RegCloseKey(newValue);	
}


#endif