473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 8645
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: | last post by:
Hello, I hope someone will help me or I will have to dive from 11th floor. Why this --> test = @"Melanie "Jets" Riggs" doesnt work in asp? I need something like that and I dont know how to make it...
1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
3
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am...
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
1
Digital Don
by: Digital Don | last post by:
I am writing a program for Peg solitaire... To check for no repetition of previous states I use a Set for storage of Board states.. The pronblem is when I declare the set as type char i.e. set...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for...
20
by: Hush | last post by:
Hi, The following code works fine in IE7 but FF returns with an error: Access denied to achieve the property Element.firstChild. In this line: nodes = xmlDoc.documentElement.childNodes; My...
9
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.