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

C++ CreateProcess - System Error #2 can't find file - what is wrong with my file path?

本文关键字:文件 路径 什么 我的 问题 找不到 创建 进程 系统错误 C++      更新时间:2023-10-16

我正在尝试通过 Firefox 打开一个 PDF 与 CreateProcess() ,我是一个初学者,对使用 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;
}