Announcement

Collapse
No announcement yet.

Using CreateFile and WriteFile api HELP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using CreateFile and WriteFile api HELP

    Can anyone offer a little assistance?
    PB/DLL 6
    WinNT 4.0
    Pentium II - 400 MHz

    I am creating a file and trying to write to it.
    I am using the following code.

    LOCAL FileNum AS LONG
    LOCAL Count AS LONG
    LOCAL SecAttrib AS SECURITY_ATTRIBUTES

    SecAttrib.nLength = SIZEOF(SecAttrib)
    SecAttrib.lpSecurityDescriptor = %NULL
    SecAttrib.bInheritHandle = %True

    FileNum = CreateFile(FileIO, _
    %GENERIC_READ OR %GENERIC_WRITE, _
    %FILE_SHARE_READ OR _
    %FILE_SHARE_WRITE, _
    SecAttrib, _
    %CREATE_ALWAYS, %FILE_ATTRIBUTE_NORMAL, %NULL)

    This works and return me a file handle.
    Next I do this:
    LOCAL Buffer AS ASCIIZ * 1024
    LOCAL Written AS LONG
    LOCAL Result AS LONG

    Buffer = "Anything at all" + $NUL

    Result = WriteFile(FileNum, Buffer, LEN(Buffer), Written, 0)

    This ALWAYS fails. Result = 0 every time.
    Am I doing something wrong?
    I know this can be done with PB/DLL functions, but I need
    to do it this way.
    Ben Clark
    [email protected]
    If at first you don't succeed, destroy all evidence that you tried.

  • #2
    What does GetLastError() return when the WriteFile() API fails? That is the clue you will need. If you still have problems, post a compilable example so we can duplicate your exact problem at our end.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      I use SecAttrib with ByVal 0&

      I runs ok on w95 to 2000

      ------------------

      Comment


      • #4
        Ben --
        Such fragment (de-facto 100% your) works without problems on my PC (Win2000).

        Code:
        #Compile Exe
        #Include "Win32Api.Inc"
        DefLng A-Z
        Function PbMain
           Local Buffer As Asciiz * 1024
           Local SecAttrib As SECURITY_ATTRIBUTES
           SecAttrib.nLength = SizeOf(SecAttrib)
           SecAttrib.lpSecurityDescriptor = %NULL
           SecAttrib.bInheritHandle = %True
           FileNum = CreateFile("C:\FileIO5", _
              %GENERIC_READ Or %GENERIC_WRITE, _
              %FILE_SHARE_READ Or %FILE_SHARE_WRITE, SecAttrib, _
              %CREATE_ALWAYS, %FILE_ATTRIBUTE_NORMAL, %NULL)
           For i = 1 To 1000
              Buffer = "Line" + Str$(i) + " AT " + Time$ + $CRLF
              Result = WriteFile(FileNum, Buffer, Len(Buffer), Written, ByVal 0)
           Next
           CloseHandle FileNum
        End Function
        ------------------

        Comment


        • #5
          Thanks for all your help.
          I found the answer in a archived post.
          I changed the line from This:
          Result = WriteFile(hStdIO, Buffer, LEN(x), Written, 0)
          TO:
          Result = WriteFile(hStdIO, Buf, LEN(Buf), BYCOPY 0, BYVAL 0)

          Thanks again

          ------------------
          Ben Clark
          [email protected]
          If at first you don't succeed, destroy all evidence that you tried.

          Comment

          Working...
          X