Open existing file with API CreateFile() 
Author Message
 Open existing file with API CreateFile()

Hi!

Im trying to open an existing file and read it through API-Calls.
But CreateFile() refuses to open the file.
What am I doing wrong?

Dim lpSEC As SECURITY_ATTRIBUTES
hFile = CreateFile("C:\SCANDISK.LOG", GENERIC_READ, 0&, lpSEC,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)

Help is greatly appreciated!
-Harald



Tue, 29 Aug 2000 03:00:00 GMT  
 Open existing file with API CreateFile()

Hi,

I just tried this  and at least for me, it seems to be working.  Hopefully
someone can shed more light on this.  Also, what exactly happens when you
run your code?

Private Declare Function apiCreateFile Lib "kernel32" _
    Alias "CreateFileA" _
    (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) _
    As Long

Private Declare Function apiCloseHandle Lib "kernel32" _
    Alias "CloseHandle" _
    (ByVal hObject As Long) _
    As Long

Declare Function GetLastError& Lib "kernel32" ()

Type SECURITY_ATTRIBUTES
     nLength As Long
     lpSecurityDescriptor As Long
     bInheritHandle As Long
End Type

Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_COMPRESSED = &H800
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Const FILE_FLAG_WRITE_THROUGH = &H80000000
Const FILE_FLAG_OVERLAPPED = &H40000000
Const FILE_FLAG_NO_BUFFERING = &H20000000
Const FILE_FLAG_RANDOM_ACCESS = &H10000000
Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000

Const GENERIC_ALL = &H10000000
Const GENERIC_EXECUTE = &H20000000
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000

Const CREATE_ALWAYS = 2
Const CREATE_NEW = 1
Const OPEN_ALWAYS = 4
Const OPEN_EXISTING = 3
Const TRUNCATE_EXISTING = 5

Const INVALID_HANDLE_VALUE = -1

Function fOpenFile()
Dim lpFileName As String
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
Dim lngRet As Long

    lpFileName = "D:\test.txt"  'exists
    lngRet = apiCreateFile(lpFileName, GENERIC_READ Or GENERIC_WRITE, 0, _
            lpSecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
    If lngRet <> INVALID_HANDLE_VALUE Then Debug.Print lngRet

    lngRet = apiCloseHandle(lngRet)
    If lngRet <> 0 Then Debug.Print lngRet
End Function

Dev
--
Just my $.001
Please limit questions to newsgroups only!
Dev Ashish
---------------
The Access Web ( http://home.att.net/~dashish )
---------------

:Hi!
:
:Im trying to open an existing file and read it through API-Calls.
:But CreateFile() refuses to open the file.
:What am I doing wrong?
:
:Dim lpSEC As SECURITY_ATTRIBUTES
:hFile = CreateFile("C:\SCANDISK.LOG", GENERIC_READ, 0&, lpSEC,
:OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
:
:Help is greatly appreciated!
:-Harald
:
:
:
:
:



Tue, 29 Aug 2000 03:00:00 GMT  
 Open existing file with API CreateFile()

Hi Dev!

Thanks for the code, but the function still returns FALSE...
Are you using NT (I ran it on a Win95 machine)?

Yours
Harald

Quote:

> Hi,

> I just tried this  and at least for me, it seems to be working.  Hopefully
> someone can shed more light on this.  Also, what exactly happens when you
> run your code?

> Private Declare Function apiCreateFile Lib "kernel32" _
>     Alias "CreateFileA" _
>     (ByVal lpFileName As String, _
>     ByVal dwDesiredAccess As Long, _
>     ByVal dwShareMode As Long, _
>     lpSecurityAttributes As SECURITY_ATTRIBUTES, _
>     ByVal dwCreationDisposition As Long, _
>     ByVal dwFlagsAndAttributes As Long, _
>     ByVal hTemplateFile As Long) _
>     As Long

> Private Declare Function apiCloseHandle Lib "kernel32" _
>     Alias "CloseHandle" _
>     (ByVal hObject As Long) _
>     As Long

> Declare Function GetLastError& Lib "kernel32" ()

> Type SECURITY_ATTRIBUTES
>      nLength As Long
>      lpSecurityDescriptor As Long
>      bInheritHandle As Long
> End Type

> Const FILE_ATTRIBUTE_ARCHIVE = &H20
> Const FILE_ATTRIBUTE_COMPRESSED = &H800
> Const FILE_ATTRIBUTE_DIRECTORY = &H10
> Const FILE_ATTRIBUTE_HIDDEN = &H2
> Const FILE_ATTRIBUTE_NORMAL = &H80
> Const FILE_ATTRIBUTE_READONLY = &H1
> Const FILE_ATTRIBUTE_SYSTEM = &H4
> Const FILE_ATTRIBUTE_TEMPORARY = &H100

> Const FILE_FLAG_WRITE_THROUGH = &H80000000
> Const FILE_FLAG_OVERLAPPED = &H40000000
> Const FILE_FLAG_NO_BUFFERING = &H20000000
> Const FILE_FLAG_RANDOM_ACCESS = &H10000000
> Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
> Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000

> Const GENERIC_ALL = &H10000000
> Const GENERIC_EXECUTE = &H20000000
> Const GENERIC_READ = &H80000000
> Const GENERIC_WRITE = &H40000000

> Const CREATE_ALWAYS = 2
> Const CREATE_NEW = 1
> Const OPEN_ALWAYS = 4
> Const OPEN_EXISTING = 3
> Const TRUNCATE_EXISTING = 5

> Const INVALID_HANDLE_VALUE = -1

> Function fOpenFile()
> Dim lpFileName As String
> Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
> Dim lngRet As Long

>     lpFileName = "D:\test.txt"  'exists
>     lngRet = apiCreateFile(lpFileName, GENERIC_READ Or GENERIC_WRITE, 0, _
>             lpSecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
>     If lngRet <> INVALID_HANDLE_VALUE Then Debug.Print lngRet

>     lngRet = apiCloseHandle(lngRet)
>     If lngRet <> 0 Then Debug.Print lngRet
> End Function

> Dev
> --
> Just my $.001
> Please limit questions to newsgroups only!
> Dev Ashish
> ---------------
> The Access Web ( http://home.att.net/~dashish )
> ---------------


> :Hi!
> :
> :Im trying to open an existing file and read it through API-Calls.
> :But CreateFile() refuses to open the file.
> :What am I doing wrong?
> :
> :Dim lpSEC As SECURITY_ATTRIBUTES
> :hFile = CreateFile("C:\SCANDISK.LOG", GENERIC_READ, 0&, lpSEC,
> :OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
> :
> :Help is greatly appreciated!
> :-Harald
> :
> :
> :
> :
> :



Fri, 01 Sep 2000 03:00:00 GMT  
 Open existing file with API CreateFile()

Hi Harald,

yes I'm using NT.... Sorry didn't check that under 95.  Will do so later
today.  In the meantime,  Her'es something from the KB.. Check this out...
I'll post my findings later on.

Const GENERIC_WRITE = &H40000000
Const GENERIC_READ = &H80000000
Const FILE_ATTRIBUTE_NORMAL = &H80
Const CREATE_ALWAYS = 2
Const OPEN_ALWAYS = 4
Const INVALID_HANDLE_VALUE = -1

Const FILE_NAME = "c:\TEST.DAT"     'This can be any file that does not
                                       'currently exist.
Type MyType
    value As Integer
End Type

Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
         lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, _
         lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As
Long
Private Declare Function CloseHandle Lib "kernel32" ( _
        ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
        ByVal hFile As Long, lpBuffer As Any, _
        ByVal nNumberOfBytesToWrite As Long, _
        lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As
Long
Private Declare Function CreateFile Lib "kernel32" _
        Alias "CreateFileA" (ByVal lpFileName As String, _
        ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
        ByVal lpSecurityAttributes As Long, _
        ByVal dwCreationDisposition As Long, _
        ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long)
_
        As Long
Declare Function FlushFileBuffers Lib "kernel32" ( _
        ByVal hFile As Long) As Long

Sub fillArray(anArray() As MyType)
    Dim x As Integer
    For x = 0 To UBound(anArray)
        anArray(x).value = x
    Next x
End Sub
Sub Main()
    Dim fHandle As Integer
    Dim T(1000) As MyType            'Define a large array of data
    Dim S(1000) As MyType            'Define another large array
    fillArray T                      'Fill the array with some values
    writearray FILE_NAME, T          'Write the entire array to disk
    readArray FILE_NAME, S           'Read into a different array
End Sub

Sub readArray(Fname As String, anArray() As MyType)
Dim fHandle As Long
Dim fSuccess As Long
Dim sTest As String
Dim lBytesRead As Long
Dim BytesToRead As Long         'Get size of data to write
    BytesToRead = (UBound(anArray) + 1) * LenB(anArray(0))
    'Get a handle to a file Fname.
    fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
                    0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
    'Here you should test to see if you get a file handle or not.
    'CreateFile returns INVALID_HANDLE_VALUE if it fails.
    If fHandle <> INVALID_HANDLE_VALUE Then
        fSuccess = ReadFile(fHandle, anArray(LBound(anArray)), _
                    BytesToRead, lBytesRead, 0)
        'ReadFile returns a non-zero value if it is successful.
        'Now you just close the file.
        fSuccess = CloseHandle(fHandle)
    End If
End Sub
Sub writearray(Fname As String, anArray() As MyType)
Dim fHandle As Long
Dim fSuccess As Long
Dim sTest As String
Dim lBytesWritten As Long
Dim BytesToWrite As Long         'Get the length of data to write

    BytesToWrite = (UBound(anArray) + 1) * LenB(anArray(0))
    'Get a handle to a file Fname.
    fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
                    0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
    'Here you should test to see if you get a file handle or not.
    'CreateFile returns INVALID_HANDLE_VALUE if it fails.
    If fHandle <> INVALID_HANDLE_VALUE Then
        fSuccess = WriteFile(fHandle, anArray(LBound(anArray)), _
                    BytesToWrite, lBytesWritten, 0)
        'Check to see if you were successful writing the data
        If fSuccess <> 0 Then
            'Flush the file buffers to force writing of the data.
            fSuccess = FlushFileBuffers(fHandle)
            'Close the file.
            fSuccess = CloseHandle(fHandle)
        End If
    End If
End Sub

HTW
--
Please limit questions to newsgroups only...
Just my $.001
Dev Ashish
---------------
The Access Web ( http://home.att.net/~dashish )
---------------

Quote:

>Hi Dev!

>Thanks for the code, but the function still returns FALSE...
>Are you using NT (I ran it on a Win95 machine)?

<<snip>>


Fri, 01 Sep 2000 03:00:00 GMT  
 Open existing file with API CreateFile()

Ok I think I'm lost.  Why isn't

Private Declare Function apiCreateFile Lib "kernel32" _
    Alias "CreateFileA" _
    (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    '************See Above Line*********************
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) _
    As Long

working under 95??  Seems to be working fine with NT.  (I call it like

'Next line modified from above
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES

Dim lngRet As Long

    lpFileName = "D:\test.txt"  'exists
    lngRet = apiCreateFile(lpFileName, GENERIC_READ Or _
                    GENERIC_WRITE, 0, lpSecurityAttributes, _
                    OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

If I change the mentioned line at top to
ByVal lpSecurityAttributes As Long

and then call the function as
    lngRet = apiCreateFile(lpFileName, GENERIC_READ Or _
                    GENERIC_WRITE, 0, _
                    0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

only then it works under 95

Harald,  hope the mentioned changes work for you.

I'd appreciate others input as well.

Thanks
Dev
--
Please limit questions to newsgroups only...
Just my $.001
Dev Ashish
---------------
The Access Web ( http://home.att.net/~dashish )
---------------

Quote:

>Hi,

>I just tried this  and at least for me, it seems to be working.  Hopefully
>someone can shed more light on this.  Also, what exactly happens when you
>run your code?

<<snip>>


Fri, 01 Sep 2000 03:00:00 GMT  
 Open existing file with API CreateFile()

Yes, thats IT!
hmm, I knew that Win95 doesnt support Security attributes but didntt try to
change the
declaration....

Great job, Dev!
Thank you very much!!!!

Harald

Quote:

> Ok I think I'm lost.  Why isn't

> Private Declare Function apiCreateFile Lib "kernel32" _
>     Alias "CreateFileA" _
>     (ByVal lpFileName As String, _
>     ByVal dwDesiredAccess As Long, _
>     ByVal dwShareMode As Long, _
>     lpSecurityAttributes As SECURITY_ATTRIBUTES, _
>     '************See Above Line*********************
>     ByVal dwCreationDisposition As Long, _
>     ByVal dwFlagsAndAttributes As Long, _
>     ByVal hTemplateFile As Long) _
>     As Long

> working under 95??  Seems to be working fine with NT.  (I call it like

> 'Next line modified from above
> Dim lpSecurityAttributes As SECURITY_ATTRIBUTES

> Dim lngRet As Long

>     lpFileName = "D:\test.txt"  'exists
>     lngRet = apiCreateFile(lpFileName, GENERIC_READ Or _
>                     GENERIC_WRITE, 0, lpSecurityAttributes, _
>                     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

> If I change the mentioned line at top to
> ByVal lpSecurityAttributes As Long

> and then call the function as
>     lngRet = apiCreateFile(lpFileName, GENERIC_READ Or _
>                     GENERIC_WRITE, 0, _
>                     0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

> only then it works under 95

> Harald,  hope the mentioned changes work for you.

> I'd appreciate others input as well.

> Thanks
> Dev
> --
> Please limit questions to newsgroups only...
> Just my $.001
> Dev Ashish
> ---------------
> The Access Web ( http://home.att.net/~dashish )
> ---------------


> >Hi,

> >I just tried this  and at least for me, it seems to be working.  Hopefully
> >someone can shed more light on this.  Also, what exactly happens when you
> >run your code?
> <<snip>>



Sat, 02 Sep 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Open existing file with API CreateFile()

2. Open existing file with API CreateFile()

3. CreateFile/GetFileTime on open file : NT ok, W95 fails

4. Opening a dBase file : file not found but it exists really

5. Open file if it exists

6. Problem opening existing files

7. Open file in existing instance of app

8. Open File in Existing App Instanciation

9. How to edit the existing opening Excel file?

10. I search a open (select) file ocx with the functionalities (New - Existing - Recent)

11. Opening existing Excel file through VB 5.0

12. Getting API file handle to open file

 

 
Powered by phpBB® Forum Software