CreateFile API Return INVALID_HANDLE_VALUE 
Author Message
 CreateFile API Return INVALID_HANDLE_VALUE

Dier,
I was having problems with CreateFile API in Windows 98. I found that in
order to make the API work under windows 98 I had to pass a zero instead
of a SECURITY_ATTRIBUTES structure, and therefore, declare the parameter
as ByVal and long.
On the other hand, under windows 2000, you can pass a
SECURITY_ATTRIBUTES structure, and it will work. In addition, you can
also declare the securityattributes parameters as ByVal and as long
(same as Win98,) and it also works.

Ernesto
PS: You can run the following code in the two different OS, and you will
see that it does not work under win98.

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

Public Declare Function CreateFile 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
Public Declare Function GetLastError Lib "kernel32" () As Long

Private Sub Command1_Click()
Dim sSecAtb As SECURITY_ATTRIBUTES
Dim lngFilHdl As Long
Dim lngErrNo As Long
Dim strFilPath As String
Dim lMaxPathLength As Integer

strFilPath = "c:\text.txt"
lMaxPathLength = 254

Do While Len(strFilPath) < lMaxPathLength - 1
    strFilPath = strFilPath & " "
Loop

strFilPath = Mid$(strFilPath, 1, lMaxPathLength - 1)
strFilPath = strFilPath & Chr$(0)

lngFilHdl = CreateFile(strFilPath, GENERIC_READ, 0, sSecAtb,
CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0)

lngErrNo = GetLastError()

Debug.Print
Debug.Print "------"; Now; "------"
Debug.Print "lngFilHdl = "; lngFilHdl
Debug.Print "lngErrNo = "; lngErrNo

End Sub



Sun, 26 Jan 2003 03:00:00 GMT  
 CreateFile API Return INVALID_HANDLE_VALUE
Hello Ernesto,

Quote:
> I was having problems with CreateFile API in Windows 98. I found
that in
> order to make the API work under windows 98 I had to pass a zero
instead
> of a SECURITY_ATTRIBUTES structure, and therefore, declare the
parameter
> as ByVal and long.
> On the other hand, under windows 2000, you can pass a
> SECURITY_ATTRIBUTES structure, and it will work. In addition, you
can
> also declare the securityattributes parameters as ByVal and as long
> (same as Win98,) and it also works.

> Ernesto
> PS: You can run the following code in the two different OS, and you
will
> see that it does not work under win98.

I don't remenber the original question but:

    - first, if you use SECURITY_ATTRIBUTES structure, you should fill
the size of this strucutre before use it

    - second, you must fill dwDesiredAccess with appropriate values.
On Win9x, you always must combine FILE_SHARE_READ Or FILE_SHARE_WRITE
flags(I don't know why exactly but it's the only way I found, and that
will work on NT too)

    - third, it's not necessary to fill the buffer (lpFileName) and Vb
strings are null terminated

    - fourth, you have to check the return value from CreateFile
function (you can't use GetLastError in VB) and don't forget, if the
handle is valid, to close it

So, do someting like that:

Dim sSecAtb As SECURITY_ATTRIBUTES
Dim strFilPath As String
Dim lngFilHdl As Long

sSecAtb.nLength = LenB(sSecAtb)
strFilPath = "c:\text.txt"

lngFilHdl = CreateFile(strFilPath, GENERIC_READ, FILE_SHARE_READ Or
FILE_SHARE_WRITE, _
                       sSecAtb, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS,
0)

' check the error
If lngFilHdl = INVALID_HANDLE_VALUE Then Exit Function

' do some stuff
.....

Call CloseHandle(lngFilHdl)

--
a+
---
Parci



Mon, 27 Jan 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CreateFile API Return INVALID_HANDLE_VALUE

2. CreateFile returns INVALID_HANDLE_VALUE

3. CreateFile always return -1

4. CreateFile won't return handle

5. The CreateFile() always return -1

6. CreateFile fuction return -1 value ?

7. Open existing file with API CreateFile()

8. CreateFile - API, Am I a Pudding?

9. CreateFile API

10. hFile = CreateFile(...) -- need HELP with API

11. Open existing file with API CreateFile()

12. CreateFile API call with VB4(32) on Win95 OS

 

 
Powered by phpBB® Forum Software