Operating System-level environment variables are a handy way to reduce redundancy or to simplify providing support for per-user settings. (I’m sure they’re good for other things, too, but these are the ones that spring to my mind, at least. :-)
One thing I only discovered recently – and thanks to Tekno and Dieter for discussing this – is that you can use environment variables in a number of places in the file path settings accessed via AutoCAD’s OPTIONS command. The topic came up in the specific context of the TRUSTEDPATHS settings, but it seems to have more general support than that.
In this post we’re going to look at how you can test this capability, to see for ourselves how it might be used. For many of you the information in this post will be considered very basic, but it seemed worth covering, nonetheless.
Let’s take a module – available at C:\Program Files\MyCompany\MyProduct\MyApplication.dll – for which we have demand-loading entries set up in the Registry:
This module gets loaded as AutoCAD launches, but always brings up the “secure load” dialog:
The right way to avoid this warning is to add the application’s folder – C:\Program Files\MyCompany\MyProduct – to the TRUSTEDPATHS system variable. We could do this explicitly, of course, but let’s see how we might also do this with an environment variable.
Inside a Command Prompt, we’re going to type “set myproddir=C:\Program Files\MyCompany\MyProduct”. We can then test this by echoing – or using dir to check the contents of – the directory referenced in %myproddir%.
This will only be set for processes that are children of this command-prompt, so after this we go to the AutoCAD Program Files folder and launch acad.exe from there. We could, of course, set this as a system-wide setting either via the Control Panel or using setx from the command-line, but for testing purposes we’d actually like to see what happens when the variable isn’t defined.
Once AutoCAD has come up, we can reference the environment variable using %myproddir% in the Trusted Paths setting:
If you close the OPTIONS dialog and reopen it, you’ll see the path has been resolved and displays as the actual location:
When you relaunch AutoCAD via our Command Prompt, you should no longer see the load warning for our custom module. However, if you launch AutoCAD from the usual shortcut – without having set the variable at the system level – you’ll see %myproddir% in the list as it remains unresolved. Which means we’re storing the name of the variable – rather than the resolved value – and attempting to resolve it on launch.
In case you want to access this system variable from the command-line, you can do so using (getenv):
Command: (getenv "myproddir")
"C:\\Program Files\\MyCompany\\MyProduct"
That’s it for this basic introduction to using environment variables. I’d be curious to hear from people on how they use them in their own applications. Please post a comment, if you have scenarios you’d like to share.
Update:
Thanks to Glenn Ryan for mentioning the possibility to use the REG_EXPAND_SZ type to allow environment variables in Registry keys to be expanded. I tried it with the above demand-loading keys and it works perfectly (assuming you’ve used the Control Panel or setx to give the myproddir variable a broader scope):
This is really what you want, to be able to use the environment variable for your application’s path in multiple places whether inside or outside AutoCAD. Thanks, Glenn!