Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

File Lock lookup tool for Microsoft Servers

  • Filter
  • Time
  • Show
Clear All
new posts

  • File Lock lookup tool for Microsoft Servers

    this is a stripped down version of a tool i made to find out who has
    a file locked on a file server.

    after you find out the userid that has the file locked, you can use my
    idlookup.bas program to get a name and more info about that user id.
    it is posted at:


    '    inuseby.bas        - by william burns   revised on 11/2002
    ' this program will search an nt type file server for file locks
    #compile exe
    #register none
    #dim all
    #include ""
    #include ""
    %file                                   = 500
    %chgpdc                                 = 510
    %separator_515                          = 515
    %butclose                               = 520
    %butfname                               = 525
    %help                                   = 600
    %about                                  = 605
    %frame1                                 = 100
    %label1                                 = 105
    %label2                                 = 110
    %txtfile                                = 115
    %txtsrv                                 = 120
    %listbox1                               = 125
    declare callback function dlgproc
    declare function netfileenum lib "netapi32.dll" alias "netfileenum" (lpserver as any, lpbasepath as any, username as any, _
                                     byval level as dword, byval lpbuffer as dword, byval prefmaxlen as dword, lpentriesread as dword, lptotalentries as dword, lpresumehandle as dword) as dword
    global hdlg    as long    ' dialog handle
    global hmenu0&
    global hmenu1&
    global hmenu2&
    function addlist(smymsg as string) as long
          local iret        as long
          static ilstwidth  as long
          if len(smymsg) > ilstwidth then
             ilstwidth = len(smymsg)
             control send hdlg, %listbox1, %lb_sethorizontalextent, (ilstwidth * 9), 0
          end if
          listbox add hdlg, %listbox1, smymsg
    end function
    function uniptrtoansi(byval uniptr as long) as string
      dim ilen   as long
      dim stext  as string
      ilen = lstrlenw(byval uniptr)     'get wide text length
      stext = peek$(uniptr, ilen * 2)   'get wide text from memory
      function = acode$(stext)          'return ascii text
    end function
    function files_in_use(byval sfiles as string, byval sserver as string) as long
       local lret           as long
       local zservername    as string * 200
       local zfilename      as string * 200
       local fi             as dword  ' holds pointer to system buffer
       local fitmp          as file_info_3 ptr
       local dnumread       as dword
       local dmaxlen        as dword
       local dtotalentries  as dword
       local hresume        as dword
       local icounter       as dword
       local stext          as string
       zservername = ucode$(sserver) + chr$(0) + chr$(0)
       zfilename   = ucode$(sfiles) + chr$(0) + chr$(0)
       dmaxlen = %maxdword
       lret = netfileenum(zservername,zfilename, byval 0 ,byval 3, byval varptr(fi), dmaxlen, dnumread, dtotalentries, hresume)
       function = lret
       select case lret
          case 0   'worked...
             'msgbox "total found = " + str$(dnumread),,"test"
             if dnumread < 1 then
                addlist "no file locks found!"
                exit select
             end if
             for icounter = 0 to dnumread -1
                fitmp = fi + (icounter * 20)  'move pointer to next array (4 bytes in a dword) * 5 feilds
                if @fitmp.fi3_path_name <> 0 then
                   if uniptrtoansi(@fitmp.fi3_path_name) <> " then stext = uniptrtoansi(@fitmp.fi3_path_name)
                   if (@fitmp.fi3_permissions and %perm_file_write) = %perm_file_write then stext = stext + " {has write access}"
                   if (@fitmp.fi3_permissions and %perm_file_read) = %perm_file_read then stext = stext + " {has read access}"
                   if (@fitmp.fi3_permissions and %perm_file_create) = %perm_file_create then stext = stext + " {has create access}"
                   if @fitmp.fi3_user_name <> 0 and uniptrtoansi(@fitmp.fi3_path_name) <> " then stext = stext + " in use by " + uniptrtoansi(@fitmp.fi3_user_name)
                   if stext <> " then addlist stext
                end if
             next icounter
          case %error_more_data
             addlist "more data waiting.  return code= " + str$(lret)
          case else
             addlist "error searching server.  ret= " + str$(lret)
       end select
       call netapibufferfree(byval varptr(fi))
    end function
    callback function dlgproc
       local iret           as long
       local sfile          as string
       local sname          as string
       local stext          as string
       static sserver as string
       local zbuffer        as asciiz * %max_path
       select case cbmsg
          case %wm_initdialog
             iret = getprofilestring("maner" & chr$(0), "fpsrv" & chr$(0), "\s-sp-fp1" & chr$(0), zbuffer, byval %max_path) ' get default settings from win.ini..
             sserver = trim$(zbuffer,chr$(0,23))
             control set text hdlg, %txtsrv, sserver
          case %wm_close
             call writeprofilestring("maner", "fpsrv", sserver & chr$(0))       'store in win.ini
          case %wm_command
             select case cbctl
                case  %idok
                   if cbctlmsg=%bn_clicked then
                      control get text hdlg, %txtfile to sfile
                      listbox reset hdlg, %listbox1
                      if left$(sserver,2) = "\" then
                         call files_in_use(sfile, sserver)
                         msgbox "invalid server!   make sure server starts with \",,"error"
                      end if
                   end if
                case  %listbox1
                case  %txtsrv
                   if cbctlmsg=%en_change then
                      control get text hdlg, %txtsrv to sserver   'update var
                   end if
                case  %about
                   msgbox "this utility will check if a file has file locks on a server. " + $crlf + $crlf _
                         + "to use:" + $crlf + $crlf _
                         + "1.) type in the directory path (path at the server) to list all locked files in it." + $crlf _
                         + "      example:  d:\data\common\" + $crlf + $crlf _
                         + "2.) type name of server to check." + $crlf _
                         + "      note: servername must start with \ " + $crlf _
                         + "      example: \s-sp-fp1" + $crlf + $crlf _
                         + "3.) click on the lookup button.  " + $crlf + $crlf + $crlf _
                         ,%mb_iconinformation,"inuseby    ver 1.5    by william burns"
                case  %butclose
                      dialog end hdlg
                case else
             end select
          case else
       end select
    end function
    function pbmain
       local style&
       style& = %ws_popup or %ds_modalframe or %ws_caption or %ws_minimizebox or %ws_sysmenu or %ds_center
       dialog new 0, "in use by who?", 0, 0,  240,  177, style&, 0 to hdlg
       menu new bar to hmenu0&
       menu new popup to hmenu1&
       menu add popup, hmenu0& ,"&file", hmenu1&, %mf_enabled
       menu add string, hmenu1&, "&lookup file lock info",  %idok, %mf_enabled
       menu add string, hmenu1&, "-",  %separator_515, %mf_enabled
       menu add string, hmenu1&, "e&xit",  %butclose, %mf_enabled
       menu new popup to hmenu2&
       menu add popup, hmenu0& ,"&help", hmenu2&, %mf_enabled
       menu add string, hmenu2&, "&about",  %about, %mf_enabled
       menu attach hmenu0&, hdlg
       control add frame, hdlg,  %frame1,  "file lock information", 3, 42, 235, 121, %ws_child or %ws_visible or %bs_groupbox, %ws_ex_transparent
       control add label, hdlg,  %label1,  "file or dir to check:", 2, 7, 85, 12, %ws_child or %ws_visible or %ss_center
       control add textbox, hdlg,  %txtfile,  "d:\data\groups\", 85, 5, 83, 12, %ws_child or %ws_visible or %es_autohscroll or %ws_tabstop, %ws_ex_clientedge
       control add "button", hdlg,  %idok,  "&lookup", 179, 5, 53, 15, %ws_child or %ws_visible or %bs_pushbutton or %ws_tabstop
       control add "button", hdlg,  %butclose,  "&close", 179, 25, 53, 15, %ws_child or %ws_visible or %bs_pushbutton or %ws_tabstop
       control add label, hdlg,  %label2,  "   on what server:", 3, 27, 83, 12, %ws_child or %ws_visible or %ss_center
       control add textbox, hdlg,  %txtsrv,  ", 85, 25, 83, 12, %ws_child or %ws_visible or %es_autohscroll or %ws_tabstop, %ws_ex_clientedge
       control add listbox, hdlg,  %listbox1, , 8, 52, 224, 106, %ws_child or %ws_visible or %lbs_notify or %lbs_nointegralheight or %ws_vscroll or %ws_tabstop or %lbs_usetabstops or %ws_hscroll or %lbs_sort, %ws_ex_clientedge
       dialog show modal hdlg , call dlgproc
    end function
    "i haven't lost my mind... its backed up on tape... i think??"

    [this message has been edited by william burns (edited may 08, 2003).]
    "I haven't lost my mind... its backed up on tape... I think??" :D