Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12231

Handles for certain programs are not properly detached when launched using the CreateProcess function in Windows

$
0
0

I want to use the windows api to launch other programs (.exe, .url, .lnk) on my computer, so I use the following to do so

int main() {    //const std::wstring& programPath = L"C:\\Users\\ghost\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Steam\\Steam.lnk";    const std::wstring& programPath = L"C:\\Program Files\\Everything\\Everything.exe";    // Prepare the command line to run the program using cmd.exe    std::wstring commandLine = L"cmd.exe /c start \"\" \"" + programPath + L"\"";    // Log the command line to ensure it is correct    qDebug() << "Attempting to run command: " << commandLine;    // Prepare the STARTUPINFO structure    STARTUPINFO si;    PROCESS_INFORMATION pi;    ZeroMemory(&si, sizeof(si));    si.cb = sizeof(si);    ZeroMemory(&pi, sizeof(pi));    // Create a writable copy of the command line    std::vector<wchar_t> commandLineBuffer(commandLine.begin(), commandLine.end());    commandLineBuffer.push_back(0);    // Create the process    if (!CreateProcess(            NULL,           // Module name (use command line directly)            commandLineBuffer.data(), // Command line            NULL,           // Process handle not inheritable            NULL,           // Thread handle not inheritable            FALSE,          // Set handle inheritance to FALSE            CREATE_NO_WINDOW | DETACHED_PROCESS, // No console window, detached process            NULL,           // Use parent's environment block            NULL,           // Use parent's starting directory&si,            // Pointer to STARTUPINFO structure&pi)            // Pointer to PROCESS_INFORMATION structure        ) {        qDebug() << "Failed to create process. Error: " << GetLastError();    } else {        // Close process and thread handles to ensure they are completely detached        CloseHandle(pi.hProcess);        CloseHandle(pi.hThread);        qDebug() << "Process created successfully: " << commandLine;    }    int t;    std::cin >> t;    return 0;}

Launching .url, .lnk files with this code works fine. But when I want to use this code to launch .exe file, I found that the folder where the application is located still prompts "This folder is in use" when the application finishes running, so I used the resource monitor to find out that the handle of the application that was launched with this code still points to this folder, which led to the following problem So I used Resource Monitor to find out that the handle of the application started with the program was still pointing to this folder, which made it impossible to rename the folder.

I started the everything.exe programme using the above code, and I can see that the handle to everything.exe still points to this folder, so when I finish running this programme, I still can't delete and rename this folder. As you can see from the picture below:

enter image description here

I've also tried separating using ShellExecute, and separating using ShellExecuteEx, neither of which work

Now I want the handle to everything.exe to no longer point to this folder to make sure I can delete or rename this folder, what should I do?


Viewing all articles
Browse latest Browse all 12231

Trending Articles