473,554 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.d ll" Alias "CreateFile A" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As
Long, lpSecurityAttri butes As Any, ByVal dwCreationDispo sition As Long,
ByVal dwFlagsAndAttri butes As Long, ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32.d ll" (ByVal hObject As Long) As
Long
Declare Function WriteFile Lib "kernel32.d ll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesT oWrite As Long, lpNumberOfBytes Written As Long,
lpOverlapped As Any) As Long
Declare Function ReadFile Lib "kernel32.d ll" (ByVal hFile As Long, lpBuffer
As Any, ByVal nNumberOfBytesT oRead As Long, lpNumberOfBytes Read As Long,
lpOverlapped As Any) As Long
Declare Function GetLastError Lib "kernel32.d ll" () As Long


Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRIT E = &H2
Const CREATE_ALWAYS = 2
Const CREATE_NEW = 1
Const OPEN_ALWAYS = 4 'Create if doesn't exist
Const OPEN_EXISTING = 3
Const TRUNCATE_EXISTI NG = 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(hFi le)
End Sub
Jul 17 '05 #1
4 8672
Isn't this ~below! easier?

open "c:\text.te xt" 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******* *************@n ewsc.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(sPat h & "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.LastDLLErro r
Jul 17 '05 #3

"Bob Butler" <bu*******@eart hlink.net> wrote in message
news:fa******** *************** **@posting.goog le.com...
"dim" <di****@hotpop. com> wrote in message news:<dR******* *************@n ewsc.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******* *************@c omcast.com>...
"Bob Butler" <bu*******@eart hlink.net> wrote in message
news:fa******** *************** **@posting.goog le.com...

<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
6698
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 work. Please help me!! answer to dan@syscz.com many thanx
1
2517
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 ( 255 ); SELECT DISTINCT MyField AS Result FROM tb_Client;
3
7205
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 also setting the autopostback property to true for the dropdown list and it works but the handler doesnt get invoked at runtime... I have to do it in...
0
1628
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 application by pressing run button. but it doesnt work when we type the path directly in the browser's address bar eg....
1
2068
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 <char> open it doesnt take any char values into it and says cannot convert char to const char... I wonder why is it converting char to const char...
3
2956
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 imiedietely after i insert value into session1... is there any other way to do it? insert into 2 tables at the same time ...? if(!($sessid)){...
1
1587
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 showing curent data in dataset . in end i am calling update metod to insert data in sql database but this metod doesnt work correctly. at all doesnt work....
20
2830
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 code:
9
1692
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)
0
7605
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7893
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3556
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2022
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
845
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.