在 C++ 中尝试从 Windows 10 中的 UNC 运行 一个 EXE。C++ 的新手

In C++ Trying to Run an EXE from a UNC in Windows 10. Very new to C++

我刚开始用 C++ 编写控制台应用程序。它会进行一些验证,然后需要查找并 运行 一个可执行文件,该可执行文件可根据其部署方式位于不同的位置。所以大部分脚本都可以工作,甚至 运行 部分也可以工作,具体取决于 OS 和位置。如果它是本地的,它可以工作,如果它是 windows 7,它似乎甚至可以在 UNC 上工作。但是在 Windows 10 它就退出了。

脚本找到 exe 并从它所在的路径 运行s 它。当我将应用程序创建为批处理文件时它工作,使用 popD 移动到工作目录的 exe 位置但我似乎无法在 C++ 中模仿该功能。我试过 SetCurrentDirectory 但它不会接受我试图传递的字符串。

if (version >= minver)
    {
        std::string name = "testApp.exe";
        std::string path = (fs::current_path().string());
        for (const auto& entry : fs::recursive_directory_iterator(path))
        {
            std::string list = entry.path().string();
            int found;
            if ((found = list.find(name)) !=list.npos)
            {
                std::cout << list << std::endl;
\This is the part that sometimes works and sometimes doesn't
                system(list.c_str());

            }
        }
    }   

由于您可以从控制台启动程序,请尝试在控制台模式下使用 CreateProcess 功能:

int runProcessFromCmd( char* pcExecPath)
{

    if( NULL == pcExecPath)
    {
        printf("empty path!");
        return -1;
    }

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    memset( &si, 0, sizeof(si) );
    memset( &pi, 0, sizeof(pi) );
    si.cb = sizeof(si);

    // Start child process from command line
    // First parameter = NULL => we use cmd
    if( !CreateProcess( NULL, pcExecPath, NULL,NULL, FALSE, 0, NULL,    NULL, &si, &pi )) 
    {
        printf( "CreateProcess failed with error (%d).\n", GetLastError() );
        return -2;
    }

    // Wait child process 
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close all handles
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

所以在漫长的 运行 中,我一直在以错误的方式追查问题。感谢 LandStalker 开辟了一条更好的道路。我模仿 popD 是对的,但很难以正确的方式进行实际工作。
SetCurrentDirectory 然后在程序名称上做了一个 ShellExecute