Getting a file handle; is there an alternative to CreateFile?

Dear Experts,

I am having problems using CreateFile to obtain the handle of a file. Although it usually works, it is sometimes returning -1. Is there an alternate way to obtain the handle of a file in Visual Basic 6?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
What will the handle be used for?
ttobin333Author Commented:
To set the file time.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
There certainly are other ways to set the file time, but they will not succeed either. CreateFile returns -1 if access is denied, the file does not exist, or any other error occurred. This might result from the share mode used, and the files being in use.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Martin LissOlder than dirtCommented:
This site shows how to do it with the following code:

The function SetFileDateTime is used to set the Create Date, Last Access Date, or Last Modified Date of a specified file.

Public Enum FileDateToProcess
    FileDateCreate = 1
    FileDateLastAccess = 2
    FileDateLastModified = 3
End Enum

Public Function SetFileDateTime(FileName As String, _
        FileDateTime As Double, WhichDateToChange As FileDateToProcess, _
        Optional NoGMTConvert As Boolean = False) As Boolean

Open in new window

where FileName is the name of the file to change, FileDateTime is the new time to assign to the file, in standard VB/VBA dddd.tttttt Double variable format, and WhichDateToChange indicates which date/time of the file to change. It is one of the value of the FileDateToProcess enum described above. NoGMTConvert indicates whether FileDateTime should be converted to GMT. Windows stores file times in GMT, and there in nearly all circumstances you should set NoGMTConvert to False. Since Windows displays file times in local time format, the displayed file time will be N hours later (or earlier if you are East of GMT)  than the time passed in as FileDateTime.

Dim FName As String
Dim Result As Boolean
Dim TheNewTime As Double
Dim WhatTime As FileDateToProcess
Dim TheNewDate As Double

' Craete the new date and time.
TheNewDate = DateSerial(2006, 7, 4)
TheNewDate  = TheNewDate + TimeSerial(12, 34, 56)

FName = "C:\Test.txt"

Result = SetFileDateTime(FileName:=FName, FileDateTime:=TheNewDate, _ 
                         WhichDateToChange:=WhatTime, NoGMTConvert:=False)
If Result = True Then
    Debug.Print "File date/time successfully modified."
    Debug.Print "An error occurred with SetFileDateTime."
End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ttobin333Author Commented:
Great, thanks!
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
ttobin333Author Commented:
Martin, can you help with my other open question?
Martin LissOlder than dirtCommented:
If you provide me with the URL I'll take a look.
ttobin333Author Commented:
Martin LissOlder than dirtCommented:
Sorry but that's out of my area of expertise.
ttobin333Author Commented:
OK, thanks for looking!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.