Template question - How to get an environment variable's value (e.g. %ProgramFiles(x86)%)

Hi,
I would like to get the value of “%ProgramFiles(x86)%” inside a template. I even thought this should be easy :grinning:

I thought that the following would work:

#GROUP(%oGetProgramFilesX86)
    #DECLARE(%jProgramFiles)        
    #SET(%jProgramFiles,EVALUATE('%ProgramFiles(x86)%'))
    #!
    #! If OS is 32bits, we need to check %ProgramFiles% instead....
    #IF(%jProgramFiles = '%ProgramFiles(x86)%')    
       #SET(%jProgramFiles,EVALUATE('%ProgramFiles%'))
    #ENDIF
    #RETURN(%jProgramFiles)

But that, or various variations of that I have tried, didn’t work.

You can probably use #RUNDLL to call a external DLL to return the value.

I’m curious, what are you going to do with the value in your template?

What are trying to do exactly?

Get the folder contents or the folder path to the CSIDL or check if the folder exists?

Edit. This should give you an idea if you want the CSIDL folder paths.

Hi,
I am exploring this because noyantis codejock templates (version 6) go to a sub-folder under “%ProgramFiles(x86)%” to get the activex’s license codes.

That doesn’t work on my machine because my windows is in Spanish. So instead of
“c:\program files (x86)” mine is “c:\archivos de programa (x86)”. In the end, I have to enter each control’s license manually for each application that uses the templates (as far as I can tell).

This problem got me wondering about how to get environment variables from inside the templates.

My screen shot in my previous post shows you its possible and the right monitor shows you some of the code that does it.

HTH. :grinning:

Thanks @anon23294430!

I was kind of hoping that the environment variables could be “expanded” from inside template code, without having to use an external DLL.

Regards…

Anything that is returned by a windows api will need to be called using the #RunDll, so if you want to call (D)COM+ objects, you’ll need to write a (D)COM+ dll in clarion first then call it from the template using #RunDLL.

Put the value you want into a text file in the project folder and then read that in your template. Not perfect, but might work for what you want.

Thats a good point and there’s a couple ways of doing it.

The Redirection file can be used to store macro symbols, but you would need to parse the redirection file into a template, and then process the symbols in template code which is a lot more work than the other solutions suggested below.

The other way is using the template code itself and there are a couple of ways to do that.
#Equate(%MyProgramFilesPath,‘c:\archivos de programa (x86)’)
or
#Declare(%MyProgramFilesPath)
#Set(%MyProgramFilesPath,‘c:\archivos de programa (x86)’)

Can you use GETREG from the template language ?
If so, you could read
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)


If not, here’s how an example of a DLL written in clarion, for the purpose of being called by a Template.
ClarionCommunity/CW/Templates/WrapRun at master · MarkGoldberg/ClarionCommunity (github.com)

As you can see, it’s not much source at all.
Don’t forget to alter the .EXP


If you want to read an Environment Variable in CW

  MAP
     MODULE('APIs')
       ! I am pretty sure I got this Arnor Baldvinson in the 90s

       GetEnvironmentVariable(  |
            *CSTRING lpName,    |  !// address of environment variable name
            *CSTRING lpBuffer,  |  !// address of buffer for variable value
            ULONG nSize),       |  !// size of buffer, in characters
            ULONG, RAW, PASCAL, |
            NAME('GetEnvironmentVariableA')
    END 
   END 


GetEnvVar            PROCEDURE(STRING pEnvironmentVariable)!!,String
Dw  ULONG
En  CSTRING(256)
Rn  CSTRING(256)
S   ULONG
  CODE
  En = CLIP(pEnvironmentVariable)
  Rn = ''
  S  = SIZE(Rn)
  Dw = GetEnvironmentVariable(En,Rn,S)
  RETURN Rn