With the following code, I experience horrible runtime:
Option Explicit
Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")
Dim Name: Name="MyVar"
Dim NewVal: NewVal="This is my value"
Services.StartTransaction "SetEnv"
ShellEnvironment (Name)=NewVal
Services.EndTransaction ("SetEnv")
Note that only the Services.* stuff is QTP-specific. The two statements generate the following run result entry, indicating the runtime for the environment variable assignment:
Transaction "SetEnv" ended with "Pass" status (Total Duration: 12.1970 sec).
This is on a very fast machine. Of course it is an unacceptable long runtime.
According to Environment.SetEnvironmentVariable takes a long time to set a variable at User or Machine level, this is because all top-level windows are notified with a 1-second timeout. I am not sure if that is C#-specific or not. Well, it obviously is not. But I don´t see how I can control this notification/timeout process under VBScript.
So generally speaking, the question is:
How can I set a USER environment variable in VBScript without getting the horrible runtime?