gpt4 book ai didi

c++ - 批处理文件不适用于启动命令

转载 作者:行者123 更新时间:2023-11-30 03:02:18 24 4
gpt4 key购买 nike

我需要从 C++ 运行一个执行 .exe 应用程序的批处理文件,.exe 是一个名为 dumpedid 的免费程序,它将监视器的 edid 写入文本文件。我使用了 CreateProcess,只要批处理文件不使用启动命令,它就可以工作

"DumpEDID.exe" > "edid.txt" // this works
start /wait /d "DumpEDID.exe" > "edid.txt" // this doesn't

但是我想使用/wait 命令,所以这是个问题。这可能只是应用程序本身的问题吗?我运行批处理文件的代码如下

std::ofstream fs;
fs.open(DUMP_EDID_BATCH_FILE_PATH);
fs << "\"" << DUMP_EDID_EXE_PATH << "\" > \"" << DUMP_EDID_TXT_FILE_PATH << "\"";
fs.close();

STARTUPINFOA si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

std::string str = (std::string)"/c " + "\"" + DUMP_EDID_BATCH_FILE_PATH + "\"";
char * cmdLine = new char[str.size() + 1];
strncpy(cmdLine,str.c_str(), str.size());
cmdLine[str.size()] = '\0';

if( !CreateProcessA( "c:\\Windows\\system32\\cmd.exe",
cmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) )
{
delete[] cmdLine;
DWORD errCode = GetLastError();
g_log.Info(_T("CreateProcess failed - error code %d"), errCode);
return errCode;
}

delete[] cmdLine;

WaitForSingleObject( pi.hProcess, INFINITE );

CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

谢谢。

编辑 - 我只是尝试直接从命令行运行批处理文件,我发现有些奇怪,批处理文件的内容是

开始/wait/d "D:...\dumpedid"> "D:...\edid.txt"

但是当我运行批处理文件时出现的命令是

开始/wait/d "D:...\dumpedid"1>"D:...\edid.txt"

注意 > 符号前的 1 和 1 前的额外空格

最佳答案

start 需要注意两点。

title 选项是第一个引用的参数,因此如果您有一个引用的 cmd,您还需要一个 title 选项。
启动“myTitle”/启动“cmd”

如果您尝试重定向输出,您需要逃避重定向,因为正常 重定向将重定向start 命令的输出,而不是启动命令的输出!

start ""/wait/d "DumpEDID.exe"^> "edid.txt"

关于c++ - 批处理文件不适用于启动命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com