<tfoot id='vJPDy'></tfoot>

    <legend id='vJPDy'><style id='vJPDy'><dir id='vJPDy'><q id='vJPDy'></q></dir></style></legend>

    1. <i id='vJPDy'><tr id='vJPDy'><dt id='vJPDy'><q id='vJPDy'><span id='vJPDy'><b id='vJPDy'><form id='vJPDy'><ins id='vJPDy'></ins><ul id='vJPDy'></ul><sub id='vJPDy'></sub></form><legend id='vJPDy'></legend><bdo id='vJPDy'><pre id='vJPDy'><center id='vJPDy'></center></pre></bdo></b><th id='vJPDy'></th></span></q></dt></tr></i><div id='vJPDy'><tfoot id='vJPDy'></tfoot><dl id='vJPDy'><fieldset id='vJPDy'></fieldset></dl></div>
        <bdo id='vJPDy'></bdo><ul id='vJPDy'></ul>

      <small id='vJPDy'></small><noframes id='vJPDy'>

      如何检查程序是否从控制台运行?

      时间:2023-07-31

          <small id='tqPPU'></small><noframes id='tqPPU'>

            <tbody id='tqPPU'></tbody>
          <legend id='tqPPU'><style id='tqPPU'><dir id='tqPPU'><q id='tqPPU'></q></dir></style></legend>

            <i id='tqPPU'><tr id='tqPPU'><dt id='tqPPU'><q id='tqPPU'><span id='tqPPU'><b id='tqPPU'><form id='tqPPU'><ins id='tqPPU'></ins><ul id='tqPPU'></ul><sub id='tqPPU'></sub></form><legend id='tqPPU'></legend><bdo id='tqPPU'><pre id='tqPPU'><center id='tqPPU'></center></pre></bdo></b><th id='tqPPU'></th></span></q></dt></tr></i><div id='tqPPU'><tfoot id='tqPPU'></tfoot><dl id='tqPPU'><fieldset id='tqPPU'></fieldset></dl></div>

              <bdo id='tqPPU'></bdo><ul id='tqPPU'></ul>

              <tfoot id='tqPPU'></tfoot>

              1. 本文介绍了如何检查程序是否从控制台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在编写一个应用程序,它将一些诊断信息转储到标准输出.

                I'm writing an application which dumps some diagnostics to the standard output.

                我想让应用程序以这种方式工作:

                I'd like to have the application work this way:

                • 如果它是从独立命令提示符(通过 cmd.exe)运行或将标准输出重定向/管道到文件,则在完成后立即退出,
                • 否则(如果它是从一个窗口运行并且控制台窗口是自动生成的),那么另外在窗口消失之前等待按键退出(让用户阅读诊断)
                • If it is run from a standalone command prompt (via cmd.exe) or has standard output redirected/piped to a file, exit cleanly as soon as it finished,
                • Otherwise (if it is run from a window and the console window is spawned automagically), then additionally wait for a keypress before exiting (to let the user read the diagnostics) before the window disappears

                我该如何区分?我怀疑检查父进程可能是一种方法,但我并不是真的很喜欢 WinAPI,因此这个问题.

                我在 MinGW GCC 上.

                I'm on MinGW GCC.

                推荐答案

                您可以使用 GetConsoleWindow,GetWindowThreadProcessId 和 GetCurrentProcessId 方法.

                You can use GetConsoleWindow, GetWindowThreadProcessId and GetCurrentProcessId methods.

                1) 首先,您必须使用 GetConsoleWindow 函数检索控制台窗口的当前句柄.

                1) First you must retrieve the current handle of the console window using the GetConsoleWindow function.

                2) 然后你会得到控制台窗口句柄的进程所有者.

                2) Then you get the process owner of the handle of the console window.

                3) 最后,将返回的 PID 与应用程序的 PID 进行比较.

                3) Finally you compare the returned PID against the PID of your application.

                检查此示例(VS C++)

                Check this sample (VS C++)

                #include "stdafx.h"
                #include <iostream>
                using namespace std;
                #if       _WIN32_WINNT < 0x0500
                  #undef  _WIN32_WINNT
                  #define _WIN32_WINNT   0x0500
                #endif
                #include <windows.h>
                #include "Wincon.h" 
                
                int _tmain(int argc, _TCHAR* argv[])
                {   
                    HWND consoleWnd = GetConsoleWindow();
                    DWORD dwProcessId;
                    GetWindowThreadProcessId(consoleWnd, &dwProcessId);
                    if (GetCurrentProcessId()==dwProcessId)
                    {
                        cout << "I have my own console, press enter to exit" << endl;
                        cin.get();
                    }
                    else
                    {
                        cout << "This Console is not mine, good bye" << endl;   
                    }
                
                
                    return 0;
                }
                

                这篇关于如何检查程序是否从控制台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:多次加载同一个dll 下一篇:如何运行需要管理员权限的应用程序

                相关文章

                最新文章

              2. <i id='AygBr'><tr id='AygBr'><dt id='AygBr'><q id='AygBr'><span id='AygBr'><b id='AygBr'><form id='AygBr'><ins id='AygBr'></ins><ul id='AygBr'></ul><sub id='AygBr'></sub></form><legend id='AygBr'></legend><bdo id='AygBr'><pre id='AygBr'><center id='AygBr'></center></pre></bdo></b><th id='AygBr'></th></span></q></dt></tr></i><div id='AygBr'><tfoot id='AygBr'></tfoot><dl id='AygBr'><fieldset id='AygBr'></fieldset></dl></div>

                1. <tfoot id='AygBr'></tfoot>
                      <bdo id='AygBr'></bdo><ul id='AygBr'></ul>
                    <legend id='AygBr'><style id='AygBr'><dir id='AygBr'><q id='AygBr'></q></dir></style></legend>
                  1. <small id='AygBr'></small><noframes id='AygBr'>