0

I have wrote an initialization script that sets user environment variables which are keys that have been hashed and encrypted...Once the keys have been created the key encryption exe is no longer required. I want to launch the main application and remove the init file containing the hashing and key encryption functions.

I am not having any trouble with any of the above...Everything works as should when independent of each other. The problem is that in order for the main application to have access to the newly created environment variables I need the init script to completely exit...

Everything I have tried, Popen with flags, os.system() and others have still left me in a situation where the parent process ends and the main application launches, however, the environment variables have not updated...I close and relaunch main.py and...boom the program sees the updated variables and all is fine.

All I want is the init script to run, spawn a new process that is not linked at all with init.py and then exit so it can be removed. I thought this would be simple but after many hours of head scratching and trying numerous things, I am still no closer.

If I have to I will simply bundle it as two separate .exe files but I wanted it to be a one click install type thing.

I am running windows 10 and this can be platform specific.

Links looked at:

How to stop/terminate a python script from running?

Using a Python subprocess call to invoke a Python script

Starting a separate process

https://docs.python.org/2/library/subprocess.html

Python: Howto launch a full process not a child process and retrieve the PID

And more...

Current closest result

p = Popen(["python","UserInterface.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE,
      creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
Paul Hashmi
  • 456
  • 5
  • 18

1 Answers1

1

Create an environment block, set the environment variable using SetEnvironmentVariable, and use CreateProcess to specify this environment block for the created process.

MSDN DOC:

To specify a different environment for a process, create a new environment block and pass the pointer to it as a parameter to the CreateProcess function.

...

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30