Главная страница | назад





Article #16789: Detecting how a console-mode program was launched

 Question and Answer Database

FAQ1789C.txt   Detecting how a console-mode program was launched 
Category   :Windows API
Platform    :All
Product    :C++Builder  1.x

Question:
How can I tell if a console-mode application was launched from
Explorer or from a DOS box? 

Answer:
The following mechanism depends on officially undocumented
behavior which is not guaranteed to work under future versions
of Windows, but will tell you whether or not the program
was launched from Explorer or not:

#include 
#include 

bool QueryLaunchedFromExplorer() 
{
   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
   CONSOLE_SCREEN_BUFFER_INFO info;
   GetConsoleScreenBuffer(hConsole, &info);

   bool explorer = ( (info.dwCursorPosition.X==0) &&
            (info.dwCursorPosition.Y==0) );

   if ( (info.dwSize.X<=0) || (info.dwSize.Y<=0) ) 
   {
      explorer = false;
   }

   return explorer;
}

int main() 
{
    bool explorer = QueryLaunchedFromExplorer();

    if (explorer) 
    {
      cout << "Launched from Explorer" << endl;
      cin.get();
    }
    else
    {
      cout << "Launched from a DosBox" << endl;
      cin.get();
    }
    return true;
}




 

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99