0

Set by another Program (started as user) my own Environmental Variable using (kernel32.dll:

 SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" ) 

So the program that sets the Environmental Variable ToTestVar_001 can read it but I do In PowerShell (started as the same user)

 Get-ChildItem Env: 

ToTestVar_001 is not listed how can I create an Environmental Variable that can be read and written by my program and PowerShell?

My next try was to create an Env. Var. by PowerShell: "TestVariable"="test Value" but my program can't find it using kernel32.dll:

string rdBuff = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx": GetEnvironmentVariableW("TestVariable", rdBuff, StringLen(rdBuff)); 

Where can I find, read and set the Kernel32.dll-Env.Variables in Win 7 64 bit - is that the problem?

Thanks in advance, Gooly

1 Answer 1

2

This is not so simple.

In the way you are writting your code Get-ChildItem Env: will see the environment var positionned by your EXE file using SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" ) only if your process (the EXE file) start PowerShell.exe (allowing his children to inherit his environment vars) after positionning the environment variable. In this case PowerShell will inherit of the environment vars and will see it.

It exists another place to set environment vars, the one in the registry I mean the one you can see using advanced system properties :

enter image description here

The environment variables for all users are all stored under:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 

The environment variables for a specific user are all stored under:

HKEY_CURRENT_USER\Environment 

But once again it's not so simple, if you change the registry and start a process all is ok, the var you set in registry can be seen in the process. But if the process is started when you add the var in the registry, then the process don't see the new environment var except if it manage a special event broadcast by the system informing that the environment vars have been modified (that is the way Explorer.exe is working).

Sign up to request clarification or add additional context in comments.

1 Comment

I found a way - finally! I 'pre-set' the Variables that I need: Start-Menu=>Computer=>Properties=>Advanced System Settings=>Environmental Variables=>System variables=>New.. That way defined I can read and write by PowerShell and my Program - oh my god. As we say in German: From the behind through the brest in the eye. Thanks you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.