Valhalla Legends Forums Archive | Visual Basic Programming | Writefile API problem

AuthorMessageTime
Blade
The Problem is that it prints wierd characters to the file instead of the string I tell it to. Here is my code.


My Declairs, types and constants.
[code]
Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal _
nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
lpOverlapped As OVERLAPPED) As Long

Public Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long

Public Declare Function GetFileSize Lib "kernel32" _
(ByVal hFile As Long, lpFileSizeHigh As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Public Const OFS_MAXPATHNAME = 128
Public Const OF_WRITE = &H1
Public Const GENERIC_WRITE = &H40000000


Public Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type

Public Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
[/code]


The sub.
[code]
Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

hHandle = OpenFile(fileDB, ofData, OF_WRITE)
lngFilesize = GetFileSize(hHandle, 0)
WriteFile hHandle, strData, Len(DBdata), lngRead, overlap
CloseHandle hHandle

End Sub
[/code]



Usage.
[code]
dim arrData(2) as string
dim strData as string
dim strFile as string

arrData(0) = "blah"
arrData(1) = "ha"
arrData(2) = "something"
strData = join(arrData(), vbCrLf)
strFile = app.path & "\textfile.txt"
call savefile(strFile, strData)
[/code]

The file is already created and is empty, after I call the savefile sub in my program it works with no errors but it prints wierd characters to the file. I've used GENERIC_WRITE also but that doesn't write anything to the file. Whats wrong?
August 3, 2004, 4:27 AM
phvckmeh
[code]
Open (App.Path & "\log.txt") For Append As #1
Print #1, "My String!"
Close #1
[/code]

it will make log.txt if it does not exist
August 3, 2004, 6:50 AM
Blade
Yes I know that but I want to use the API because it's supposed to be faster. Any Idea why it doesn't work right?
August 3, 2004, 6:09 PM
St0rm.iD
It won't be noticeably faster.
August 4, 2004, 3:58 AM
Tuberload
[quote author=$t0rm link=board=31;threadid=8006;start=0#msg74010 date=1091591922]
It won't be noticeably faster.
[/quote]
Compared to using a binary stream in VB, yes it is noticeably faster.
August 4, 2004, 4:08 AM
Blade
So any ideas whats wrong here?
August 4, 2004, 9:19 PM
St0rm.iD
[quote author=Tuberload link=board=31;threadid=8006;start=0#msg74012 date=1091592480]
[quote author=$t0rm link=board=31;threadid=8006;start=0#msg74010 date=1091591922]
It won't be noticeably faster.
[/quote]
Compared to using a binary stream in VB, yes it is noticeably faster.
[/quote]

If < .25sec than I don't consider it noticeable.
August 4, 2004, 10:21 PM
Myndfyr
Chances are that it would be noticeably faster if you were using many accesses in a row, but just for one file? Pah.
August 5, 2004, 12:11 AM
St0rm.iD
A logfile, at that.
August 5, 2004, 4:42 AM
Tuberload
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?
August 5, 2004, 6:39 AM
St0rm.iD
[quote author=Tuberload link=board=31;threadid=8006;start=0#msg74159 date=1091687965]
Now my question: wtf is "pah"?
[/quote]

Yes, I was wondering that myself.
August 5, 2004, 3:08 PM
Flame
[code]
Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

hHandle = OpenFile(fileDB, ofData, OF_WRITE)
lngFilesize = GetFileSize(hHandle, 0)
WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
CloseHandle hHandle

End Sub
[/code]

Where does DBdata come from? It looks to me like it should be strData
August 5, 2004, 7:36 PM
Myndfyr
[quote author=Tuberload link=board=31;threadid=8006;start=0#msg74159 date=1091687965]
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?
[/quote]

Honestly, though, my statement was more fact than chance too. I was just trying to preface it with something that would be more disarming. Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.
August 5, 2004, 8:29 PM
Tuberload
[quote author=Myndfyre link=board=31;threadid=8006;start=0#msg74232 date=1091737767]
[quote author=Tuberload link=board=31;threadid=8006;start=0#msg74159 date=1091687965]
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?
[/quote]

Honestly, though, my statement was more fact than chance too. I was just trying to preface it with something that would be more disarming. Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.
[/quote]

You did not offend me, and that response was not directed solely towards you, that is why I did not quote you. Damned if I cannot find a way to make you believe that. I just stated a simple fact and everybody was arguing it. Thanks for the definition of pah btw.
August 5, 2004, 8:46 PM
Blade
[quote author=Flame link=board=31;threadid=8006;start=0#msg74222 date=1091734565]
[code]
Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

hHandle = OpenFile(fileDB, ofData, OF_WRITE)
lngFilesize = GetFileSize(hHandle, 0)
WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
CloseHandle hHandle

End Sub
[/code]

Where does DBdata come from? It looks to me like it should be strData
[/quote]

Yes that was an error in me copying it to this forum and I fixed it here, but there is no such error in my code so whats wrong!
August 5, 2004, 9:32 PM
St0rm.iD
The fact of the matter is, you would've saved more developing time from learning how to use WriteFile and actually typing it, and also saved more maintenence time, than its worth in the very slight speed increase.
August 6, 2004, 4:03 PM
o.OV
1. The variables fileDB, lngRead, and DBdata pointless in your sample code

2. You can't pass a string variable directly to an API. You are suppose to use ByVal.

3. In your sample code you aren't even using the OVERLAPPED structure. You should just pass a NULL. Use of OVERLAPPED structure for Writing to a file is like APPEND in VB. Use of a NULL is like OUTPUT.
August 12, 2004, 2:04 PM

Search