Const GENERIC_READ = &H80000000
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_FLAG_NO_BUFFERING = &H20000000
Declare Function CreateFile Lib "kernel32"
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDistribution As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As IntPtr
Dim fh As IntPtr = CreateFile("\\.\A:", _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
0, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, _
0)
I'm hoping someone can tell me what's wrong here and help me fix it so I
get a valid handle returned.
*** Sent via Developersdex http://www.developersdex.com ***
Declare all of the constants as 'Int32'.
> Declare Function CreateFile Lib "kernel32"
> Alias "CreateFileA" (ByVal lpFileName As String, _
> ByVal dwDesiredAccess As Long, _
> ByVal dwShareMode As Long, _
> ByVal lpSecurityAttributes As Long, _
> ByVal dwCreationDistribution As Long, _
> ByVal dwFlagsAndAttributes As Long, _
> ByVal hTemplateFile As Long) As IntPtr
Use this declaration instead (untested):
\\\
Imports System.Runtime.InteropServices
.
.
.
Private Declare Auto Function CreateFile Lub "kernel32.dll" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDistribution As Int32, _
ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr _
) As IntPtr
<StructLayout(LayoutKind.Sequential)> _
Private Structure SECURITY_ATTRIBUTES
Public nLength As Int32
Public lpSecurityDescriptor As IntPtr
Public bInheritHandle As Boolean
End Structure
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As
String, _
ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As
Integer, _
ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr)
As IntPtr
Dim handle As IntPtr
handle = CreateFile("\\.\\A:", GENERIC_READ, FILE_SHARE_READ Or
FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero)
"Terry Olsen" <tols...@hotmail.com> wrote in message
news:e9DU29W4...@TK2MSFTNGP15.phx.gbl...
Dim fh As IntPtr = CreateFile("\\.\A:", _
GENERIC_READ, _
FILE_SHARE_READ, _
IntPtr.Zero, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, _
0)
All variables are Int32. Great. Now when I try to open a stream for
reading using:
Dim sr As New FileStream(fh, FileAccess.Read)
I get...
Unhandled Exception: System.IO.IOException: The parameter is incorrect.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream.get_Length()
at System.IO.FileStream..ctor(IntPtr handle, FileAccess access, Boolean
ownsHandle, Int32 bufferSize, Boolean isAsync)
at System.IO.FileStream..ctor(IntPtr handle, FileAccess access)
Private Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As
IntPtr, ByVal lpBuffer As IntPtr, ByVal nNumberOfBytesToRead As Int32, ByRef
lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Int32
"Terry Olsen" <tols...@hotmail.com> wrote in message
news:OclFk8X4...@TK2MSFTNGP09.phx.gbl...
That's true for .NET 1.0/1.1, which seem to suffer from a bug (see:
URL:http://groups.google.de/group/microsoft.public.dotnet.framework.interop/msg/33df91265dd847a1).
The OP's code should work with .NET 2.0.
"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:uSWEiIZ...@TK2MSFTNGP10.phx.gbl...
Dim buf(18432) As Byte
Dim fh As IntPtr = CreateFile("\\.\" & cboFloppy.Text, _
GENERIC_WRITE, _
FILE_SHARE_WRITE, _
IntPtr.Zero, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, _
0)
If fh.ToInt32 < 0 Then
MsgBox("Bad Disk or No Disk in Drive. Can't continue.",
MsgBoxStyle.Critical)
btnCreate.Enabled = True
Exit Sub
End If
Dim sr As New FileStream(ImageName, FileMode.Open, FileAccess.Read,
FileShare.None)
With ProgressBar1
.Minimum = 0
.Maximum = 80
.Value = 0
End With
For i As Integer = 1 To 80
sr.Read(buf, 0, 18432)
Dim y As Int32 = 0
Dim x As Int32
x = WriteFile(fh, buf, 18432, y, IntPtr.Zero) 'This line give me an
"object reference not set to an instance" message.
Next
This routine is almost identical to the routine that reads from the
floppy and writes to a file. This one just reads from the file and
attempts to write to the floppy. But I'm getting the error above. Hope
you can help. Thanks.
Post the declaration of 'WriteFile' you are using.
*** Sent via Developersdex http://www.developersdex.com ***
=> 'ByRef lpNumberOfBytesWritten As Int32'.
"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:%23wQWDKk...@TK2MSFTNGP15.phx.gbl...