Subversion Repositories DevTools

Rev

Rev 4914 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4914 Rev 4922
Line 10... Line 10...
10
using namespace std;
10
using namespace std;
11
 
11
 
12
extern "C" void __declspec(dllexport) ChangePath ( HWND hwndParent, int string_size, char *variables, stack_t **stacktop, extra_parameters *extra );
12
extern "C" void __declspec(dllexport) ChangePath ( HWND hwndParent, int string_size, char *variables, stack_t **stacktop, extra_parameters *extra );
13
 
13
 
14
int  ModifyPath( const string &action, const string &type, string &element );
14
int  ModifyPath( const string &action, const string &type, string &element );
-
 
15
bool FindInPath( string &path, const string &search );
15
void CleanPath ( string &path, const string &delpath = "" );
16
bool CleanPath ( string &path, const string &delpath = "" );
16
 
17
 
17
// GLOBALS
18
// GLOBALS
18
HINSTANCE   g_hInstance;
19
HINSTANCE   g_hInstance;
19
HWND        g_hwndParent;
20
HWND        g_hwndParent;
20
 
21
 
Line 94... Line 95...
94
    string::reverse_iterator    rit;    
95
    string::reverse_iterator    rit;    
95
    HKEY                        hKey;
96
    HKEY                        hKey;
96
    LONG                        res;
97
    LONG                        res;
97
    char                        val[8152];
98
    char                        val[8152];
98
    DWORD                       size = sizeof(val);
99
    DWORD                       size = sizeof(val);
-
 
100
    bool                        pathChanged = false;
99
 
101
 
100
    // Open the registry key for system or user enviroment
102
    // Open the registry key for system or user enviroment
101
    if ( type == TypeSystem )
103
    if ( type == TypeSystem )
102
    {
104
    {
103
        res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"), 0, KEY_ALL_ACCESS, &hKey);
105
        res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"), 0, KEY_ALL_ACCESS, &hKey);
Line 124... Line 126...
124
 
126
 
125
    // Remove any trailing ; from element
127
    // Remove any trailing ; from element
126
    for ( rit = element.rbegin(); *rit == ';'; rit = element.rbegin() )
128
    for ( rit = element.rbegin(); *rit == ';'; rit = element.rbegin() )
127
        element.resize(element.length()-1);
129
        element.resize(element.length()-1);
128
 
130
 
129
    if ( action == ActionAddBefore )
131
    if ( action == ActionAddBefore && ! FindInPath(path, element) )
130
    {
132
    {
131
        // make sure new dir to add has a ; on the end so we can insert it to the start of path 
133
        // make sure new dir to add has a ; on the end so we can insert it to the start of path 
132
        element += ';';
134
        element += ';';
133
        path.insert(0, element);
135
        path.insert(0, element);
134
        // Clean up any empty fields
136
        // Clean up any empty fields
135
        CleanPath(path);
137
        CleanPath(path);
-
 
138
        pathChanged = true;
136
    }
139
    }
137
    else if ( action == ActionAddAfter )
140
    else if ( action == ActionAddAfter && ! FindInPath(path, element) )
138
    {
141
    {
139
        // append ; followed by new dir, clean path will remove empty fields so dont need to check for ; on the end
142
        // append ; followed by new dir, clean path will remove empty fields so dont need to check for ; on the end
140
        path += ';';
143
        path += ';';
141
        path += element;
144
        path += element;
142
        // Clean up any empty fields
145
        // Clean up any empty fields
143
        CleanPath(path);
146
        CleanPath(path);
-
 
147
        pathChanged = true;
144
    }
148
    }
145
    else if ( action == ActionDelete )
149
    else if ( action == ActionDelete )
146
    {
150
    {
147
        // remove element from path if it exists
151
        // remove element from path if it exists
148
        CleanPath(path, element);
152
        pathChanged = CleanPath(path, element);
149
    }
153
    }
150
 
154
 
151
    // Nopw write the path
155
    // Now write the path
-
 
156
    res = ERROR_SUCCESS;
-
 
157
    if ( pathChanged )
152
    res = RegSetValueEx(hKey, TEXT("Path"), NULL, REG_EXPAND_SZ, (LPBYTE)path.c_str(), path.length()+1);
158
        res = RegSetValueEx(hKey, TEXT("Path"), NULL, REG_EXPAND_SZ, (LPBYTE)path.c_str(), path.length()+1);
153
 
159
 
154
    RegCloseKey(hKey);
160
    RegCloseKey(hKey);
155
 
161
 
156
    return ( res != ERROR_SUCCESS ? 0 : 1);
162
    return ( res != ERROR_SUCCESS ? 0 : 1);
157
}
163
}
158
 
164
 
159
 
165
 
-
 
166
bool FindInPath( string &path, const string &search )
-
 
167
{
-
 
168
    string                      element;
-
 
169
    istringstream               spath(path);
-
 
170
 
-
 
171
    //Split each path element on ; and discard any empty ones
-
 
172
    while ( getline(spath, element, ';') )
-
 
173
    {
-
 
174
        if ( element == search )
-
 
175
            return true;
-
 
176
    }
-
 
177
    
-
 
178
    return false;
-
 
179
}
-
 
180
 
-
 
181
 
160
void CleanPath( string &path, const string &delpath )
182
bool CleanPath( string &path, const string &delpath )
161
{
183
{
162
    string                      newpath("");
184
    string                      newpath("");
163
    string                      element;
185
    string                      element;
164
    istringstream               spath(path);
186
    istringstream               spath(path);
165
    string::reverse_iterator    it;
187
    string::reverse_iterator    it;
-
 
188
    bool                        changed = false;
166
 
189
 
167
    //Split each path element on ; and discard any empty ones
190
    //Split each path element on ; and discard any empty ones
168
    while ( getline(spath, element, ';') )
191
    while ( getline(spath, element, ';') )
169
    {
192
    {
170
        if ( ! element.empty() && ( delpath.empty() || element != delpath ))
193
        if ( ! element.empty() && ( delpath.empty() || element != delpath ))
171
        {
194
        {
172
            newpath += element + ";";
195
            newpath += element + ";";
173
        }
196
        }
-
 
197
        else
-
 
198
        {
-
 
199
            changed = true;
-
 
200
        }
174
    }
201
    }
175
    
202
    
176
    // Remove any trailing ;
203
    // Remove any trailing ;
177
    for ( it = newpath.rbegin(); *it == ';'; it = newpath.rbegin() )
204
    for ( it = newpath.rbegin(); *it == ';'; it = newpath.rbegin() )
-
 
205
    {
178
        newpath.resize(newpath.length()-1);
206
        newpath.resize(newpath.length()-1);
-
 
207
        changed = true;
-
 
208
    }
179
 
209
 
-
 
210
    if ( changed )
180
    path = newpath;
211
        path = newpath;
-
 
212
    
-
 
213
    return changed;
181
}
214
}
182
 
215
 
183
 
216
 
184
BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
217
BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
185
{
218
{