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 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>
31 #include "registrytool.h"
34 typedef struct
35 {
36 HKEY key;
37 int strlen;
38 char *str;
39 } KeyTableEntry;
43 KeyTableEntry keyTable[] =
44 {
45 { HKEY_CLASSES_ROOT, 18, "HKEY_CLASSES_ROOT\\" },
46 { HKEY_CURRENT_CONFIG, 20, "HKEY_CURRENT_CONFIG\\" },
47 { HKEY_CURRENT_USER, 18, "HKEY_CURRENT_USER\\" },
48 { HKEY_LOCAL_MACHINE, 19, "HKEY_LOCAL_MACHINE\\" },
49 { HKEY_USERS, 11, "HKEY_USERS\\" },
50 { NULL, 0, NULL }
51 };
53 bool RegistryTool::setStringValue(const Glib::ustring &keyNameArg,
54 const Glib::ustring &valueName,
55 const Glib::ustring &value)
56 {
57 Glib::ustring keyName = keyNameArg;
59 HKEY rootKey = HKEY_LOCAL_MACHINE; //default root
60 //Trim out the root key if necessary
61 for (KeyTableEntry *entry = keyTable; entry->key; entry++)
62 {
63 if (keyName.compare(0, entry->strlen, entry->str)==0)
64 {
65 rootKey = entry->key;
66 keyName = keyName.substr(entry->strlen);
67 }
68 }
69 //printf("trimmed string: '%s'\n", keyName.c_str());
71 //Get or create the key
72 HKEY key;
73 if (RegCreateKeyEx(rootKey, keyName.c_str(),
74 0, NULL, REG_OPTION_NON_VOLATILE,
75 KEY_WRITE, NULL, &key, NULL))
76 {
77 printf("RegistryTool: Could not create the registry key '%s'\n", keyName.c_str());
78 return false;
79 }
81 //Set the value
82 if (RegSetValueEx(key, valueName.c_str(),
83 0, REG_SZ, (LPBYTE) value.c_str(), (DWORD) value.size()))
84 {
85 printf("RegistryTool: Could not set the value '%s'\n", value.c_str());
86 RegCloseKey(key);
87 return false;
88 }
91 RegCloseKey(key);
93 return true;
94 }
96 bool RegistryTool::getExeInfo(Glib::ustring &fullPath,
97 Glib::ustring &path,
98 Glib::ustring &exeName)
99 {
101 char buf[MAX_PATH+1];
102 if (!GetModuleFileName(NULL, buf, MAX_PATH))
103 {
104 printf("Could not fetch executable file name\n");
105 return false;
106 }
107 else
108 {
109 //printf("Executable file name: '%s'\n", buf);
110 }
112 fullPath = buf;
113 path = "";
114 exeName = "";
115 Glib::ustring::size_type pos = fullPath.rfind('\\');
116 if (pos != fullPath.npos)
117 {
118 path = fullPath.substr(0, pos);
119 exeName = fullPath.substr(pos+1);
120 }
122 return true;
123 }
126 bool RegistryTool::setPathInfo()
127 {
128 Glib::ustring fullPath;
129 Glib::ustring path;
130 Glib::ustring exeName;
132 if (!getExeInfo(fullPath, path, exeName))
133 return false;
135 //printf("full:'%s' path:'%s' exe:'%s'\n",
136 // fullPath.c_str(), path.c_str(), exeName.c_str());
138 Glib::ustring keyName =
139 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
140 keyName.append(exeName);
142 Glib::ustring valueName = "";
143 Glib::ustring value = fullPath;
145 if (!setStringValue(keyName, valueName, value))
146 return false;
148 //add our subdirectories
149 Glib::ustring appPath = path;
150 appPath.append("\\python;");
151 appPath.append(path);
152 appPath.append("\\perl");
153 valueName = "Path";
154 value = appPath;
156 if (!setStringValue(keyName, valueName, value))
157 return false;
159 return true;
160 }
163 #ifdef TESTREG
165 void testReg()
166 {
167 RegistryTool rt;
168 char *key =
169 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\inkscape.exe";
170 char *name = "";
171 char *value = "c:\\inkscape\\inkscape.exe";
172 if (!rt.setStringValue(key, name, value))
173 {
174 printf("Test failed\n");
175 }
176 else
177 {
178 printf("Test succeeded\n");
179 }
180 name = "Path";
181 value = "c:\\inkscape\\python";
182 if (!rt.setStringValue(key, name, value))
183 {
184 printf("Test failed\n");
185 }
186 else
187 {
188 printf("Test succeeded\n");
189 }
190 }
193 void testPath()
194 {
195 RegistryTool rt;
196 rt.setPathInfo();
197 }
200 int main(int argc, char **argv)
201 {
202 //testReg();
203 testPath();
204 return 0;
205 }
207 #endif /* TESTREG */
209 //########################################################################
210 //# E N D O F F I L E
211 //########################################################################