.386
.model	flat, stdcall
option	casemap :none

include		windows.inc
include		user32.inc
include		kernel32.inc
include		comdlg32.inc
includelib	user32.lib
includelib	kernel32.lib
includelib	comdlg32.lib

DlgProc		PROTO		:DWORD,:DWORD,:DWORD,:DWORD
Validate	PROTO		:DWORD

.data
strFilter	db		"Executable Files (*.exe, *.dll)",0,
				"*.exe;*.dll",0,"All Files",0,"*.*",0,0 
OpenError	db		"Unable to open target file",0
MsgBoxCap	db		"Results",0
Invalid		db		"This is not a valid PE file!!",0
Valid		db		"This is a valid PE file!!",0

.data?
ofn		OPENFILENAME	<>
hInstance	HINSTANCE	?
hTarget		dd		?
hMapping	dd		?
pMapping	dd		?
TargetName	db		512 dup(?)

.const
IDD_MAIN	equ		1001
IDC_TARGET	equ		1003
IDC_OPEN	equ		1004
IDC_GO 		equ		1005
IDC_EXIT 	equ		1006
ARIcon		equ		2001

.code
start:
	invoke	GetModuleHandle, NULL
	mov	hInstance, eax
	invoke	DialogBoxParam,hInstance,IDD_MAIN,0,addr DlgProc,0
	invoke	ExitProcess, eax

DlgProc proc hWin:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
	mov	eax,uMsg
	.if	eax==WM_INITDIALOG
		invoke	LoadIcon,hInstance,2001
		invoke	SendMessage,hWin,WM_SETICON,1,eax
	.elseif eax==WM_COMMAND
		mov	eax,wParam
		.if	eax==IDC_OPEN
			mov ofn.lStructSize,SIZEOF ofn 
			mov ofn.lpstrFilter,offset strFilter
			mov ofn.lpstrFile,offset TargetName 
			mov ofn.nMaxFile,512 
			mov ofn.Flags,OFN_FILEMUSTEXIST+OFN_PATHMUSTEXIST+\
						OFN_LONGNAMES+OFN_EXPLORER+OFN_HIDEREADONLY 
			invoke GetOpenFileName,addr ofn
			.if eax==TRUE
				invoke SetDlgItemText,hWin,IDC_TARGET,addr TargetName
				invoke RtlZeroMemory,addr TargetName,512
			.endif
		.elseif eax==IDC_GO
			invoke GetDlgItemText,hWin,IDC_TARGET,addr TargetName,512
			invoke lstrlen,addr TargetName
			.if eax!=0
				invoke Validate,addr TargetName
			.endif
		.elseif eax==IDC_EXIT
			invoke SendMessage,hWin,WM_CLOSE,0,0
		.endif
	.elseif	eax==WM_CLOSE
		invoke	EndDialog,hWin,0
	.endif
	xor	eax,eax
	ret
DlgProc endp

Validate proc FileName:DWORD
	invoke CreateFile,FileName,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0 
	.if eax!=INVALID_HANDLE_VALUE
		mov hTarget,eax
		invoke CreateFileMapping,eax,0,PAGE_READ,0,0,0 
		mov hMapping,eax
		invoke MapViewOfFile,eax,FILE_MAP_READ,0,0,0 
		mov pMapping,eax 
		.if [eax.IMAGE_DOS_HEADER.e_magic]==IMAGE_DOS_SIGNATURE
			add eax,[eax.IMAGE_DOS_HEADER.e_lfanew]
			.if [eax.IMAGE_NT_HEADERS.Signature]==IMAGE_NT_SIGNATURE
				invoke MessageBox,0,addr Valid,addr MsgBoxCap,MB_ICONASTERISK
			.endif 
		.else 
			invoke MessageBox,0,addr Invalid,addr MsgBoxCap,MB_ICONASTERISK
		.endif 
		invoke UnmapViewOfFile,pMapping
		invoke CloseHandle,hMapping
		invoke CloseHandle,hTarget
	.else
		invoke MessageBox,0,addr OpenError,0,0
	.endif
	xor eax,eax
	Ret
Validate EndP

end start