Question

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??

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top