C++ CreateProcess - 系统错误 #2 找不到文件 - 我的文件路径有什么问题?

标签 c++ windows

我正在尝试使用 CreateProcess() 通过 Firefox 打开 PDF,我是初学者并且对使用 CreateProcess 一无所知,但在我的最后一个问题中有人指出了它的 MSDN ...它表明:

To run a batch file, you must start the command interpreter; 
set lpApplicationName to    cmd.exe and set lpCommandLine to the 
following arguments: /c plus the name of the batch file.

因此,我创建了一个批处理文件,它可以使用 system() 命令完美运行,批处理文件没有任何问题。

我不明白为什么系统找不到文件,不知道是批处理文件,批处理文件中的exe,批处理文件中的PDF文档还是cmd.exe的位置。 .. 非常感谢任何帮助...

void openPDF(char scansQueue[][MAX_NAME], int index)
{
// build batch file
char openPath[300];
char procCommand[MAX_NAME]="C:\\firefox";
char cmdEXE[MAX_NAME]="C:\\Windows\\System32\\cmd.exe";
fstream outfile;
outfile.open("C:\\firefox.bat");
copyCString(openPath,"\"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe\"");
outfile << openPath;
outfile << ' ';
copyCString(openPath,"\"C:\\Scans\\");
catArray(openPath,scansQueue[index]);
catArray(openPath,"\"");

STARTUPINFOW si; 
    PROCESS_INFORMATION pi; 
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
cout<<"PROCESS ATTEMPT"<<endl;
if(!CreateProcess((LPCTSTR)cmdEXE ,(LPWSTR)procCommand, NULL, NULL, false, 0, NULL, NULL, &si, &pi))cout << GetLastError();cout<<"PROCESS FAILED TO EXECUTE!!!";
}

最佳答案

这假设整个批处理文件是 XY 问题的一部分,因为您实际上并不需要制作批处理文件,您真的只想使用命令行参数启动 Firefox。

我还假设您真的不需要传递带有要使用的索引的整个文件名数组,而应该像我调用函数时那样单独传递文件名。

#include <Windows.h>
#include <stdio.h>

void MsgBoxLastError()
{
    LPWSTR lpMsgBuf = NULL;
    if(FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPWSTR)&lpMsgBuf,
        0, NULL ) != 0)
    {
        MessageBox(NULL, lpMsgBuf, L"Error", MB_OK);
    }
    LocalFree(lpMsgBuf);
}

void OpenWithFirefox(const char* Filename)
{
    const WCHAR pathToFirefox[] = L"C:/Program Files (x86)/Mozilla Firefox/firefox.exe";
    const WCHAR scanPrefix[] = L"file://C:/Scans/";
    WCHAR fullCommandLine[MAX_PATH] = {0};

    //Build full command line
    swprintf_s(fullCommandLine, L"\"%s\" \"%s%S\"", pathToFirefox, scanPrefix, Filename);

    STARTUPINFOW si; 
    PROCESS_INFORMATION pi; 
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    BOOL success = CreateProcess(NULL, fullCommandLine, NULL, NULL, false, 0, NULL, NULL, &si, &pi);
    if(success)
    {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
    else
    {
        MsgBoxLastError();
    }
}

int main()
{
    const int MAX_NAME = 13;
    char scansQueue[][MAX_NAME] =
    {
        "file1.pdf",
        "file2.pdf"
    };

    for(int i = 0; i < 2; ++i)
    {
        OpenWithFirefox(scansQueue[i]);
    }
    return 0;
}

关于C++ CreateProcess - 系统错误 #2 找不到文件 - 我的文件路径有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18809338/

相关文章:

c++ - 有关Facade模式实现的快速问题

c++ - OpenGL 上的多线程渲染

c++ - 系统参数信息(SPI_GETFONTSMOOTHINGTYPE)返回 0

python - Windows 10在启动时运行python程序

windows - 将 .bat 代码包含到 vbs 中

c - sprintf 的一件有趣的事

java - 告诉eclipse重新加载环境变量

c++ - STL accumulate() 函数

c++ - 访问数组中的派生类的问题

c++ - 成员函数指针权限