1 /**
2 * Inkscape Registry Tool
3 *
4 * This simple tool is intended for allowing Inkscape to append subdirectories
5 * to its path. This will allow extensions and other files to be accesses
6 * without explicit user intervention.
7 *
8 * Authors:
9 * Bob Jamison
10 *
11 * Copyright (C) 2005-2008 Bob Jamison
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
28 #include <windows.h>
29 #include <string>
30 #include <cstdio>
32 #include "registrytool.h"
35 typedef struct
36 {
37 HKEY key;
38 int strlen;
39 const char *str;
40 } KeyTableEntry;
44 KeyTableEntry keyTable[] =
45 {
46 { HKEY_CLASSES_ROOT, 18, "HKEY_CLASSES_ROOT\\" },
47 { HKEY_CURRENT_CONFIG, 20, "HKEY_CURRENT_CONFIG\\" },
48 { HKEY_CURRENT_USER, 18, "HKEY_CURRENT_USER\\" },
49 { HKEY_LOCAL_MACHINE, 19, "HKEY_LOCAL_MACHINE\\" },
50 { HKEY_USERS, 11, "HKEY_USERS\\" },
51 { NULL, 0, NULL }
52 };
55 /**
56 * Set the string value of a key/name registry entry
57 */
58 bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
59 const Glib::ustring &valueName,
60 const Glib::ustring &value)
61 {
62 Glib::ustring keyName = keyNameArg;
64 HKEY rootKey = HKEY_LOCAL_MACHINE; //default root
65 //Trim out the root key if necessary
66 for (KeyTableEntry *entry = keyTable; entry->key; entry++)
67 {
68 if (keyName.compare(0, entry->strlen, entry->str)==0)
69 {
70 rootKey = entry->key;
71 keyName = keyName.substr(entry->strlen);
72 }
73 }
74 //printf("trimmed string: '%s'\n", keyName.c_str());
76 //Get or create the key
77 HKEY key;
78 if (RegCreateKeyEx(rootKey, keyName.c_str(),
79 0, NULL, REG_OPTION_NON_VOLATILE,
80 KEY_WRITE, NULL, &key, NULL))
81 {
82 fprintf(stderr, "RegistryTool: Could not create the registry key '%s'\n", keyName.c_str());
83 return false;
84 }
86 //Set the value
87 if (RegSetValueEx(key, valueName.c_str(),
88 0, REG_SZ, (LPBYTE) value.c_str(), (DWORD) value.size()))
89 {
90 fprintf(stderr, "RegistryTool: Could not set the value '%s'\n", value.c_str());
91 RegCloseKey(key);
92 return false;
93 }
96 RegCloseKey(key);
98 return true;
99 }
103 /**
104 * Get the full path, directory, and base file name of this running executable
105 */
106 bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
107 Glib::ustring &path,
108 Glib::ustring &exeName)
109 {
111 char buf[MAX_PATH+1];
112 if (!GetModuleFileName(NULL, buf, MAX_PATH))
113 {
114 fprintf(stderr, "Could not fetch executable file name\n");
115 return false;
116 }
117 else
118 {
119 //printf("Executable file name: '%s'\n", buf);
120 }
122 fullPath = buf;
123 path = "";
124 exeName = "";
125 Glib::ustring::size_type pos = fullPath.rfind('\\');
126 if (pos != fullPath.npos)
127 {
128 path = fullPath.substr(0, pos);
129 exeName = fullPath.substr(pos+1);
130 }
132 return true;
133 }
137 /**
138 * Append our subdirectories to the Application Path for this
139 * application
140 */
141 bool RegistryTool::setPathInfo()
142 {
143 Glib::ustring fullPath;
144 Glib::ustring path;
145 Glib::ustring exeName;
147 if (!getExeInfo(fullPath, path, exeName))
148 return false;
150 //printf("full:'%s' path:'%s' exe:'%s'\n",
151 // fullPath.c_str(), path.c_str(), exeName.c_str());
153 Glib::ustring keyName =
154 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
155 keyName.append(exeName);
157 Glib::ustring valueName = "";
158 Glib::ustring value = fullPath;
160 if (!setStringValue(keyName, valueName, value))
161 return false;
163 //add our subdirectories
164 Glib::ustring appPath = path;
165 appPath.append("\\python;");
166 appPath.append(path);
167 appPath.append("\\perl");
168 valueName = "Path";
169 value = appPath;
171 if (!setStringValue(keyName, valueName, value))
172 return false;
174 return true;
175 }
178 #ifdef TESTREG
181 /**
182 * Compile this file with
183 * g++ -DTESTREG registrytool.cpp -o registrytool
184 * to run these tests.
185 */
189 void testReg()
190 {
191 RegistryTool rt;
192 char *key =
193 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\inkscape.exe";
194 char const *name = "";
195 char const *value = "c:\\inkscape\\inkscape.exe";
196 if (!rt.setStringValue(key, name, value))
197 {
198 printf("Test failed\n");
199 }
200 else
201 {
202 printf("Test succeeded\n");
203 }
204 name = "Path";
205 value = "c:\\inkscape\\python";
206 if (!rt.setStringValue(key, name, value))
207 {
208 printf("Test failed\n");
209 }
210 else
211 {
212 printf("Test succeeded\n");
213 }
214 }
217 void testPath()
218 {
219 RegistryTool rt;
220 rt.setPathInfo();
221 }
224 int main(int argc, char **argv)
225 {
226 //testReg();
227 testPath();
228 return 0;
229 }
231 #endif /* TESTREG */
233 //########################################################################
234 //# E N D O F F I L E
235 //########################################################################