The proper way.

  1. Create two batch files, one with setx commands, one with the miner
  2. Launch separate batch with setx
  3. Logoff or reboot
  4. Launch separate batch with the miner
Ok.. let's settle this once and for all ; ) And turns out that it even worse than I thought.

Test scenario #1

Windows 2008 R2, 6xGPU, GPU_MAX_ALLOC_PERCENT is not set.
 
Standard "recommended" batch with setx in the batch is used.
Environment window is opened before launching the script.
GPU_MAX_ALLOC_PERCENT is set in the miner batch file on Windows Server 2008 R2
 
First run.
cgminer fails if GPU_MAX_ALLOC_PERCENT is set in the miner batch file
Second run results in the same error.
Reboot / logoff - all good.
 
Ok.. Win2k8 r2 is different.. blah blah..

Test scenario #2

Windows 7, 6xGPU, GPU_MAX_ALLOC_PERCENT is not set.
 
GPU_MAX_ALLOC_PERCENT is not set at all.
GPU_MAX_ALLOC_PERCENT is not set
 
As expected, cgminer throws error.
cgminer reports Error -61 if GPU_MAX_ALLOC_PERCENT is not set
 
ENTER is pressed.
cgminer crashes if GPU_MAX_ALLOC_PERCENT is not set
 
Standard "recommended" batch with setx in the batch is used. Environment Variables window is opened after execution, so it shows "GPU_MAX_ALLOC_PERCENT = 100". However cgminer still crashes.
GPU_MAX_ALLOC_PERCENT is set in the miner batch file on Windows 7
 
WTF?? Ok, close, run again!
cgminer reports Error -61 if GPU_MAX_ALLOC_PERCENT is set
 
Ok. Windows sux and Bill Gates must burn in hell. And *nix is free. Blah-blah.
Let's re-install Windows. I did not, but I've removed GPU_MAX_ALLOC_PERCENT and re-booted ; )
GPU_MAX_ALLOC_PERCENT is not set
 
And get the same error..
cgminer reports Error -61 if GPU_MAX_ALLOC_PERCENT is set
 
And this is just funny -
yes, it is funny, not in ha-ha way, but still
That's why I personally never had issue with setx. Because I use FAR and it re-reads environment every time it is started, like CMD does and it also can be configured to auto-update environment variables.
And that's why I said that it is "even worse", I thought that you just need to launch separate batch with setx commands and after it launch batch with the miner(still true if launched from CMD), however turns out that FAR was re-reading Environment variables for me.
 
At this point, I should scream "MOMMY HELP!!" and start crying aka open new thread on the forum.
And a lot of mommies(no offense to a1) will start saying something like "use Linux/BAMT/CentOS", "buy new motherboard", "what PSU do you have?", "Windows sux", "re-install Windows", "re-install driver" and "hire certified electrician to check wiring at your place".
And god forbid some bounty is mentioned..
Some of the proposed solutions will lead to re-boot or logoff(I hope it will be the one involving certified electrician) and Environment variables will be read from the registry on logon ; )

But seriously, WTF is going on??

setx test batch
 
Eh??
setx test batch result
 
And launch again -
setx test batch second launch result
 
let's logoff or re-boot. Complete circuit re-wiring requires first to turn everything off ; )
setx test batch after re-wiring result
OMG, Turns out it was the power issue!! I got my place completely re-wired and it works now, THANKS!!11

How does setx work?

Setx modifies Windows registry - HKEY_CURRENT_USER\Environment and broadcasts(HWND_BROADCAST) setting change event(WM_SETTINGCHANGE) with "Environment" parameter.
Windows Explorer listens to this message and updates it's environment variables.
 
When process is launched, environment variables are copied to the process memory space.
Environment gets propagated from parent to child.
 
When user logins to the system, explorer.exe(let's call it main explorer) launches and gets a copy of the Environment.
Main explorer responds to the Environment change event.
 
In Win95 and Win98, no built-in apps, like Windows Explorer or Program Manager, respond to the Environment change event.
That's why in Win95/98 software installation manuals(Delphi comes to mind) it was mentioned to log off - thus restart explorer.exe.
 
When cmd.exe is started from W->Run or from the Desktop or from the Taskbar it is the child of main explorer.
And since main explorer responds to Env. changes, Env. of the cmd will reflect changes, granted if CMD was started after the changes.
 
 
If Tools->Folder Options->View->Launch folder windows in a separate process is ticked then when new folder window is opened, anew instance of the Windows Explorer is launched, let's call it "slave" explorer.
And "slave" explorer does NOT respond to the Environment change event, broadcasted by setx.
So any child of the "slave" explorer will NOT get its Environment updated.
Or maybe it does respond but never gets it.
 
Second, slave explorer.exe
On this screenshot, "main" explorer had PID 2036 and "slave" explorer - 4128.
Maybe svchost(644) breaks the messaging, looks like it's DCOM Server, or maybe services.exe(472).

What happens.

Scenario #1 - Windows Explorer - Launch folder windows in a separate process is ticked
  1. PC starts
  2. Windows boots
  3. User logins
  4. Main explorer is started
  5. Main explorer gets Environment Variables, GPU_MAX_ALLOC_PERCENT is set if it is there
  6. User opens folder with the cgminer
  7. Slave explorer instance is launched
  8. Slave explorer instance gets Environment Variables, GPU_MAX_ALLOC_PERCENT is set if it is there
  9. batch with setx is started
  10. Slave explorer spawns CMD.exe child and passes Environment Variables to the CMD, which handles the batch
  11. setx modifies Environment Variables, sets GPU_MAX_ALLOC_PERCENT, sends the event, Main explorer gets it
  12. cgminer starts
  13. cgminer tries to load GPUs using AMD OpenCL
  14. OpenCL checks GPU_MAX_ALLOC_PERCENT, which was read on step#8 - there is none - default is used
  15. OpenCL reports, that GPUs do not support required amount of RAM for specified TC
  16. cgminer reports -61 error and crashes
  17. At this point user might repeat from step#9 - won't work for same reason as on step#15
 
Or at step#18 user might close folder with the cgminer. After some time slave explorer process will die ; )
And if the folder with the cgminer is opened again - new slave explorer will be created - and it will get updated Environment.
Or if user launches cgminer batch not from the folder, but from the Desktop shortcut or from Taskbar or maybe from the Start Menu(objects handled by the main explorer) if repeated from step#9 - it will get updated Environment.
 
Scenario#2 - CMD - Launch folder windows in a separate process - does not matter
  1. CMD is started via Start->Run(started by the main explorer)
  2. CMD reads Environment Variables, GPU_MAX_ALLOC_PERCENT gets read if it is there
...
  1. Same shit ; )
  2. At this point user might repeat from step#1 - and it will work
 
Scenario#3 - FAR configured to auto-update Environment, via Shift+Enter
  1. Launch separate batch with setx commands - CMD starts
  2. setx saves variables to registry - CMD finishes
  3. Launch miner batch
  4. FAR updates Environment
  5. CMD starts
Now it is clear, that both Windows 2008 R2 and Windows 7 from Test Scenario #1 and #2 are configured with "Launch folder windows in a separate process".

Some visual aid.

#1 launched from Windows Explorer(separate process) -> #2 - launched again -> #3 - launched from W->Run -> #4 - launched from W->Run->cmd
testing setx

Conclusion.

  1. Including setx command in the batch file in the worst case will mislead the user that it "works"(value always gets saved, but not always gets read after).
  2. In the best case - will do nothing, it will overwrite specified environment value at each batch launch.
 
How to be sure that updated environment variables are being used.
  1. Launch separate batch with setx, logoff // reboot, launch separate batch with the miner.
    Or
  2. Launch separate batch with setx
    1. If Tools->Folder Options->View->Launch folder windows in a separate process is NOT ticked - launch batch with the miner from whatever place
      Or
    2. If Tools->Folder Options->View->Launch folder windows in a separate process IS ticked
      1. Launch batch with the miner via Start -> Run or in the freshly opened CMD window via Start -> Run or in any other child of the main explorer
      2. Kill slave explorer via Task Manager or wait until it dies on its own
      Or
    3. Use FAR
 
Do note, that neither of those solutions are using setx in the miner batch, ofc there is a way to include them in the batch.. but it's just wrong anyway ; )
At this point it should be absolutely clear which way is the safest and guaranteed to work 100% on any Windows.
 
References
http://support.microsoft.com/kb/104011
http://superuser.com/questions/593949/why-wont-my-windows-8-command-line-update-its-path
http://support.microsoft.com/kb/263105
http://support.microsoft.com/kb/195050
http://superuser.com/questions/215376/when-should-i-activate-the-launch-folder-windows-in-a-separate-process-option
http://windows.microsoft.com/en-us/windows-vista/change-folder-views-and-behavior
http://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w
http://stackoverflow.com/questions/573817/where-are-environment-variables-stored-in-registry
 
p.s.
I wish it was that easy to demonstrate what may happen if several PSUs are not connected "properly".
Though, intentionally blowing up stuff is fun ; )

BTC: 14kzZJuzRZ27crZNdQgG2kQor6RUmtMTCL
LTC: LgVc7KdedPGZyDXHXEH9G7z6AoTmTvDdWb