.data? 
        Bff BROWSEINFO <?>
.code

; ParenthWnd is the Parents handle
; EditBuff is the pointer to a text buffer
; WhichTitle is the Title to use..
;               ...1 for the Original Folder
;               ...2 for the Patched Folder
; Editid is an id of an editbox if we wish to set the text of it to the selected path
;               otherwise set as NULL 

BrowseForFolder proc ParenthWnd:DWORD, EditBuff:DWORD, WhichTitle:DWORD, Editid:DWORD

        LOCAL g_pMalloc:LPMALLOC
        LOCAL pidlParent:LPITEMIDLIST
        LOCAL Errors:DWORD
        
        szText szTitle0,"Select..."
        szText szTitle1,"Original Folder"
        szText szTitle2,"Patched Folder"
        szText szError0,"Shell Browse Error"
        szText szError1,"Failed to get an IMalloc interface"
        szText szError2,"You Clicked Cancel !!!"
        szText szError3,"Failed to retrieve the path from the IDList"

            mov Errors, 0       
            invoke CoInitialize, 0

            invoke  SHGetMalloc, addr g_pMalloc
            .if eax == E_FAIL
                mov Errors, 1
                jmp errors
            .endif    

        ; initialise  BrowseForFolder 
            mov eax,ParenthWnd
            mov Bff.hwndOwner,eax
            mov eax, EditBuff
            mov Bff.pszDisplayName, eax
            .if WhichTitle == 1
                mov Bff.lpszTitle, offset szTitle1
            .elseif WhichTitle == 2    
                mov Bff.lpszTitle, offset szTitle2
            .endif
            mov Bff.ulFlags,BIF_DONTGOBELOWDOMAIN or BIF_RETURNONLYFSDIRS ; or BIF_STATUSTEXT

        ; browse folder === IDLIST in pidlParent
            

            invoke SHBrowseForFolder,addr Bff
            .if eax == NULL
                mov Errors, 2
                jmp errors
            .endif
            mov pidlParent,eax


        ; desktop folder pointer to shell folder in psfParent
                    
            invoke SHGetPathFromIDList, pidlParent, EditBuff
            .if eax == NULL
                mov Errors, 3
                jmp errors
            .endif    
            
            
            
            .if Editid != NULL
                invoke SetDlgItemText,ParenthWnd, Editid,EditBuff
            .endif    

errors:
        .if Errors == 1
                mov eax, offset szError1
                invoke MessageBoxA,hWnd,eax,addr szError0,MB_ICONERROR
                jmp error1
        .elseif Errors == 2
                mov eax, offset szError2
                invoke MessageBoxA,hWnd,eax,addr szError0,MB_ICONERROR
                jmp error2
        .elseif Errors == 3
                mov eax, offset szError3
                invoke MessageBoxA,hWnd,eax,addr szError0,MB_ICONERROR
                jmp error3
        .endif


error3:

; failed to retrieve the path from idlist


        mov eax, g_pMalloc
        mov eax, [eax]
        invoke (IMalloc PTR [eax]).Free, g_pMalloc, pidlParent  

error2:
; user pressed cancel on the BrowseForFolder dialogBox        

        mov eax, g_pMalloc
        mov eax, [eax]
        invoke (IMalloc PTR [eax]).Release, g_pMalloc  
error1:
; failed to retrieve imalloc interface
        invoke CoUninitialize
        ret
BrowseForFolder endp