1 /* Utilities - Win32 utilities (Windows NT and Windows '95)
2 Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
4 Written 1996 by Juan Grigera<grigera@isis.unlp.edu.ar>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "util.win32.h"
24 #include "util.debug.h"
26 /* int win32_GetPlatform ()
27 Checks in which OS Midnight Commander is running.
30 OS_WinNT - Windows NT 3.x
31 OS_Win95 - Windows 4.x
33 Note: GetVersionEx (Win32API) is called only once.
35 int win32_GetPlatform ()
37 static int platform
= 0;
39 return (platform
? platform
: (platform
= win32_GetVersionEx()) );
42 /* int win32_GetVersionEx ()
43 intended for use by win32_GetPlatform only
45 int win32_GetVersionEx ()
49 ovi
.dwOSVersionInfoSize
= sizeof(ovi
);
50 win32APICALL( GetVersionEx(&ovi
) );
52 return ovi
.dwPlatformId
;
55 /* int win32_GetEXEType (const char* filename)
56 Determines whether filename (an Executable) is
57 a Console application (CUI) or a Graphical application(GUI).
59 filename - Name of executable file to check
61 Returns: EXE_win16 - Windows 3.x archive or OS/2
62 EXE_win32CUI - NT or Chicago Console API, also OS/2
63 EXE_win32GUI - NT or Chicago GUI API
64 EXE_otherCUI - DOS COM, MZ, ZM, Phar Lap
66 EXE_Error - Couldn't read file/EXE image
68 TODO: better management of OS/2 images
69 EXE_CompressedArchive can be easily implemented
70 Notes: This function parses the executable header (the only ugly way
71 to do it). If header is not found or not understood,
74 Information on NE, LE, LX and MZ taken from Ralf Brown's interrupt
75 list, under INT 21-function 4B ("EXEC" - LOAD AND/OR EXECUTE PROGRAM),
78 Parsing of PE header (Win32 signature, "Portable Executable")
79 taken from MSKBase article Number: Q90493.
82 /* ---- Executable Signatures ---- */
84 /* Alternative DOS signagure */
85 #define IMAGE_DOS_SIGNATURE_ALTERNATIVE 0x4D5A /* ZM */
87 /* Phar Lap .EXP files */
88 #define IMAGE_OLDPHARLAP_SIGNATURE 0x504D /* MP */
89 #define IMAGE_NEWPHARLAP_286_SIGNATURE 0x3250 /* P2 */
90 #define IMAGE_NEWPHARLAP_386_SIGNATURE 0x3350 /* P3 */
93 #define IMAGE_LX_SIGNATURE 0x584C /* LX */
94 #define IMAGE_PE_SIGNATURE 0x4550 /* PE */
97 int win32_GetEXEType (const char* a_szFileName
)
102 DWORD CoffHeaderOffset
;
104 /* DWORD MoreDosHeader[16]; */
106 IMAGE_DOS_HEADER image_dos_header
;
107 IMAGE_FILE_HEADER image_file_header
;
108 IMAGE_OPTIONAL_HEADER image_optional_header
;
109 /* IMAGE_SECTION_HEADER image_section_header; */
111 /* Open the EXE file - Use Native API for SHARE compatibility */
112 hImage
= CreateFile(a_szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
113 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
114 if (hImage
== INVALID_HANDLE_VALUE
) {
115 win32Trace (("win32_GetEXEType: Could not open file %s. API Error %d.", a_szFileName
, GetLastError()));
119 /* Read the MZ (DOS) image header. */
120 win32APICALL( ReadFile (hImage
, (LPVOID
)&image_dos_header
, sizeof(IMAGE_DOS_HEADER
), &dwDumm
, NULL
) );
122 switch (image_dos_header
.e_magic
) {
123 case IMAGE_DOS_SIGNATURE
: /* MZ or ZM */
124 case IMAGE_DOS_SIGNATURE_ALTERNATIVE
:
127 case IMAGE_OLDPHARLAP_SIGNATURE
: /* MP, P2, P3: Phar Lap executables */
128 case IMAGE_NEWPHARLAP_286_SIGNATURE
:
129 case IMAGE_NEWPHARLAP_386_SIGNATURE
:
133 return EXE_otherCUI
; /* Probably .COM? */
136 /* Read more MS-DOS header. */
137 /* win32APICALL( ReadFile (hImage, MoreDosHeader, sizeof(MoreDosHeader)); */
139 /* Get new executable header */
140 CoffHeaderOffset
= SetFilePointer(hImage
, image_dos_header
.e_lfanew
, NULL
, FILE_BEGIN
);
141 /* + sizeof(ULONG); */
142 win32APICALL( ReadFile (hImage
, (LPVOID
) &wSignature
, sizeof(WORD
), &dwDumm
, NULL
) );
144 switch (wSignature
) {
145 case IMAGE_PE_SIGNATURE
: /* PE - Portable Executable */
147 case IMAGE_OS2_SIGNATURE
: /* NE - New Executable OS/2 and Windows 3.x */
148 case IMAGE_OS2_SIGNATURE_LE
: /* LE - Linear Execuable (Windows 3.x) */
149 case IMAGE_LX_SIGNATURE
: /* LX - Linear Execuable (OS/2) */
152 return EXE_Unknown
; /* unknown New Executable or bad pointer */
156 /* Continue parsing PE (COFF-like) */
157 SectionOffset
= CoffHeaderOffset
+ IMAGE_SIZEOF_FILE_HEADER
+ IMAGE_SIZEOF_NT_OPTIONAL_HEADER
;
159 win32APICALL( ReadFile(hImage
, (LPVOID
) &image_file_header
, IMAGE_SIZEOF_FILE_HEADER
, &dwDumm
, NULL
) );
161 /* Read optional header. */
162 win32APICALL( ReadFile(hImage
, (LPVOID
) &image_optional_header
, IMAGE_SIZEOF_NT_OPTIONAL_HEADER
, &dwDumm
, NULL
) );
164 switch (image_optional_header
.Subsystem
) {
165 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
168 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
169 case IMAGE_SUBSYSTEM_OS2_CUI
:
170 case IMAGE_SUBSYSTEM_POSIX_CUI
:
173 case IMAGE_SUBSYSTEM_UNKNOWN
:
174 case IMAGE_SUBSYSTEM_NATIVE
:
175 return EXE_Unknown
; /* FIXME: what is "NATIVE??" */
177 win32Trace(("Unknown type %u.\n", image_optional_header
.Subsystem
));