Results 1 to 5 of 5

Thread: CreateFile ... no luck

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14

    Angry

    In a VB program, for a "new file" command, I need to create a file, but CreateFile is acting up. It doesn't stop the program, but doesn't create the file either. Here's the entire sub (in case I'm going about this the wrong way...)

    Dim mfile$, blah As SECURITY_ATTRIBUTES
    dialog.Action = 1
    mfile$ = dialog.FileName
    X = CreateFile(mfile$, &HC0000000, 0, blah, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0)
    Open mfile$ For Input As #1
    For i = 0 To 99
    Write #1, vbNullString
    Next
    Call LoadMacros
    Call LoadLBox

    The sub hangs @ Open mfile$ where it returns that the file doesn't exist. Any suggestions?

    Thanks,
    [email protected]

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Here is an example from API-Guide:
    Code:
    'This project needs a Common Dialog box, named CDBox.
    '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    '   and select Microsoft Common Dialog control)
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
        Private Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
    End Type
    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
    Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        '[email protected]
        Dim m_Date As Date, lngHandle As Long
        Dim udtFileTime As FILETIME
        Dim udtLocalTime As FILETIME
        Dim udtSystemTime As SYSTEMTIME
        m_Date = Format(Now, "DD-MM-YY")
    
        'Set the dialog's title
        CDBox.DialogTitle = "Choose a file ..."
        'Set the dialog's filter
        CDBox.Filter = "All Files (*.*)|*.*"
        'Show the 'Open File'-dialog
        CDBox.ShowOpen
    
        udtSystemTime.wYear = Year(m_Date)
        udtSystemTime.wMonth = Month(m_Date)
        udtSystemTime.wDay = Day(m_Date)
        udtSystemTime.wDayOfWeek = WeekDay(m_Date) - 1
        udtSystemTime.wHour = Hour(m_Date)
        udtSystemTime.wSecond = Second(m_Date)
        udtSystemTime.wMilliseconds = 0
    
        ' convert system time to local time
        SystemTimeToFileTime udtSystemTime, udtLocalTime
        ' convert local time to GMT
        LocalFileTimeToFileTime udtLocalTime, udtFileTime
        ' open the file to get the filehandle
        lngHandle = CreateFile(CDBox.Filename, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
        ' change date/time property of the file
        SetFileTime lngHandle, udtFileTime, udtFileTime, udtFileTime
        ' close the handle
        CloseHandle lngHandle
        MsgBox "The date of the file '" + CDBox.Filename + "' has been changed to" + Str$(m_Date), vbInformation + vbOKOnly, App.Title
    End Sub
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14

    Thumbs up

    This seems to work well.

    However, for others needing a new command, I found another system which seems to work well. A template file "ala Word' s normal.dot" seems to not only provide customization options for the end-user, but the simplicity of creating a new file with "FileCopy" instead of rolling the dice with API. Just wanted to explore other options.

    Thanks,
    [email protected]

  4. #4
    Addicted Member Babbalouie's Avatar
    Join Date
    Jan 2001
    Location
    On the bright, blue sea...
    Posts
    197
    Of course, there is a non-API answer to your question as well. If the file is opened in the Append, Binary, Output, or Random modes (instead of the Input mode you used), then it is automatically created if it does not already exist...

    Building A Better Body Albeit Left Out Under Intense Extrapolation

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    14

    Exclamation Oops...

    "Of course, there is a non-API answer to your question as well. If the file is opened in the Append, Binary, Output, or Random modes (instead of the Input mode you used), then it is automatically created if it does not already exist... "

    Why was I using Write when I had the file open as Input ... *smack*. Note to self: No VB after 3 AM...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width