Page 1 of 1

OpenSharedFile(...)

Posted: Tue Nov 08, 2011 3:47 pm
by mk-soft
Property times again the case which I a file read in must where another program the file still to write opened.
There pure basic always the file for others the access closes has I now a (dirty) method found these to go around.

Hope the purebasic soon for files also Shared to open can.

Bugfix: #INVALID_HANDLE_VALUE

Code: Select all

#FILE_SHARE_DELETE = 4 ; NT4
#FILE_SHARE_READWRITE = #FILE_SHARE_READ | #FILE_SHARE_WRITE

Procedure OpenSharedFile(ID, FileName.s, DesiredAccess = #GENERIC_READ, ShareMode = #FILE_SHARE_READWRITE, TempFile.s = "temp.tmp")
 
  Protected hFile, addr, result
  
  hFile = CreateFile_(FileName, DesiredAccess, ShareMode, 0, #OPEN_ALWAYS, #FILE_ATTRIBUTE_NORMAL, 0)
  
  If hFile = #INVALID_HANDLE_VALUE
    result = 0
  Else
    result = OpenFile(ID, TempFile)
    If result
      If ID = #PB_Any
        addr = IsFile(result)
        CloseHandle_(FileID(result))
      Else
        addr = IsFile(ID)
        CloseHandle_(FileID(ID))
      EndIf
      PokeI(addr, hFile)
    Else
      CloseHandle_(hFile)
      ProcedureReturn 0
    EndIf
  EndIf
 
  ProcedureReturn result
  
EndProcedure
GT
Michael

Re: OpenSharedFile(...)

Posted: Wed Nov 09, 2011 9:15 am
by mk-soft
For desiredaccess and sharemode show msdn

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

GT :wink: