2

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.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
gberg927
  • 1,636
  • 9
  • 38
  • 51
  • 2
    What does `GetLastError()` return? – SLaks Nov 02 '11 at 14:18
  • 1
    In the words of the MSDN team: `If the function fails, the return value is INVALID_HANDLE_VALUE (-1). To get extended error information, call GetLastError.` – Deanna Nov 02 '11 at 15:18
  • 1
    Sorry, for `GetLastError` in VB6, you should use `Err.LastDLLError` as it can be overwritten by later calls. – Deanna Nov 02 '11 at 15:47

2 Answers2

4

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

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
  • 1
    +1. Possibly a virus scanner or backup program. Read more from Microsoft in [this KnowledgeBase article](http://support.microsoft.com/kb/316609) or in [other](http://stackoverflow.com/questions/4435268/createfile-fails-with-error-error-sharing-violation) [questions](http://stackoverflow.com/questions/1746781/waiting-until-a-file-is-available-for-reading-with-win32) on Stackoverflow – MarkJ Nov 02 '11 at 17:25
4

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.

jac
  • 9,666
  • 2
  • 34
  • 63
  • +1 A more sophisticated approach to cleaning up on errors might be: create a class to wrap the handle object. Have a method on the class that calls CreateFile. In the Class_Terminate check the handle and call CloseHandle. Use local variables to hold instances of this object. Whenever the local variable goes out of scope, the VB6 runtime will make sure the Class_terminate event runs. – MarkJ Nov 03 '11 at 09:24