3

I come up with this code. It will execute correctly and return true. but it doesn't change Path variable's value. When I type like this --> setx Path "C:\Program Files\Java\jdk1.7.0_02\bin\" in cmd, it works and change the Path value

here is the code

// Prepare shellExecutInfo
SHELLEXECUTEINFO ShRun = {0};
ShRun.cbSize = sizeof(SHELLEXECUTEINFO);
ShRun.fMask = SEE_MASK_NOCLOSEPROCESS;
ShRun.hwnd = NULL;
ShRun.lpVerb =NULL;
ShRun.lpFile = "C:\\Windows\\System32\\setx.exe";
ShRun.lpParameters = "Path \"\"\"C:\\Program Files\\Java\\jdk1.7.0_02\\bin\\\"\"\"";
ShRun.lpDirectory =NULL;
ShRun.nShow = SW_SHOWNORMAL;
ShRun.hInstApp = NULL;

// Execute the file with the parameters
if(ShellExecuteEx(&ShRun))
    printf("done");
else
    printf("no");

what will be the problem here??

Aspen
  • 53
  • 1
  • 9

1 Answers1

1

Your quoting on the arguments is wrong. You have too many quotes. You need to write

ShRun.lpParameters = "Path \"C:\\Program Files\\Java\\jdk1.7.0_02\\bin\\\"";

To see that your version will fail I did the following experiment at the console:

C:\Users\heff>setx path """C:\Program Files\Java\jdk1.7.0_02\bin\"""
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

I also note that you are using SEE_MASK_NOCLOSEPROCESS. Normally you do that so that you can then wait on the process handle that is returned. You don't appear to be doing that. What's more, you don't appear to be closing the process handle which is your responsibility when you use SEE_MASK_NOCLOSEPROCESS.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I fixed the quoting but result is same – Aspen Apr 01 '13 at 09:54
  • I get a security dialog when I compile the code into a simple `int main()` program using g++. When I compile with the MS compiler or call `ShellExecute` from a Delphi app, it's all good. Which compiler are you using? – David Heffernan Apr 01 '13 at 10:08
  • You might want to mention the "destroyed in 10 seconds" failure mode of changing the PATH environment variable like that. Lots of extremely hard to diagnose problems when the Windows directories are no longer on the PATH. You'd better check your own machine too if you actually did this ;) The damage isn't immediately obvious, it requires a log-out to make the new setting effective. – Hans Passant Apr 01 '13 at 12:37
  • @Hans It just modifies the user variable which is then merged with the system variable. And I was super careful. Rapid Environment Editor was used. – David Heffernan Apr 01 '13 at 12:43
  • @DavidHeffernan I'm using gcc compiler in MinGW32 – Aspen Apr 01 '13 at 16:39
  • @HansPassant I Restart machine but it not effected – Aspen Apr 01 '13 at 16:40
  • Thank you @HansPassant for helping me. I'm able to fix that issue using ShellExecute. Thanks Again – Aspen Apr 01 '13 at 16:52
  • @Aspen What do you mean? Hans didn't talk about `ShellExecute`. What issue did you fix? – David Heffernan Apr 01 '13 at 19:01
  • @DavidHeffernan I mean I fix my issue using ShellExecute.Issue is what my early code didn't set the path correctly. now it sets the path correctly. – Aspen Apr 02 '13 at 00:40