469,353 Members | 2,116 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

WriteFile (or CreateFile) Why doesn't this work

dim
Copied directly from exaple book but not working....
All i get is an empty 0 byte file
Call to GetLastError directly after the call to WriteFile returns 0
(NO_ERROR) but no data sees to be written :-(
??????????
Thx


The code:


Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As
Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long,
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As
Long
Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long,
lpOverlapped As Any) As Long
Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long,
lpOverlapped As Any) As Long
Declare Function GetLastError Lib "kernel32.dll" () As Long


Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_ALWAYS = 2
Const CREATE_NEW = 1
Const OPEN_ALWAYS = 4 'Create if doesn't exist
Const OPEN_EXISTING = 3
Const TRUNCATE_EXISTING = 5
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Sub test()
Dim longbuffer As Long ' long to write to the file
Dim stringbuffer As String ' string to write to the file
Dim numwritten As Long ' receives number of bytes written to the file
Dim hFile As Long ' handle of the open file
Dim retval As Long ' return value

' Open or create the file being written to.
hFile = CreateFile(App.Path & "\test.txt", GENERIC_WRITE, FILE_SHARE_READ,
ByVal CLng(0), OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
If hFile = -1 Then ' the file could not be opened
Debug.Print "Unable to open the file -- it probably does not exist."
End ' abort the program
End If
stringbuffer = "Anonymous!"
retval = WriteFile(hFile, ByVal stringbuffer, 10, numwritten, CLng(0))
MsgBox GetLastError

retval = CloseHandle(hFile)
End Sub
Jul 17 '05 #1
4 8404
Isn't this ~below! easier?

open "c:\text.text" for output as #1
print #1, "Anonymous!"
close #1

You can do checks and things to see if the file exists and if it has data.
you can link it to the commondialog control as well.

--
Regards
Dillon Mantle
Jul 17 '05 #2
"dim" <di****@hotpop.com> wrote in message news:<dR********************@newsc.telia.net>...
<cut>
hFile = CreateFile(App.Path & "\test.txt", GENERIC_WRITE, FILE_SHARE_READ,
ByVal CLng(0), OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
Note that App.Path can sometimes end with a trailing \ and that will
cause the above line to fail. It's not causing your immediate problem
but is something you should be aware of. I usually do something like:
dim sPath as string
sPath=app.path
if right$(spath,1)<>"\" then spath=spath & "\"
hFile = CreateFile(sPath & "test.txt", ...

<cut> End ' abort the program
You should not use END since it does not allow normal cleanup
operations to complete. A VB app NEVER needs the END statement and
using it can cause data corruption and memory leaks. VB apps end when
no forms are loaded and no code is running.

<cut> retval = WriteFile(hFile, ByVal stringbuffer, 10, numwritten, CLng(0))
That line is your problem. Your code works if you change it to read:
retval = WriteFile(hFile, ByVal stringbuffer, 10, numwritten, ByVal
CLng(0))

you were passing a pointer to a zero which the API tried to interpret
as the overlapped operation parameter.
MsgBox GetLastError


The GetLastError API call is useless from VB because VB itself trashes
the value there. Fortunately it saves it for you before trashing it:

MsgBox Err.LastDLLError
Jul 17 '05 #3

"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...
"dim" <di****@hotpop.com> wrote in message news:<dR********************@newsc.telia.net>... <cut>
<cut>
retval = WriteFile(hFile, ByVal stringbuffer, 10, numwritten,
CLng(0))
That line is your problem. Your code works if you change it to read:
retval = WriteFile(hFile, ByVal stringbuffer, 10, numwritten, ByVal
CLng(0))
Is Bob a detective or what?
you were passing a pointer to a zero which the API tried to interpret
as the overlapped operation parameter.


I'm gonna save that line for the next tech support call I get...I can
pretend I know what it means as well as the next guy...<g>
Jul 17 '05 #4
"Steve Gerrard" <no*************@comcast.net> wrote in message news:<9P********************@comcast.com>...
"Bob Butler" <bu*******@earthlink.net> wrote in message
news:fa*************************@posting.google.co m...

<cut>
you were passing a pointer to a zero which the API tried to interpret
as the overlapped operation parameter.


I'm gonna save that line for the next tech support call I get...I can
pretend I know what it means as well as the next guy...<g>


LOL; it does sound pretty cryptic at that, doesn't it? I should have
just given the standard response: User Error
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Juna | last post: by
3 posts views Thread by jx2 | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.