
CreateFile API call with VB4(32) on Win95 OS
Hi all,
Anybody would know why the folowing code fails to either create a new
file or reopen an existing one. The fHandl value is always = -1 and the
GetLastError always return 0. I have: consulted the SDK, MSKB, DejaNews for
old articles, VB4 documentation and also tempered with all the constants and
settings for three days non-stop now. I am in the process of loosing: my hair,
some weight and my wife. If you've got any hint...
___________________________________________________________________
'* Means on one line *'
Option Explicit 'In Form's declaration
'For opening the file
Const FILE_SHARE_READ = &H1
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_FLAG_RANDOM_ACCESS = &H10000000
Const GENERIC_READ = &H80000000
Const OPEN_ALWAYS = 4
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
'*
Private 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
*'
'For reading the file
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
'*
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long,
lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As
Long, lpOverlapped As OVERLAPPED) As Long
*'
'For closing the file
'*
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As
Long
*'
'for getting the error code
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim MYSECURITY_ATTRIBUTES As SECURITY_ATTRIBUTES
Dim MYOVERLAPPED As OVERLAPPED
Dim fHandl As Long
Dim r As Long
Dim ReadBytes As String * 200
Dim ReturnedNumberBytes As Long
Dim c As Long
Dim LastError As Long
MYSECURITY_ATTRIBUTES.nLength = 10
MYSECURITY_ATTRIBUTES.lpSecurityDescriptor = 0
MYSECURITY_ATTRIBUTES.bInheritHandle = True
MYOVERLAPPED.Internal = 0
MYOVERLAPPED.InternalHigh = 0
MYOVERLAPPED.offset = 0
MYOVERLAPPED.OffsetHigh = 0
MYOVERLAPPED.hEvent = 0
'Next statement never seems to return another value then -1
'*
fHandl = CreateFile("c:\test2.blr", GENERIC_READ, FILE_SHARE_READ,
MYSECURITY_ATTRIBUTES, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL +
FILE_FLAG_RANDOM_ACCESS, 0)
*'
'LastError always = 0
LastError = GetLastError
'*
r = ReadFile(fHandl, ReadBytes, 200, ReturnedNumberBytes,
MYOVERLAPPED)
*'
c = CloseHandle(fHandl)
End Sub
___________________________________________________________________
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"You might know a Sylvain C?t, but it is not me"
- Sylvain C?t
Sylvain C?t
McGill University
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~