0

我目前正在尝试运行带有一些参数的程序。我已经使用批处理文件做到了这一点。我在那里使用的命令是.\runtime\bin\python\python_mcp .\runtime\recompile.py %*

我其实是在网上找到了一个不错的启动程序功能,只是稍微调整了一下。由于我正在从一个非常不同的位置运行新程序,因此相同的命令不起作用(我想$*这是导致问题的原因!)

我试过这个和一些变化。

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

这是函数的代码:

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;
}

是的,我正在修改 Minecraft

4

2 回答 2

1

代码本身可以工作,所以我只能假设“不工作”是指第二个应用程序启动但失败退出。当您从批处理文件执行此操作%*时,命令行末尾的命令行将被删除,因为 shell 尝试将其扩展为环境变量(想想 %PATH%)。当您将它作为参数传递给 时,它作为CreateProcess附加%*参数传递并可能转发到recompile.py. 如果%*被 python 脚本解释为文件名,它将无法找到它并会以失败退出。

于 2013-04-21T20:24:34.993 回答
0

那么问题不是%*。就是需要从E:\Modding\mcp\. 所以我只需要更改调用文件的路径!

于 2013-04-21T20:48:04.670 回答