Reputation: 2064
I am using a C++ application to launch another process. This process happens to be java so I need to run javaw.exe. However I want my application to work on any windows system with a compatible java version installed and in the windows search path.
I am launching my process with CreateProcess, however the first argument requires the FULL path of the executable and does not search the windows search path.
I would like to find the full path of the javaw.exe from the windows search path in my CPP code to then pass to CreateProcess so that I can appropriately start and later, via TerminateProcess. Stop the external program.
How do I find the full path of javaw.exe via windows search path?
Edit: this question is different. than the referenced duplicate because my specific question is to use the windows search path (which btw does includes the local directory, the PATH env, and some standard locations). And I want to do this via CPP specifically. I specifically want to use the windows search path system since users may have more than one javaw.exe and I want which one is used to be predictable via windows standard search paths and not a custom search method I implement.
Upvotes: 4
Views: 2538
Reputation: 2064
There is a Win32 API function called SearchPath which (depending on registry settings) searches the local directory first, then the windows PATH variable. You can optionally add another directory to search. More details can be found in the documentation on MSDN.
Here is some example code:
LPSTR lpFilePart;
char filename[MAX_PATH];
if(!SearchPath( NULL, "javaw", ".exe", MAX_PATH, filename, &lpFilePart))
{
//error handling here
}
std::cout<<"The path is " << filename<<std::endl;
Upvotes: 4