BrainStone
BrainStone

Reputation: 3205

C++ - Running a program without using system converting from a batch file

I'm currently trying to run a program with some arguments. I already did this using a batch file. The command I used there was .\runtime\bin\python\python_mcp .\runtime\recompile.py %*

I actually found a good start program function on the web, which I just adjusted a little bit. Since I'm running the new program from a very different location the same command does not work (I guess it's the $* which is causing the problems!)

I tried this and some variations.

ExecuteProcess(L"E:\\Modding\\mcp\\runtime\\bin\\python\\python_mcp.exe", L"E:\\Modding\\mcp\\runtime\\recompile.py %*");

Here is the code of the function:

size_t ExecuteProcess(wstring FullPathToExe, wstring Parameters = L"", size_t SecondsToWait = -1)
{
    size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
    DWORD dwExitCode = 0;
    std::wstring sTempStr = L"";

    /* - NOTE - You should check here to see if the exe even exists */

    /* Add a space to the beginning of the Parameters */
    if (Parameters.size() != 0)
    {
        if (Parameters[0] != L' ')
        {
            Parameters.insert(0,L" ");
        }
    }

    /* The first parameter needs to be the exe itself */
    sTempStr = FullPathToExe;
    iPos = sTempStr.find_last_of(L"\\");
    sTempStr.erase(0, iPos +1);
    Parameters = sTempStr.append(Parameters);

     /* CreateProcessW can modify Parameters thus we allocate needed memory */
    wchar_t * pwszParam = new wchar_t[Parameters.size() + 1];
    if (pwszParam == 0)
    {
        return 1;
    }
    const wchar_t* pchrTemp = Parameters.c_str();
    wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp);

    /* CreateProcess API initialization */
    STARTUPINFOW siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);

    if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()),
                            pwszParam, 0, 0, false,
                            CREATE_DEFAULT_ERROR_MODE, 0, 0,
                            &siStartupInfo, &piProcessInfo) != false)
    {
         /* Watch the process. */
        dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait < 0) ? INFINITE : (SecondsToWait * 1000));
    }
    else
    {
        /* CreateProcess failed */
        iReturnVal = GetLastError();
    }

    /* Free memory */
    delete[]pwszParam;
    pwszParam = 0;

    /* Release handles */
    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);

    return iReturnVal;
}

And yes, I'm Modding Minecraft

Upvotes: 0

Views: 226

Answers (2)

BrainStone
BrainStone

Reputation: 3205

Well the problem was not the %*. It was that the python script needs to be called from E:\Modding\mcp\. So I just need to change the path from where the file is called!

Upvotes: 0

Captain Obvlious
Captain Obvlious

Reputation: 20073

The code itself works so I can only assume that by "does not work" you mean the second application launches but exits with failure. When you execute this from a batch file the %* at the end of the command line gets removed as the shell tries to expand it as an environment variable (think %PATH%). When you pass it as an argument to CreateProcessthe %* is passed as an additional parameter and likely forwarded to recompile.py. If %* is interpreted as a filename by the python script it won't be able to find it and will exit with failure.

Upvotes: 1

Related Questions