Open file as shared (Update)

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Open file as shared (Update)

Post by mk-soft »

How to find a code to open file as shared. PB ReadFile open files as explicit!

Thanks
Last edited by mk-soft on Sun Feb 28, 2010 5:23 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Open file as shared

Post by blueznl »

Yeah, I'd love an open parameter for opening shared as well, but it isn't there... You have to go the way of the (win)api...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Open file as shared

Post by mk-soft »

I know,

Now write a code for reading files... :(
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Open file as shared

Post by RASHAD »

Hi mk-soft
I do not know what do you mean by opening a file as shared
If you mean that a special user or group can only open the file or a directory, so go to DOS box help and read every thing about
1- Takeown
2- icacls
and of course if you find that is your request so you can run it from windows enviroment easily

Good luck
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Open file as shared (UpDate)

Post by mk-soft »

When PB a file have open than can´t another application saving the same file.

Here now my first code for open file as shared and read strings

Update v1.02
- Checked CR, LF, LFCR, CRLF for NewLine

Code: Select all

;-TOP
; Kommentar     :
; Author        : mk-soft
; Second Author :
; Datei         : files.pb
; Version       : 1.02
; Erstellt      : 24.02.2010
; Geändert      : 28.02.2010
;
; Compilermode  :
;
; ***************************************************************************************

EnableExplicit

Structure udtMyFile
  hFile.i
  lpReOpenBuff.OFSTRUCT
  Eof.i
EndStructure

Global NewList MyFile.udtMyFile()
Global FileLastError

; ***************************************************************************************

Procedure MyIsFile(File)

  ForEach MyFile()
    If MyFile() = File
      ProcedureReturn #True
    EndIf
  Next
  ProcedureReturn #False
EndProcedure

; ***************************************************************************************

Procedure MyOpenFile(filename.s, uStyle = #OF_READ)

  Protected hFile
  Protected lpReOpenBuff.OFSTRUCT
  Protected result
 
  hFile = OpenFile_(@filename, lpReOpenBuff, uStyle)
  If hFile = 0
    FileLastError = GetLastError_()
    ProcedureReturn 0
  Else
    result = AddElement(MyFile())
    With MyFile()
      \hFile = hFile
      \lpReOpenBuff = lpReOpenBuff
      \Eof = #False
    EndWith
    ProcedureReturn result
  EndIf
 
EndProcedure

; ***************************************************************************************

Procedure MyCloseFile(File)
 
  Protected r1
 
  If File
    ChangeCurrentElement(MyFile(), File)
    r1 = CloseHandle_(MyFile()\hFile)
    If r1 = 0
      FileLastError = GetLastError_()
    EndIf
    DeleteElement(MyFile())
  EndIf
  ProcedureReturn r1
 
EndProcedure

; ***************************************************************************************

Procedure.q MyLof(File)

  Protected result.q
  Protected dwError.i
  Protected dwSizeLow.l
  Protected dwSizeHigh.l
 
  If File = 0
    ProcedureReturn -1
  EndIf
 
  ChangeCurrentElement(MyFile(), File)
  dwSizeLow = GetFileSize_(MyFile()\hFile, @dwSizeHigh)
  If dwSizeLow = $FFFFFFFF
    dwError = GetLastError_()
    If dwError <> #NO_ERROR
      FileLastError = dwError
      ProcedureReturn -1
    EndIf 
  Else
    result = (dwSizeHigh << 32) | dwSizeLow
    ProcedureReturn result
  EndIf
 
EndProcedure

; ***************************************************************************************

Procedure MyEof(*File.udtMyFile)
  ProcedureReturn *File\Eof
EndProcedure

; ***************************************************************************************

Procedure.q MySeekFile(File, position.q)
 
  Protected result.q
  Protected dwError.i
  Protected dwPosLow.l
  Protected dwPosHigh.l
 
  If File = 0
    ProcedureReturn 0
  EndIf
 
  dwPosLow = position & $FFFFFFFF
  dwPosHigh = position >> 32
 
  ChangeCurrentElement(MyFile(), File)
  dwPosLow = SetFilePointer_(MyFile()\hFile, dwPosLow, @dwPosHigh, #FILE_BEGIN)
  If dwPosLow = $FFFFFFFF
    dwError = GetLastError_()
    If dwError <> #NO_ERROR
      FileLastError = dwError
      ProcedureReturn -1
    EndIf 
  Else
    result = (dwPosHigh << 32) | dwPosLow
    ProcedureReturn result
  EndIf
EndProcedure

; ***************************************************************************************

Procedure.s MyReadString(File)

  Protected lpBuffer.Character, nNumberOfBytesToRead, lpNumberOfByteRead
  Protected r1
  Protected result.s
  Protected LastChar.c = 0 
  
  If File = 0
    ProcedureReturn ""
  EndIf
 
  ChangeCurrentElement(MyFile(), File)
  nNumberOfBytesToRead = 1
  result = ""
  Repeat
    r1 = ReadFile_(MyFile()\hFile, @lpBuffer, nNumberOfBytesToRead, @lpNumberOfByteRead, #Null)
    If r1 = 0
      Break
    Else
      If lpNumberOfByteRead = 0
        MyFile()\Eof = #True
        Break
      EndIf
      If LastChar = #LF And lpBuffer\c = #CR
        Break
      ElseIf LastChar = #CR And lpBuffer\c = #LF 
        Break
      ElseIf LastChar = #CR Or LastChar = #LF
        SetFilePointer_(MyFile()\hFile, -1, 0, #FILE_CURRENT)
        Break
      Else
        If lpBuffer\c <> #CR And lpBuffer\c <> #LF
          result + Chr(lpBuffer\c)
        EndIf
      EndIf
      LastChar = lpBuffer\c
    EndIf
  ForEver
 
  ProcedureReturn result
 
EndProcedure

; ***************************************************************************************

GT 8)
Last edited by mk-soft on Sun Feb 28, 2010 5:23 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Open file as shared

Post by blueznl »

I've gone the same route, but I would prefer it if it would be part of PureBasic.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Open file as shared (Update)

Post by mk-soft »

I hope that it soon in purebasic the option shared will give too

P.S. Update v1.02
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

Do you have an example how to use the functions for 'using files shared'?

Thanks
Richard
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

After a lot of searching following code. It works with a small testfile test.txt containing 3 lines

Code: Select all

test1
test2
test3
2 questions:
1. I cannot figure out the parameter of the MyEof() function.
2. I need a MyWriteString(File) function to be able to write.

Thanks
Richard

Code: Select all

    ;-TOP
    ; Kommentar     :
    ; Author        : mk-soft
    ; Second Author :
    ; Datei         : files.pb
    ; Version       : 1.02
    ; Erstellt      : 24.02.2010
    ; Geändert      : 28.02.2010
    ;
    ; Compilermode  :
    ;
    ; ***************************************************************************************

      Structure udtMyFile
      hFile.i
      lpReOpenBuff.OFSTRUCT
      Eof.i
    EndStructure

    Global NewList MyFile.udtMyFile()
    Global FileLastError

    ; ***************************************************************************************

    Procedure MyIsFile(File)

      ForEach MyFile()
        If MyFile() = File
          ProcedureReturn #True
        EndIf
      Next
      ProcedureReturn #False
    EndProcedure

    ; ***************************************************************************************

    Procedure MyOpenFile(filename.s, uStyle =  #OF_READ)

      Protected hFile
      Protected lpReOpenBuff.OFSTRUCT
      Protected result

      hFile = OpenFile_(@filename, lpReOpenBuff, uStyle)
      If hFile = 0
        FileLastError = GetLastError_()
        ProcedureReturn 0
      Else
        result = AddElement(MyFile())
        With MyFile()
          \hFile = hFile
          \lpReOpenBuff = lpReOpenBuff
          \Eof = #False
        EndWith
        ProcedureReturn result
      EndIf

    EndProcedure

    ; ***************************************************************************************

    Procedure MyCloseFile(File)

      Protected r1

      If File
        ChangeCurrentElement(MyFile(), File)
        r1 = CloseHandle_(MyFile()\hFile)
        If r1 = 0
          FileLastError = GetLastError_()
        EndIf
        DeleteElement(MyFile())
      EndIf
      ProcedureReturn r1

    EndProcedure

    ; ***************************************************************************************

    Procedure.q MyLof(File)

      Protected result.q
      Protected dwError.i
      Protected dwSizeLow.l
      Protected dwSizeHigh.l

      If File = 0
        ProcedureReturn -1
      EndIf

      ChangeCurrentElement(MyFile(), File)
      dwSizeLow = GetFileSize_(MyFile()\hFile, @dwSizeHigh)
      If dwSizeLow = $FFFFFFFF
        dwError = GetLastError_()
        If dwError <> #NO_ERROR
          FileLastError = dwError
          ProcedureReturn -1
        EndIf
      Else
        result = (dwSizeHigh << 32) | dwSizeLow
        ProcedureReturn result
      EndIf

    EndProcedure

    ; ***************************************************************************************

    Procedure MyEof(*File.udtMyFile)
      ProcedureReturn *File\Eof
    EndProcedure

    ; ***************************************************************************************

    Procedure.q MySeekFile(File, position.q)

      Protected result.q
      Protected dwError.i
      Protected dwPosLow.l
      Protected dwPosHigh.l

      If File = 0
        ProcedureReturn 0
      EndIf

      dwPosLow = position & $FFFFFFFF
      dwPosHigh = position >> 32

      ChangeCurrentElement(MyFile(), File)
      dwPosLow = SetFilePointer_(MyFile()\hFile, dwPosLow, @dwPosHigh, #FILE_BEGIN)
      If dwPosLow = $FFFFFFFF
        dwError = GetLastError_()
        If dwError <> #NO_ERROR
          FileLastError = dwError
          ProcedureReturn -1
        EndIf
      Else
        result = (dwPosHigh << 32) | dwPosLow
        ProcedureReturn result
      EndIf
    EndProcedure

    ; ***************************************************************************************

    Procedure.s MyReadString(File)

      Protected lpBuffer.Character, nNumberOfBytesToRead, lpNumberOfByteRead
      Protected r1
      Protected result.s
      Protected LastChar.c = 0
     
      If File = 0
        ProcedureReturn ""
      EndIf

      ChangeCurrentElement(MyFile(), File)
      nNumberOfBytesToRead = 1
      result = ""
      Repeat
        r1 = ReadFile_(MyFile()\hFile, @lpBuffer, nNumberOfBytesToRead, @lpNumberOfByteRead, #Null)
        If r1 = 0
          Break
        Else
          If lpNumberOfByteRead = 0
            MyFile()\Eof = #True
            Break
          EndIf
          If LastChar = #LF And lpBuffer\c = #CR
            Break
          ElseIf LastChar = #CR And lpBuffer\c = #LF
            Break
          ElseIf LastChar = #CR Or LastChar = #LF
            SetFilePointer_(MyFile()\hFile, -1, 0, #FILE_CURRENT)
            Break
          Else
            If lpBuffer\c <> #CR And lpBuffer\c <> #LF
              result + Chr(lpBuffer\c)
            EndIf
          EndIf
          LastChar = lpBuffer\c
        EndIf
      ForEver

      ProcedureReturn result

    EndProcedure

    ; ***************************************************************************************
    ; main
    a=MyOpenFile("test.txt", uStyle = #OF_SHARE_COMPAT)
    s$=MyReadString(a)
    Debug Str(a) + "  " + s$
    Debug MyLof(a)
    MySeekFile(a, 7)
    s$=MyReadString(a)
    Debug "second:  " +  s$    
    MyCloseFile(a)
    End 
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

With my (very) limited knowledge of API I tried to write a MyWriteString myself.
It is supposed to need 3 parameters: the filehandle, the string and the length of the string to write.
As expected it does not work.
What is wrong with it?

This is the API function (from ALLAPI)

Code: Select all

Declare Function WriteFile Lib "kernel32" Alias "WriteFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As OVERLAPPED) As Long

Code: Select all

    ;-TOP
    ; Kommentar     :
    ; Author        : mk-soft
    ; Second Author :
    ; Datei         : files.pb
    ; Version       : 1.02
    ; Erstellt      : 24.02.2010
    ; Geändert      : 28.02.2010
    ;
    ; Compilermode  :
    ;
    ; ***************************************************************************************

    ;EnableExplicit

    Structure udtMyFile
      hFile.i
      lpReOpenBuff.OFSTRUCT
      Eof.i
    EndStructure

    Global NewList MyFile.udtMyFile()
    Global FileLastError

    ; ***************************************************************************************

    Procedure MyIsFile(File)

      ForEach MyFile()
        If MyFile() = File
          ProcedureReturn #True
        EndIf
      Next
      ProcedureReturn #False
    EndProcedure

    ; ***************************************************************************************

    Procedure MyOpenFile(filename.s, uStyle =  #OF_READ)

      Protected hFile
      Protected lpReOpenBuff.OFSTRUCT
      Protected result

      hFile = OpenFile_(@filename, lpReOpenBuff, uStyle)
      If hFile = 0
        FileLastError = GetLastError_()
        ProcedureReturn 0
      Else
        Global h = hFile
        result =  AddElement(MyFile())
        
        With MyFile()
          \hFile = hFile
          \lpReOpenBuff = lpReOpenBuff
          \Eof = #False
        EndWith
        ProcedureReturn result
        
      EndIf

    EndProcedure

    ; ***************************************************************************************

    Procedure MyCloseFile(File)

      Protected r1

      If File
        ChangeCurrentElement(MyFile(), File)
        r1 = CloseHandle_(MyFile()\hFile)
        If r1 = 0
          FileLastError = GetLastError_()
        EndIf
        DeleteElement(MyFile())
      EndIf
      ProcedureReturn r1

    EndProcedure

    ; ***************************************************************************************

    Procedure.q MyLof(File)

      Protected result.q
      Protected dwError.i
      Protected dwSizeLow.l
      Protected dwSizeHigh.l

      If File = 0
        ProcedureReturn -1
      EndIf

      ChangeCurrentElement(MyFile(), File)
      dwSizeLow = GetFileSize_(MyFile()\hFile, @dwSizeHigh)
      If dwSizeLow = $FFFFFFFF
        dwError = GetLastError_()
        If dwError <> #NO_ERROR
          FileLastError = dwError
          ProcedureReturn -1
        EndIf
      Else
        result = (dwSizeHigh << 32) | dwSizeLow
        ProcedureReturn result
      EndIf

    EndProcedure

    ; ***************************************************************************************

    Procedure MyEof(*File.udtMyFile)
      ProcedureReturn *File\Eof
    EndProcedure

    ; ***************************************************************************************

    Procedure.q MySeekFile(File, position.q)

      Protected result.q
      Protected dwError.i
      Protected dwPosLow.l
      Protected dwPosHigh.l

      If File = 0
        ProcedureReturn 0
      EndIf

      dwPosLow = position & $FFFFFFFF
      dwPosHigh = position >> 32

      ChangeCurrentElement(MyFile(), File)
      dwPosLow = SetFilePointer_(MyFile()\hFile, dwPosLow, @dwPosHigh, #FILE_BEGIN)
      If dwPosLow = $FFFFFFFF
        dwError = GetLastError_()
        If dwError <> #NO_ERROR
          FileLastError = dwError
          ProcedureReturn -1
        EndIf
      Else
        result = (dwPosHigh << 32) | dwPosLow
        ProcedureReturn result
      EndIf
    EndProcedure

    ; ***************************************************************************************

    Procedure.s MyReadString(File)

      Protected lpBuffer.Character, nNumberOfBytesToRead, lpNumberOfByteRead
      Protected r1
      Protected result.s
      Protected LastChar.c = 0
     
      If File = 0
        ProcedureReturn ""
      EndIf

      ChangeCurrentElement(MyFile(), File)
      nNumberOfBytesToRead = 1
      result = ""
      Repeat
        r1 = ReadFile_(MyFile()\hFile, @lpBuffer, nNumberOfBytesToRead, @lpNumberOfByteRead, #Null)
        If r1 = 0
          Break
        Else
          If lpNumberOfByteRead = 0
            MyFile()\Eof = #True
            Break
          EndIf
          If LastChar = #LF And lpBuffer\c = #CR
            Break
          ElseIf LastChar = #CR And lpBuffer\c = #LF
            Break
          ElseIf LastChar = #CR Or LastChar = #LF
            SetFilePointer_(MyFile()\hFile, -1, 0, #FILE_CURRENT)
            Break
          Else
            If lpBuffer\c <> #CR And lpBuffer\c <> #LF
              result + Chr(lpBuffer\c)
            EndIf
          EndIf
          LastChar = lpBuffer\c
        EndIf
      ForEver

      ProcedureReturn result

    EndProcedure

    ; ***************************************************************************************
     Procedure MyWriteString(File,text$, length)

      Protected lpBuffer.s, nNumberOfBytesToWrite
            
      lpBuffer=text$ 
      nNumberOfBytesToWrite=length
      
      ChangeCurrentElement(MyFile(), File)
      result = WriteFile_(MyFile()\hFile, @lpBuffer, nNumberOfBytesToWrite,  #Null, #Null)
      
    EndProcedure

    ; ***************************************************************************************
    
    ; main
    
      a=MyOpenFile("test.txt", uStyle = #OF_SHARE_COMPAT)
    For i= 0 To 2
      MySeekFile(a, i*7)
      s$=MyReadString(a)
      Debug s$  
    Next
    MyCloseFile(a)
    
    
   a=MyOpenFile("test.txt", uStyle = #OF_SHARE_COMPAT) 
   t$="XXXXX"
   q= MyWriteString(a,t$,3)
   MyCloseFile(a)
   
   End 
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Open file as shared (Update)

Post by mk-soft »

hi,
at time i can't testet. I show when i'm coming home. but i think is not linefeed and missing truncate file.

gt
Michael
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

Hallo Michael,
I think I found (a) solution:
This is probably not complete but it works.

1. last 2 arguments of the WriteFile function cannot be NULL both.
2. the file has to be opened with #OF_READWRITE parameter or #OF_WRITE parameter

Richard

Code: Select all

; ***************************************************************************************
     Procedure MyWriteString(File,text$, length)

      Protected lpBuffer.s, nNumberOfBytesToWrite,nNumberOfBytesWritten
            
      lpBuffer=text$ 
      nNumberOfBytesToWrite=length
      
      ChangeCurrentElement(MyFile(), File)
      result = WriteFile_(MyFile()\hFile, @lpBuffer, nNumberOfBytesToWrite,@nNumberOfBytesWritten, #Null)
     ; Debug result
    EndProcedure

    ; ***************************************************************************************
     ; main
        
    a=MyOpenFile("test.txt", uStyle = #OF_SHARE_COMPAT |#OF_READWRITE) 
    MySeekFile(a,0)
   t$="12345"
   q= MyWriteString(a,t$,5)
   MyCloseFile(a)
   End
   
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

I found already one big flaw: the function is about 30 times SLOWER then the native PB writestring function.
I presume this has something to do with buffering??

Richard
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Open file as shared (Update)

Post by mk-soft »

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: Open file as shared (Update)

Post by t57042 »

Do you have an example how to use this function?
Richard
Post Reply