473,421 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

CreateFile API call in VB .NET

I'm trying to create a disk image of a floppy disk. Since I can't open
the device using the system.io methods, i'm trying to use the CreateFile
API to get a handle for me. But the call fails (returning a -1).
Here's the code:

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 ***
Nov 23 '05 #1
12 21076
"Terry Olsen" <to******@hotmail.com> schrieb:
I'm trying to create a disk image of a floppy disk. Since I can't open
the device using the system.io methods, i'm trying to use the CreateFile
API to get a handle for me. But the call fails (returning a -1).
Here's the code:

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 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/>
Nov 23 '05 #2
The error code being returned from Err.lastDLLError is 87, which equates
to "The parameter is incorrect." Now which parameter are we talking
about? Still can't figure it out.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #3
Give this a try,

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" <to******@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl...
The error code being returned from Err.lastDLLError is 87, which equates
to "The parameter is incorrect." Now which parameter are we talking
about? Still can't figure it out.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 23 '05 #4
Thanks Herfried & Rocky. I'm getting a valid handle now with the
following function call:

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)
*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #5
You have to use the ReadFile API

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" <to******@hotmail.com> wrote in message
news:Oc**************@TK2MSFTNGP09.phx.gbl...
Thanks Herfried & Rocky. I'm getting a valid handle now with the
following function call:

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)
*** Sent via Developersdex http://www.developersdex.com ***

Nov 23 '05 #6
"Rocky" <no*****@nowhere.com> schrieb:
You have to use the ReadFile API

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


That's true for .NET 1.0/1.1, which seem to suffer from a bug (see:
URL:http://groups.google.de/group/micros...f91265dd847a1).
The OP's code should work with .NET 2.0.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #7
I just tried it out in .Net 2.0 and yes it does work.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uS*************@TK2MSFTNGP10.phx.gbl...
"Rocky" <no*****@nowhere.com> schrieb:
You have to use the ReadFile API

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


That's true for .NET 1.0/1.1, which seem to suffer from a bug (see:
URL:http://groups.google.de/group/micros...f91265dd847a1).
The OP's code should work with .NET 2.0.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #8
You guys have helped me to read from the floppy and create an image.
Thanks! Now I'm trying to create a floppy from the image. You'd think
it would be easy. But now i'm getting another error. Here's the code:

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.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #9
"Terry Olsen" <to******@hotmail.com> schrieb:
x = WriteFile(fh, buf, 18432, y, IntPtr.Zero) 'This line give me an
"object reference not set to an instance" message.
Next


Post the declaration of 'WriteFile' you are using.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 23 '05 #10
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As
IntPtr, _
ByVal lpBuffer As Byte(), _
ByVal nNumberOfBytesToWrite As Int32, _
ByVal lpNumberOfBytesWritten As Int32, _
ByVal lpOverlapped As IntPtr) As Int32

*** Sent via Developersdex http://www.developersdex.com ***
Nov 23 '05 #11
"Terry Olsen" <to******@hotmail.com> schrieb:
ByVal lpNumberOfBytesWritten As Int32, _


=> 'ByRef lpNumberOfBytesWritten As Int32'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 23 '05 #12
Thanks! That worked. Don't know how I missed that. My ReadFile was
correctly declared... go figure...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"Terry Olsen" <to******@hotmail.com> schrieb:
ByVal lpNumberOfBytesWritten As Int32, _


=> 'ByRef lpNumberOfBytesWritten As Int32'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dim | last post by:
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...
1
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and...
0
by: Nitin Narang | last post by:
I have a piece of code which is moved to .NET VC7 recently from VC6 and there has been an apparent change in behavior using CreateFile/Writefile functionality. I am creating a file on a shared...
4
by: JLW | last post by:
I'm having alot of difficulty with this one. I have seen no less than 20 different ways to use this function with a named pipe. I'm trying to use \\.\TAPE0 (TapeDrive). Here's my decleration: ...
5
by: Andrew Clark | last post by:
Hello, Thanks for all replies on this subject. I still cannot get CreateFile to retun a good value though. I went to PInvoke.net and saw the VB.NET declaration of this function: ...
0
by: JohnnyBoy | last post by:
I am playing with a Microsoft code example that uses the creatFile call to open a stream to a serial COM port. When I execute the example, the createFile call fails with an error code 5...
8
by: news.microsoft.com | last post by:
Hello, i'm having problems to call a api dll in vb dot net. I absolutely want to use this api call and none of the frame calls. This is my declaration: ***************** Public Structure...
3
by: Scott Bell | last post by:
Hello all, What is the equivalent in the .NET Framework for accessing a device at the block/sector level such as one would when using CreateFile on a device like "\\.\D:"? Attempting to create...
2
by: Lou | last post by:
i am using CreateFile in a VB6 app but it doesn't work in VB .NET COM dll. hGpiFile = CreateFile("\\.\BLIO1", GENERIC_READ Or GENERIC_WRITE, 0, gblSecurity, OPEN_EXISTING, 0, 0) I get an error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.