tags:

views:

278

answers:

3

Is there a way to get the SearchPath API to not search in c:\windows when using the default search path (passing NULL as the first param)? I can't modify the caller to send in a specific path.

I have a system with an application ini file in c:\windows (which I don't want it to use, but for legacy reasons has to remain there). I put my copy of the same ini file in c:\users\public, and put c:\users\public at the front of my system path environment variable, but a call to SearchPath still finds the c:\windows version. If I delete that version, it then finds the c:\users\public version, so I know the path was set correctly.

+1  A: 

According to MSDN, there's nothing you can do about this bar changing a system level (HKLM) registry entry (Which is a "bad thing"). The registry change would cause the search order to start with the current working directory, which you could set to the desired folder in a shortcut. (Again, I'm going to say; changing a Machine Level registry entry to do this - is potentially dangerous!)

Have you looked into application shims? This may be something that could work for you.

Rob
+1  A: 

Try SetCurrentDirectory("c:\users\public") and then SearchPath.

akalenuk
Thanks, SearchPath is called inside a lib, so I really shouldn't be messing with the applications current directory. Also, different users will want to use different paths, I can't hard code it. I'll use an environment var users can set and I will use that path instead of passing NULL to SearchPath.
Jeremy Mullin
Yes, that seems to be the best solution.
akalenuk
A: 

I know this is very late, but having just run into this problem myself, I would propose a better solution.

The first argument to SearchPath, as you have found, can be used to specify the directories you want it to search, instead of the default order. You can retrieve and use the current user's PATH with GetEnvironmentVariable, and then search within that:

DWORD err = GetEnvironmentVariable("PATH", NULL, 0);
char* path = new char[err+1]; path[err] = 0;
GetEnvironmentVariable("PATH", path, err);

err = SearchPath(path, "application", ".ini", 0, NULL, NULL);
char* searchResult = new char[err+1]; searchResult[err] = 0;
err = SearchPath(path, "application", ".ini", err, searchResult, NULL);
Jason Owen