Author Topic: GetOpenFileName Hook  (Read 88 times)


  • Member
  • ****
  • Posts: 538
GetOpenFileName Hook
« on: January 13, 2018, 06:01:51 AM »

I read by Msdn i can Hook the Common Dialog Box (GetOpenFileName).

I Have look here and found Gunners example (10288_Custom_Open_File_Dialog)

The code is all clear but i need to hook the new Common Dialog Box (Vista or Windows 7)

I make this new look with this flag OFN_EXPLORER .
But if i use both have i no Placesbar (listview) left side with Desktop ,Network etc.


Have you any an idea ?



  • Member
  • **
  • Posts: 115
Re: GetOpenFileName Hook
« Reply #1 on: Today at 03:58:57 AM »
the GetOpenFileName Hook is only worked with 64bit exe and dll, crashed on 32bit  exe and dll.
Code: [Select]
option casemap:none
option win64:7

include \UASM64\include\
include \UASM64\include\
include \UASM64\include\
include \UASM64\include\

includelib \UASM64\Lib\user32.lib
includelib \UASM64\Lib\kernel32.lib
includelib \UASM64\Lib\ComCtl32.lib
includelib \UASM64\Lib\ComDlg32.lib
includelib \UASM64\Lib\shlwapi.lib
includelib \UASM64\Lib\shell32.lib

EnumResNamesProc PROTO hModule:QWORD,lpszType:QWORD,lpszName:QWORD,lParam:QWORD

IDD_MAIN                equ 1000
IDC_OK                  equ 1002
IDC_CANCEL              equ 1001
IDC_IMG_LRG             equ 1003
IDC_IMG_SM              equ 1004
IDD_DLG                 equ 2000
IDC_LSV1                equ 1548
IDC_SHP1                equ 1119

_PExtractIconEx typedef proto lpszFile:qword,nIconIndex:qword,phiconLarge:qword,phiconSmall:qword,nIcons:qword
PExtractIconEx typedef ptr _PExtractIconEx

DlgFilter BYTE "All Supported Files (*.ocx;*.cpl;*.dll;*.ico;*.icl;*.exe;*.scr)", 0, "*.ocx;*.cpl;*.dll;*.ico;*.icl;*.exe;*.scr", 0
BYTE "ActiveX Controls (*.ocx)", 0, "*.ocx", 0
BYTE "Control Panel Applets (*.cpl)", 0, "*.cpl",0
BYTE "Dynamic Link Libraries (*.dll)", 0, "*.dll", 0
BYTE "Icon Files (*.ico)", 0, "*.ico",0
BYTE "Icon Libraries (*.icl)", 0, "*.icl",0
BYTE "Programs (*.exe)", 0, "*.exe", 0
BYTE "Screen Savers(*.scr)", 0, "*.scr",0,0
DlgTitle BYTE 'Browse for Icons' ,0
szExtIco BYTE    ".ico", 0

hInst QWORD   ?
himlDlg         QWORD   ?
hLVDlg          QWORD   ?
hMainDlg        QWORD   ?
hParent         QWORD   ?
hMainHeap       QWORD   ?
hImg32          QWORD   ?
hImg16          QWORD   ?
ofn             OPENFILENAME <?>
tTR RECT <?>
wLp POINT <?>
lvi             LV_ITEM <?>
DlgFilePath BYTE MAX_PATH + 1 dup (?)
hShell32_dll dq ?
@pExtractIconEx PExtractIconEx ?


Cmpi proc uses rbx rsi rdi src:QWORD,dst:QWORD

mov rsi, src ; src
mov rdi, dst ; dst
sub rax, rax ; zero eax as index
jmp @s
db   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15
db  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
db  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47
db  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
db  64, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
db 112,113,114,115,116,117,118,119,120,121,122, 91, 92, 93, 94, 95
db  96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
db 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
db 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143
db 144,145,146,147,148,149,150,151,152,153,154,155,156,156,158,159
db 160,161,162,163,164,165,166,167,168,169,170,171,172,173,173,175
db 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
db 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207
db 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
db 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
db 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
lea r8, Cmpi_tbl
movzx rdx, BYTE PTR [rsi+rax]
movzx rbx, BYTE PTR [rdi+rax]
movzx rcx, BYTE PTR [r8+rdx]
add rax, 1
cmp cl, BYTE PTR [r8+rbx]
jne @Exit ; exit on 1st mismatch with
test cl, cl ; non zero value in EAX
jnz @B

xor rax, rax ; set RAX to ZERO on match
Cmpi endp

MainDlg proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
mov eax,uMsg
invoke  GetDlgItem, hWin, IDC_IMG_LRG
mov     hImg32, rax
invoke  GetDlgItem, hWin, IDC_IMG_SM
mov     hImg16, rax
.elseif eax==WM_COMMAND
mov rdx,wParam
movzx rax,dx
shr rdx,32
.if rdx==BN_CLICKED
.if rax==IDC_OK
invoke  RtlZeroMemory, addr ofn, sizeof ofn
mov     ofn.lStructSize, sizeof OPENFILENAME
push    hWin
pop     ofn.hwndOwner
push    hInst
pop     ofn.hInstance
mov rax,offset DlgFilter
mov     ofn.lpstrFilter, rax
mov rax,offset DlgTitle
mov     ofn.lpstrTitle, rax
mov rax,offset DlgHook
mov     ofn.lpfnHook, rax
mov     ofn.lpTemplateName, IDD_DLG                
invoke  GetOpenFileName, addr ofn

.elseif rax==IDC_CANCEL
invoke SendMessage,hWin,WM_CLOSE,NULL,NULL
.elseif eax==WM_CLOSE
invoke  SendMessage, hImg32, STM_GETICON, 0, 0
invoke  DestroyIcon, rax

invoke  SendMessage, hImg16, STM_GETICON, 0, 0
invoke  DestroyIcon, rax

invoke EndDialog,hWin,0
mov rax,FALSE
mov rax,TRUE
MainDlg endp

DlgHook proc uses rsi rdi rbx hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

mov eax,uMsg
invoke LoadLibrary,CStr("shell32.dll")
.if rax
mov hShell32_dll,rax
invoke GetProcAddress,hShell32_dll,CStr("ExtractIconEx")
.if rax!=0
mov @pExtractIconEx,rax
invoke MessageBox,NULL,CStr("ExtractIconEx get Failed"),CStr("GetProcAddress"),MB_OK
invoke MessageBox,NULL,CStr("shell32.dll load Failed"),CStr("LoadLibrary"),MB_OK

; Image list for browse for icons dialog
invoke  ImageList_Create, 32, 32, ILC_MASK or ILC_COLORDDB, 2, 500
mov     himlDlg, rax

; Get handle to open file dialog
invoke  GetParent, hWin
mov     rbx, rax
mov     hParent, rax
; I don't want these 3 controls, so hide em
; Hide file name static control
invoke  SendMessage, rax, CDM_HIDECONTROL, stc3, 0
; Hide file name edit control
invoke  SendMessage, rbx, CDM_HIDECONTROL, edt1, 0
; Hide ok button
invoke  SendMessage, rbx, CDM_HIDECONTROL, IDOK, 0

; Now move the next 3 controls up
; Adjust file type static top
invoke RtlZeroMemory,addr tTR,sizeof RECT
invoke RtlZeroMemory,addr wLp,sizeof POINT

invoke  GetDlgItem, rbx, stc2
mov     rdi, rax
invoke  GetWindowRect, rdi, offset tTR
invoke  ScreenToClient, rbx, offset wLp
mov     eax, wLp.x
add     eax, tTR.left
mov     esi, wLp.y
add     esi,
sub     esi, 30
invoke  SetWindowPos, rdi, 0, eax, esi, 0, 0, SWP_NOSIZE

; Adjust file type combo top
invoke  GetDlgItem, rbx, cmb1
mov     rdi, rax
invoke  GetWindowRect, rdi, offset tTR
mov     eax, wLp.x
add     eax, tTR.left
sub     esi, 4
invoke  SetWindowPos, rdi, 0, eax, esi, 0, 0, SWP_NOSIZE

; Adjust cancel button top
invoke  GetDlgItem, rbx, IDCANCEL
mov     rdi, rax

invoke  GetWindowRect, rdi, offset tTR
mov     eax, wLp.x
add     eax, tTR.left
sub     esi, 4
invoke  SetWindowPos, rdi, 0, eax, esi, 0, 0, SWP_NOSIZE

; Get handle to our listview
invoke  GetDlgItem, hWin, IDC_LSV1
mov     hLVDlg, rax

; Set image list to listview
invoke  SendMessage, hLVDlg, LVM_SETIMAGELIST, LVSIL_NORMAL, himlDlg

push    hWin
pop     hMainDlg
.elseif eax==WM_COMMAND
mov rdx,wParam
mov rax,qword ptr [edx]
shr rdx,32
.if rdx==BN_CLICKED
.if rax==IDOK

.elseif rax==IDCANCEL
invoke  SendMessage, hWin, WM_CLOSE, NULL, NULL
.elseif eax == WM_NOTIFY
.if wParam == IDC_LSV1
mov     rax, lParam
.if dword ptr(NMHDR ptr [rax]).code == LVN_ITEMCHANGING
.if dword ptr(NMLISTVIEW ptr [rax]).uNewState == LVIS_FOCUSED or LVIS_SELECTED
invoke  ImageList_GetIcon, himlDlg, dword ptr(NMLISTVIEW ptr [rax]).iItem, ILD_TRANSPARENT
mov hIcon32,rax
invoke  SendMessage, hImg32, STM_SETICON, rax, 0
invoke  CopyImage, hIcon32, IMAGE_ICON, 16, 16, LR_COPYFROMRESOURCE
mov hIcon16,rax
invoke  SendMessage, hImg16, STM_SETICON, hIcon32, 0
invoke  DestroyIcon, hIcon32
invoke  DestroyIcon, hIcon16

invoke  PostMessage, hParent, WM_CLOSE, 0, 0
jmp     PassThrough
jmp     PassThrough
mov     rax, lParam
.if dword ptr(NMHDR ptr [rax]).code == CDN_SELCHANGE
; Get path of selected file
mov     byte ptr [DlgFilePath], 0
invoke RtlZeroMemory,addr DlgFilePath,MAX_PATH + 1
invoke  SendMessage, hParent, CDM_GETFILEPATH, MAX_PATH + 1, offset DlgFilePath
invoke  PathFindExtension, offset DlgFilePath
invoke  Cmpi, rax, offset szExtIco
test    rax, rax
jz      @F
call    ShowIcons
jmp     PassThrough
.elseif dword ptr(NMHDR ptr [rax]).code == CDN_FILEOK
invoke  PathFindExtension, offset DlgFilePath
invoke  Cmpi, rax, offset szExtIco
.if rax == 0
; Icon was selected, show 32 and 16 sizes
invoke  LoadImage, NULL, offset DlgFilePath, IMAGE_ICON, 32, 32, LR_LOADFROMFILE
mov hIcon32,rax
invoke  SendMessage, hImg32, STM_SETICON,hIcon32, 0

invoke  CopyImage, hIcon32, IMAGE_ICON, 16, 16, LR_COPYFROMRESOURCE
mov hIcon16,rax
invoke  SendMessage, hImg16, STM_SETICON, hIcon16, 0
invoke  DestroyIcon, hIcon32
invoke  DestroyIcon, hIcon16
; close the dialog
invoke  SetWindowLong, hWin, DWL_MSGRESULT, FALSE
; not an ico file, leave dialog open
invoke  SetWindowLong, hWin, DWL_MSGRESULT, TRUE
jmp     PassThrough   
.elseif eax==WM_CLOSE
invoke  ImageList_Destroy, himlDlg
invoke  DestroyWindow, hWin
invoke FreeLibrary,hShell32_dll
mov rax, FALSE
mov rax, TRUE

DlgHook endp

ShowIcons proc

; Clear imagelist
invoke  ImageList_Remove, himlDlg, -1

; Clear listview
invoke  SendMessage, hLVDlg, LVM_DELETEALLITEMS, 0, 0
; is the path a directory?  If yes, leave
invoke  PathIsDirectory, offset DlgFilePath
test    rax, rax
jnz     Done
; Get icon count
invoke  @pExtractIconEx, offset DlgFilePath,-1,NULL,NULL,NULL
test    rax, rax
jnz     @F
invoke  SetDlgItemInt, hMainDlg, 2002, eax, FALSE
jmp     Done
invoke  SetDlgItemInt, hMainDlg, 2002, eax, FALSE
; We have icons! load the file
invoke  LoadLibraryEx, offset DlgFilePath, 0, LOAD_LIBRARY_AS_DATAFILE
test    rax, rax
jz      Done
mov     hLib, rax

; Now get all the icon names
invoke  EnumResourceNames, rax, RT_GROUP_ICON, offset EnumResNamesProc, 0
invoke  FreeLibrary, hLib
ShowIcons endp

mov   rax, IcoID
shr   rax, 32

dqtoa proc uvar:QWORD,pbuffer:QWORD
invoke wsprintf,pbuffer,CStr("%d"),uvar
dqtoa endp

MemCopy proc uses rsi rdi dqS_addr:QWORD,dqD_addr:QWORD,dq_Size:QWORD

mov rsi, dqS_addr
mov rdi, dqD_addr
mov rcx, dq_Size

shr rcx, 3
rep movsq

mov rcx, dq_Size
and rcx, 7
rep movsb


MemCopy endp

EnumResNamesProc proc hModule:QWORD,lpszType:QWORD, lpszName:QWORD, lParam:QWORD

invoke  LoadImage, hModule, lpszName, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR
mov     IconTemp, rax
invoke  ImageList_ReplaceIcon, himlDlg, -1, IconTemp
invoke  DestroyIcon, IconTemp
invoke  ImageList_GetImageCount, himlDlg
sub     rax, 1
mov     lvi.mask_, LVIF_IMAGE or LVIF_TEXT
mov     lvi.iItem, eax
mov     lvi.iSubItem, 0
mov     lvi.iImage, eax
invoke  HeapAlloc, hMainHeap, HEAP_ZERO_MEMORY, 50
mov     LVParam, rax

invoke RtlZeroMemory,LVParam,50
invoke  XIS_INTRESOURCE, lpszName
test    rax, rax
jz      @F
invoke  lstrlen, lpszName
invoke  MemCopy, lpszName, LVParam, rax
jmp     @Continue
invoke  dqtoa, lpszName, LVParam
        mov     rax, LVParam
        mov     lvi.pszText, rax

        invoke  SendMessage, hLVDlg, LVM_INSERTITEM, 0, addr lvi
        invoke  HeapFree, hMainHeap, 0, LVParam

        mov     rax, TRUE
EnumResNamesProc endp

start Proc
invoke GetModuleHandle,NULL
mov hInst,  rax
invoke  GetProcessHeap
mov     hMainHeap, rax
invoke DialogBoxParam,hInst,IDD_MAIN,NULL,addr MainDlg,NULL
invoke ExitProcess,NULL

start Endp


#include <\UASM64\include\resource.h>
#define MANIFEST 24
#define IDD_MAIN 1000
#define IDC_OK 1002
#define IDC_CANCEL 1001
#define IDC_IMG_LRG 1003
#define IDC_IMG_SM 1004
#define IDD_DLG 2000
#define IDC_LSV1 1548
#define IDC_SHP1 1119
#define IDC_STC1 2001
#define IDC_STC2 2002

CAPTION "DialogApp"
FONT 8,"Tahoma",0,0,0
STYLE 0x10CA0800
  CONTROL "Choose Icon",IDC_OK,"Button",0x50010000,108,48,54,13
  CONTROL "Close",IDC_CANCEL,"Button",0x50010000,48,48,54,13
  CONTROL "",IDC_IMG_LRG,"Static",0x50000203,9,9,32,32
  CONTROL "",IDC_IMG_SM,"Static",0x50000203,51,17,16,16

IDD_DLG DIALOGEX 10,150,288,116
FONT 8,"MS Sans Serif",0,0,0
STYLE NOT 0x10000000|0x44000404
EXSTYLE 0x00000008
  CONTROL "",IDC_LSV1,"SysListView32",0x50010000,3,18,281,84,0x00000200
  CONTROL "",IDC_SHP1,"Static",NOT 0x10000000|0x40000004,3,3,279,14
  CONTROL "Totall Icons:",IDC_STC1,"Static",0x50000000,6,105,42,9
  CONTROL "",IDC_STC2,"Static",0x50000000,57,105,51,9


  • Member
  • *****
  • Posts: 7883
  • Assembler is fun ;-)
    • MasmBasic
Re: GetOpenFileName Hook
« Reply #2 on: Today at 04:19:14 AM »
The hook works in 32-bit code. FileOpen$() uses it.