Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Path Variable Permanently Using ShellExecuteEx

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

like image 382
Aspen Avatar asked Aug 20 '26 17:08

Aspen


1 Answers

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.

like image 182
David Heffernan Avatar answered Aug 23 '26 12:08

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!