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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/**
* Inkscape Registry Tool
*
* This simple tool is intended for allowing Inkscape to append subdirectories
* to its path. This will allow extensions and other files to be accesses
* without explicit user intervention.
*
* Authors:
* Bob Jamison
*
* Copyright (C) 2005-2008 Bob Jamison
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <windows.h>
#include <string>
#include <cstdio>
#include "registrytool.h"
typedef struct
{
HKEY key;
int strlen;
const char *str;
} KeyTableEntry;
KeyTableEntry keyTable[] =
{
{ HKEY_CLASSES_ROOT, 18, "HKEY_CLASSES_ROOT\\" },
{ HKEY_CURRENT_CONFIG, 20, "HKEY_CURRENT_CONFIG\\" },
{ HKEY_CURRENT_USER, 18, "HKEY_CURRENT_USER\\" },
{ HKEY_LOCAL_MACHINE, 19, "HKEY_LOCAL_MACHINE\\" },
{ HKEY_USERS, 11, "HKEY_USERS\\" },
{ NULL, 0, NULL }
};
/**
* Set the string value of a key/name registry entry
*/
bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
const Glib::ustring &valueName,
const Glib::ustring &value)
{
Glib::ustring keyName = keyNameArg;
bool ret = false;
HKEY rootKey = HKEY_LOCAL_MACHINE; //default root
//Trim out the root key if necessary
for (KeyTableEntry *entry = keyTable; entry->key; entry++)
{
if (keyName.compare(0, entry->strlen, entry->str)==0)
{
rootKey = entry->key;
keyName = keyName.substr(entry->strlen);
}
}
//printf("trimmed string: '%s'\n", keyName.c_str());
//Get or create the key
gunichar2 *keyw = g_utf8_to_utf16(keyName.data(), -1, 0,0,0);
gunichar2 *valuenamew = g_utf8_to_utf16(valueName.data(), -1, 0,0,0);
HKEY key;
if (RegCreateKeyExW(rootKey, (WCHAR*) keyw,
0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, &key, NULL))
{
fprintf(stderr, "RegistryTool: Could not create the registry key '%s'\n", keyName.c_str());
goto fail;
}
// Set the value
if (RegSetValueExW(key, (WCHAR*) valuenamew,
0, REG_SZ, (LPBYTE) value.data(), (DWORD) (value.size() + 1)))
{
fprintf(stderr, "RegistryTool: Could not set the value '%s'\n", value.c_str());
goto failkey;
}
ret = true;
failkey:
RegCloseKey(key);
fail:
g_free(keyw);
g_free(valuenamew);
return ret;
}
/**
* Get the full path, directory, and base file name of this running executable
*/
bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
Glib::ustring &path,
Glib::ustring &exeName)
{
const int pathbuf = 2048;
gunichar2 pathw[pathbuf];
GetModuleFileNameW(NULL, (WCHAR*) pathw, pathbuf);
gchar *utf8path = g_utf16_to_utf8(pathw, -1, 0,0,0);
fullPath = utf8path;
g_free(utf8path);
path = "";
exeName = "";
Glib::ustring::size_type pos = fullPath.rfind('\\');
if (pos != fullPath.npos)
{
path = fullPath.substr(0, pos);
exeName = fullPath.substr(pos+1);
}
return true;
}
/**
* Append our subdirectories to the Application Path for this
* application
*/
bool RegistryTool::setPathInfo()
{
Glib::ustring fullPath;
Glib::ustring path;
Glib::ustring exeName;
if (!getExeInfo(fullPath, path, exeName))
return false;
//printf("full:'%s' path:'%s' exe:'%s'\n",
// fullPath.c_str(), path.c_str(), exeName.c_str());
Glib::ustring keyName =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
keyName.append(exeName);
Glib::ustring valueName = "";
Glib::ustring value = fullPath;
if (!setStringValue(keyName, valueName, value))
return false;
//add our subdirectories
Glib::ustring appPath = path;
appPath.append("\\python;");
appPath.append(path);
appPath.append("\\perl");
valueName = "Path";
value = appPath;
if (!setStringValue(keyName, valueName, value))
return false;
return true;
}
#ifdef TESTREG
/**
* Compile this file with
* g++ -DTESTREG registrytool.cpp -o registrytool
* to run these tests.
*/
void testReg()
{
RegistryTool rt;
char *key =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\inkscape.exe";
char const *name = "";
char const *value = "c:\\inkscape\\inkscape.exe";
if (!rt.setStringValue(key, name, value))
{
printf("Test failed\n");
}
else
{
printf("Test succeeded\n");
}
name = "Path";
value = "c:\\inkscape\\python";
if (!rt.setStringValue(key, name, value))
{
printf("Test failed\n");
}
else
{
printf("Test succeeded\n");
}
}
void testPath()
{
RegistryTool rt;
rt.setPathInfo();
}
int main(int argc, char **argv)
{
//testReg();
testPath();
return 0;
}
#endif /* TESTREG */
//########################################################################
//# E N D O F F I L E
//########################################################################
|