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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "util_win32.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
)
99 /* FIXME: MinGW cannot compile this code */
104 DWORD CoffHeaderOffset
;
106 /* DWORD MoreDosHeader[16]; */
108 IMAGE_DOS_HEADER image_dos_header
;
109 IMAGE_FILE_HEADER image_file_header
;
110 IMAGE_OPTIONAL_HEADER image_optional_header
;
111 /* IMAGE_SECTION_HEADER image_section_header; */
113 /* Open the EXE file - Use Native API for SHARE compatibility */
114 hImage
= CreateFile(a_szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
115 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
116 if (hImage
== INVALID_HANDLE_VALUE
) {
117 win32Trace (("win32_GetEXEType: Could not open file %s. API Error %d.", a_szFileName
, GetLastError()));
121 /* Read the MZ (DOS) image header. */
122 win32APICALL( ReadFile (hImage
, (LPVOID
)&image_dos_header
, sizeof(IMAGE_DOS_HEADER
), &dwDumm
, NULL
) );
124 switch (image_dos_header
.e_magic
) {
125 case IMAGE_DOS_SIGNATURE
: /* MZ or ZM */
126 case IMAGE_DOS_SIGNATURE_ALTERNATIVE
:
129 case IMAGE_OLDPHARLAP_SIGNATURE
: /* MP, P2, P3: Phar Lap executables */
130 case IMAGE_NEWPHARLAP_286_SIGNATURE
:
131 case IMAGE_NEWPHARLAP_386_SIGNATURE
:
135 return EXE_otherCUI
; /* Probably .COM? */
138 /* Read more MS-DOS header. */
139 /* win32APICALL( ReadFile (hImage, MoreDosHeader, sizeof(MoreDosHeader)); */
141 /* Get new executable header */
142 CoffHeaderOffset
= SetFilePointer(hImage
, image_dos_header
.e_lfanew
, NULL
, FILE_BEGIN
);
143 /* + sizeof(ULONG); */
144 win32APICALL( ReadFile (hImage
, (LPVOID
) &wSignature
, sizeof(WORD
), &dwDumm
, NULL
) );
146 switch (wSignature
) {
147 case IMAGE_PE_SIGNATURE
: /* PE - Portable Executable */
149 case IMAGE_OS2_SIGNATURE
: /* NE - New Executable OS/2 and Windows 3.x */
150 case IMAGE_OS2_SIGNATURE_LE
: /* LE - Linear Execuable (Windows 3.x) */
151 case IMAGE_LX_SIGNATURE
: /* LX - Linear Execuable (OS/2) */
154 return EXE_Unknown
; /* unknown New Executable or bad pointer */
158 /* Continue parsing PE (COFF-like) */
159 SectionOffset
= CoffHeaderOffset
+ IMAGE_SIZEOF_FILE_HEADER
+ IMAGE_SIZEOF_NT_OPTIONAL_HEADER
;
161 win32APICALL( ReadFile(hImage
, (LPVOID
) &image_file_header
, IMAGE_SIZEOF_FILE_HEADER
, &dwDumm
, NULL
) );
163 /* Read optional header. */
164 win32APICALL( ReadFile(hImage
, (LPVOID
) &image_optional_header
, IMAGE_SIZEOF_NT_OPTIONAL_HEADER
, &dwDumm
, NULL
) );
166 switch (image_optional_header
.Subsystem
) {
167 case IMAGE_SUBSYSTEM_WINDOWS_GUI
:
170 case IMAGE_SUBSYSTEM_WINDOWS_CUI
:
171 case IMAGE_SUBSYSTEM_OS2_CUI
:
172 case IMAGE_SUBSYSTEM_POSIX_CUI
:
175 case IMAGE_SUBSYSTEM_UNKNOWN
:
176 case IMAGE_SUBSYSTEM_NATIVE
:
177 return EXE_Unknown
; /* FIXME: what is "NATIVE??" */
179 win32Trace(("Unknown type %u.\n", image_optional_header
.Subsystem
));
184 #endif /* !__MINGW32__ */