【问题标题】:CreateProcess in C closes console too fast [closed]C中的CreateProcess关闭控制台太快[关闭]
【发布时间】:2025-11-30 04:00:01
【问题描述】:

我在 C 中使用 createProcess 打开命令提示符以执行 jar 。我已经弄清楚了所有的问题,但为了调试,想以某种方式添加一些控制台不闪烁并立即消失的东西。

我正在寻找的是要执行的 jar 和命令窗口在它终止之前等待我按下按钮。

这仅用于调试,我将在将其发布之前将其删除,以免控制台窗口挂起并杀死所有内容。

    if (!CreateProcess(NULL,   // No module name (use command line)
        "java -jar testfile.jar",        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi)           // Pointer to PROCESS_INFORMATION structure
        ){
            MessageBox(0, "test jar passed", "jar executed", 1);
            return 1;
        }
    else {
        printf("create process call to jar was sucessful");
        MessageBox(0,"jar failed", "Error Test", 1);
        return 0;
    }
}

我没有使用 IDE,而是安装在不同的系统上,所以我需要创建 dll。无法使用 IDE 功能在此处停止命令提示符

【问题讨论】:

标签: c multithreading


【解决方案1】:

要停止控制台闪烁,您可以使用getch()

getch() 等待用户输入字符。如果没有getch(),可以使用getche()getchar()等其他功能。但与getch() 不同的是,它需要一个额外的换行符。

我认为它解决了你的问题。

【讨论】:

  • 我会试试这个
  • getchconio 中的一个 MSVC 函数。您可以使用标准的getchar - 只需点击Enter 即可完成程序执行。没有额外的 newline.
  • @SkrewEverything 它不起作用,只是闪现并退出
  • @Praveen 你在哪里添加了getch()?在您的代码中,return 是指main() 吗?如果是,您需要在两个返回语句之前添加getch()
  • @SkrewEverything 我在消息框之前的 else 部分中添加了它。没错吧?