Home > Windows API > Type of Exe(Console, Windows, MS-DOS application).

Type of Exe(Console, Windows, MS-DOS application).

How to find out the type of an executable file, i.e. whether it’s a windows application or a console application or an MS-DOS application…

void GetFileType( LPCTSTR lpctszFilePath_i, CString& csFileType_o )
{
    // Get exe file type
    const DWORD dwRetVal = SHGetFileInfo( lpctszFilePath_i,
                                          FILE_ATTRIBUTE_NORMAL,
                                          0,
                                          0,
                                          SHGFI_EXETYPE );
    if( dwRetVal )
    {
        /*
           READ THIS!! dwRetVal is interpreted as follows...

           LOWORD = NE or PE and HIWORD = 3.0, 3.5, or 4.0  Windows application
           LOWORD = MZ and HIWORD = 0  MS-DOS .exe, .com, or .bat file
           LOWORD = PE and HIWORD = 0  Win32 console application
        */

        const WORD wLowWord =  LOWORD( dwRetVal );
        const WORD wHiWord = HIWORD( dwRetVal );
        const WORD wPEWord = MAKEWORD( 'P', 'E' );
        const WORD wMZWord = MAKEWORD( 'M', 'Z' );
        const WORD wNEWord = MAKEWORD( 'N', 'E' );

        // Read above comments to understand what's happening
        if( wLowWord == wPEWord || wLowWord == wNEWord )
        {
            if( wHiWord == 0 )
            {
                csFileType_o = _T( "Win32 Console Application" );
            }
            else
            {
                csFileType_o = _T( "Windows application" );
            }
        }
        else if( wLowWord == wMZWord && wHiWord == 0 )
        {
            csFileType_o = _T( "MS-DOS .exe, .com or .bat file" );
        }
        else
        {
            csFileType_o = _T( "Unknown file type" );
        }// End if
    }// End if
}// End GetFileType
Advertisements
  1. Huong
    January 14, 2009 at 5:53 pm

    Thank you!!!

  2. January 4, 2009 at 2:34 am

    Hi, no licensing terms, just a thank you message will do. 😉

  3. Huong
    December 30, 2008 at 10:26 pm

    Are there licensing terms for this code?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: