How to use API writefile in vb.net

G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top