471,357 Members | 1,237 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to use API writefile in vb.net

db
I am trying to convert an API writefile instruction from a vb6 app to a
vb.net app. I cannot determine how to get the instruction to execute. I do
not get any runtime errors, however the data is not written to the file. I
want to use the API because I need to write the data quickly and synchrously
to a file. In the real app I will be writting ~ 10Mbytes every 5 seconds.

ANY help would be greatly appreciated.

Code follows:
-----

Public Class Form1
Inherits System.Windows.Forms.Form

Public Const GENERIC_READ As Long = &H80000000
Public Const GENERIC_WRITE As Long = &H40000000
Public Const FILE_FLAG_WRITE_THROUGH As Long = &H80000000
Public Const FILE_FLAG_NO_BUFFERING As Long = &H20000000
Public Const OPEN_EXISTING As Long = 3
Public Const CREATE_ALWAYS As Long = 2
Public Const CREATE_NEW As Long = 1
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const MB_OKCANCEL As Long = &H1
Public Const MB_ICONHAND As Long = &H10

Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As
Integer, ByVal lpBuffer As Integer, _
ByVal nNumberOfBytesToWrite As Integer, ByVal
lpNumberOfBytesWritten As Integer, _
ByVal lpOverlapped As Integer) As Integer
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA"
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, ByVal dwShareMode As
Integer, ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, ByVal
dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Short) As Short

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim lSize As Integer
Dim iDetailedTimingIndex As Short
Dim lApiTimingMissHandle As Integer
Dim lApiReturn As Integer
Dim bytArray2(1024) As Byte
Dim sApiTimingMissFileName As String = "e:\apibin.test"
Dim overlap As Integer = 0

lSize = 512

lApiTimingMissHandle = CreateFile(sApiTimingMissFileName,
GENERIC_WRITE, FILE_SHARE_READ _
Or FILE_SHARE_WRITE, 0&, CREATE_ALWAYS,
FILE_FLAG_NO_BUFFERING, 0)

WriteFile(lApiTimingMissHandle, bytArray2(0), lSize, lApiReturn,
overlap) ''CBCount& * 2, Ret, ByVal 0&
If lApiReturn <> lSize Then
MsgBox("Error writing error information to binary error file
...." & vbCrLf & _
"Requested = " & lSize & vbCrLf & _
"Written = " & lApiReturn)
End If

CloseHandle(lApiTimingMissHandle)

End Sub
End Class
Jul 21 '05 #1
0 2710

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jerry J | last post: by
2 posts views Thread by David Union | last post: by
5 posts views Thread by Tim Greenfield | last post: by
8 posts views Thread by Scott C. Reynolds | last post: by
3 posts views Thread by Buddy Ackerman | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.