CreateFile Returns negative handle

994 Views Asked by At

Any ideas why the createfile() function would be returning -1.

Handle = CreateFile(filename, &H80000000, 0, 0, 3, &H80, 0)

This is run a few times. I was thinking perhaps maybe the file is not being closed properly?

EDIT

Err.LastllError returns 32.

2

There are 2 best solutions below

1
On BEST ANSWER

You are probably right about not closing the file someplace. I would start by looking where I am opening the file and making sure I have an error handling routine in place. In the error handler I would check the value of "Handle" and if it's valid call CloseHandle(Handle). Also, since you are opening the file for read access, you could change you dwShareMode parameter to 1 to allow subsequent open for read operations on the same file.

1
On

Error 32 is ERROR_SHARING_VIOLATION (reference) which means some other process still has the file open.