tags:

views:

43

answers:

1

I need user to define all the environment variables needed for my program in a text file as shown below.

MyDLLPath = C:\MyDLLPath
MyOption = Option1
PATH = %MyDLLPath%;%PATH%;

In my program I read each line and call putenv with the string. Problem is that the environment substitutions (%MyDLLPath%) are not being expanded. I am guessing the following fix for that
- Check each line for % characters.
- Get the text between 2 consecutive % characters.
- Call getenv using the text
- Replace value obtained above into the line and then call putenv.

Is there a better way to do it?

Edit: Starting with windows platform. Will have to do unix but not sure how to deal with unix syntax for the substitutions.

+2  A: 

You could use ExpandEnvironmentStrings.

Philipp
+1, could work. Critically depends on the order in the file though.
Hans Passant
Yes, the order is important, just like in the OP's algorithm. If you wanted the entries in arbitrary order, you'd have to build up a dependency graph; that would probably be overkill for a simple configuration file.
Philipp