Issues with CreateProcess and incorrect parameters

  7zip, c++, createprocess, hash

This may be 2 questions, but they’re broadly both revolving around CreateProcess and it not quite functioning right.

I’ve been working on an application that gathers together files, processes them, and then zips them as a final step, renaming the zipped file with the hash of the directory that I had zipped. To accomplish this, I’m using a standalone copy of 7zip (7za.exe), by using CreateProcess to create / zip the archive, and a separate program called DirHash to produce the name of the archive I’m trying to make.

The problem I’m having is that neither of these programs are working properly. I’m currently running DirHash with the flags -t "temp.txt" -nowait -quiet -overwrite", and it does create a file called temp.txt, however, the file is always empty when running it using CreateProcess. When I use the exact same parameters on the standard command line, it produces the correct output.

The other issue is that 7zip seems to be erroring when trying to zip my directories. When run through my CreateProcess, I get an "Unsupported Command" error, and the files don’t get zipped. However, when I use the exact same parameters on the command line, the archive is created successfully.

Here’s the relevant code for DirHash, the same code is used for 7za. I have confirmed that the values given to CreateProcess are correct, and the parameters are the same as the ones I use on the command line.

auto hashLocation = searchPath + "" + DIRHASH_NAME;
auto params = """ + targetDir + "" MD5" + " -quiet -t "temp.txt" -nowait -overwrite";


// I know this part is gross and if there's any suggestions for how to do it better I'm willing to hear it.        
std::wstring intermediate;
intermediate.assign(params.begin(), params.end());
LPWSTR trueParams = &intermediate[0];

std::wstring intermediate_ex;
intermediate_ex.assign(hashLocation.begin(), hashLocation.end());
LPCWSTR trueLocation = intermediate_ex.c_str();

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);


// Ripped from stack overflow
bool success = CreateProcess(
    trueLocation,
    trueParams,
    NULL,
    NULL,
    TRUE,
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
    );
    // Close process and thread handles. 
WaitForSingleObject(pi.hProcess, INFINITE);
if (!success)
{
    addLog("Failed to run DirHash process.", ErrorLevel::ERROR_MESSAGE);
    return ERROR_STR;
}

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

Source: Windows Questions C++

LEAVE A COMMENT