Author |
Message |
Chip #1 / 15
|
 createfile api call
I am looking for some information on the windows api. I need to read a disk in the floppy drive byte by byte, non MS format. What api calls are used to do this? From what I've read, it should be possible to open the drive as a device. I'm using createfile and readfile, but I'm probably not using the correct parameters. I'm on an NT 4.0 server. Can anyone help?
|
Sat, 28 Dec 2002 03:00:00 GMT |
|
 |
Jonathan Woo #2 / 15
|
 createfile api call
You can use the API but CreateFile has a lot of arguments that will take you a bit to sort through. Why can't you just open the file in binary mode and read it into a Byte array, or read a byte at a time? I don't know what you mean by MS format but I can't see any reason you couldn't read the file from VB. -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
Quote: > I am looking for some information on the windows api. > I need to read a disk in the floppy drive byte by byte, non MS format. What > api calls are > used to do this? From what I've read, it should be possible to open the > drive as a device. I'm using createfile and readfile, but > I'm probably not using the correct parameters. I'm on an NT 4.0 server. > Can anyone help?
|
Sat, 28 Dec 2002 03:00:00 GMT |
|
 |
Chip #3 / 15
|
 createfile api call
It's not a file. The disk is formatted in something other than the normal PC format, what I've called the MS format. What I need to do is open the whole device and read what's on the disk, not just open a file. If there is a file there it's defined using something other than FAT. I get a disk not formatted message when I try to look at it with Explorer or something like that. I've done some reading on the createfile and the examples show how to read a physical drive, but not a floppy. So far I haven't been able to figure out what I need to do.
Quote: > You can use the API but CreateFile has a lot of arguments that will take you > a bit to sort through. > Why can't you just open the file in binary mode and read it into a Byte > array, or read a byte at a time? > I don't know what you mean by MS format but I can't see any reason you > couldn't read the file from VB. > -- > Jonathan Wood > SoftCircuits Programming > http://www.softcircuits.com
> > I am looking for some information on the windows api. > > I need to read a disk in the floppy drive byte by byte, non MS format. > What > > api calls are > > used to do this? From what I've read, it should be possible to open the > > drive as a device. I'm using createfile and readfile, but > > I'm probably not using the correct parameters. I'm on an NT 4.0 server. > > Can anyone help?
|
Sat, 28 Dec 2002 03:00:00 GMT |
|
 |
Howard Henry Schlunde #4 / 15
|
 createfile api call
Consider reading KB article Q138434 [ http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. It talks about reading individual sectors from a CDROM, but there should be no difference between it and a floppy (except you won't need to do media locking, VirtualAlloc, VirtualFree, and you'll use a different path to A: or B:). Unfortunately for you, it is in C code, but it still will remain very similar for VB since they are Win32 APIs. -- Howard Henry Schlunder Winamp is currently playing: sarah mclachlan--possession-112k
Quote: > I am looking for some information on the windows api. > I need to read a disk in the floppy drive byte by byte, non MS format. What > api calls are > used to do this? From what I've read, it should be possible to open the > drive as a device. I'm using createfile and readfile, but > I'm probably not using the correct parameters. I'm on an NT 4.0 server. > Can anyone help?
|
Sat, 28 Dec 2002 03:00:00 GMT |
|
 |
Chip #5 / 15
|
 createfile api call
Thanks for the tip on the article I've managed to use GetdiskFreeSpace to calculate number of bytes to read and CreateFile to open the A:\ drive. Now I'm having trouble allocating memory with VirtualAlloc. Does anyone know what the constant values are for MEM_COMMIT, MEM_RESERVE, and PAGE_READWRITE? When the following lines execute lpSector = 0 and ReadFile causes an error that shuts down VB. lpSector = VirtualAlloc(ByVal 0&, BytesToRead, MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE) fSuccess = ReadFile(fHandle, lpSector, BytesToRead, lBytesRead, ByVal 0&) Anyone have any ideas what to do next? Thanks
Quote: > Consider reading KB article Q138434 [ > http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. It talks > about reading individual sectors from a CDROM, but there should be no > difference between it and a floppy (except you won't need to do media > locking, VirtualAlloc, VirtualFree, and you'll use a different path to A: or > B:). Unfortunately for you, it is in C code, but it still will remain very > similar for VB since they are Win32 APIs. > -- > Howard Henry Schlunder > Winamp is currently playing: > sarah mclachlan--possession-112k
> > I am looking for some information on the windows api. > > I need to read a disk in the floppy drive byte by byte, non MS format. > What > > api calls are > > used to do this? From what I've read, it should be possible to open the > > drive as a device. I'm using createfile and readfile, but > > I'm probably not using the correct parameters. I'm on an NT 4.0 server. > > Can anyone help?
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Howard Henry Schlunde #6 / 15
|
 createfile api call
By all means, don't even touch VirtualAlloc. VB can do memory allocation for you safely and in a much simpler manner. Just: Dim LotsOBytes() As Byte and then after you know how many bytes to read (which you seem to be getting from GetdiskFreeSpace), you do a: ReDim LotsOBytes(BytesToRead-1) That will initialize your buffer which can be sent to ReadFile. -- Howard Henry Schlunder Winamp is currently playing: tears for fears--shout
Quote: > Thanks for the tip on the article > I've managed to use GetdiskFreeSpace to calculate number of bytes to read > and CreateFile to open the A:\ drive. Now I'm having trouble allocating > memory with VirtualAlloc. > Does anyone know what the constant values are for MEM_COMMIT, MEM_RESERVE, > and PAGE_READWRITE? > When the following lines execute lpSector = 0 and ReadFile causes an error > that shuts down VB. > lpSector = VirtualAlloc(ByVal 0&, BytesToRead, MEM_COMMIT Or MEM_RESERVE, > PAGE_READWRITE) > fSuccess = ReadFile(fHandle, lpSector, BytesToRead, lBytesRead, ByVal 0&) > Anyone have any ideas what to do next? > Thanks
> > Consider reading KB article Q138434 [ > > http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. It > talks > > about reading individual sectors from a CDROM, but there should be no > > difference between it and a floppy (except you won't need to do media > > locking, VirtualAlloc, VirtualFree, and you'll use a different path to A: > or > > B:). Unfortunately for you, it is in C code, but it still will remain > very > > similar for VB since they are Win32 APIs. > > -- > > Howard Henry Schlunder > > Winamp is currently playing: > > sarah mclachlan--possession-112k
> > > I am looking for some information on the windows api. > > > I need to read a disk in the floppy drive byte by byte, non MS format. > > What > > > api calls are > > > used to do this? From what I've read, it should be possible to open the > > > drive as a device. I'm using createfile and readfile, but > > > I'm probably not using the correct parameters. I'm on an NT 4.0 server. > > > Can anyone help?
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Chip #7 / 15
|
 createfile api call
I just got the same information from Microsoft support VirtualAlloc is unsupported. fSuccess = ReadFile(fHandle, mybuff(0), BytesToRead, lBytesRead, 0) So what's wrong with this line of code? When it executes it throws a DR Watson error and then shuts VB down. How do I trap this error so I can look at it?
Quote: > By all means, don't even touch VirtualAlloc. VB can do memory allocation > for you safely and in a much simpler manner. Just: > Dim LotsOBytes() As Byte > and then after you know how many bytes to read (which you seem to be getting > from GetdiskFreeSpace), you do a: > ReDim LotsOBytes(BytesToRead-1) > That will initialize your buffer which can be sent to ReadFile. > -- > Howard Henry Schlunder > Winamp is currently playing: > tears for fears--shout
> > Thanks for the tip on the article > > I've managed to use GetdiskFreeSpace to calculate number of bytes to read > > and CreateFile to open the A:\ drive. Now I'm having trouble allocating > > memory with VirtualAlloc. > > Does anyone know what the constant values are for MEM_COMMIT, MEM_RESERVE, > > and PAGE_READWRITE? > > When the following lines execute lpSector = 0 and ReadFile causes an error > > that shuts down VB. > > lpSector = VirtualAlloc(ByVal 0&, BytesToRead, MEM_COMMIT Or MEM_RESERVE, > > PAGE_READWRITE) > > fSuccess = ReadFile(fHandle, lpSector, BytesToRead, lBytesRead, ByVal 0&) > > Anyone have any ideas what to do next? > > Thanks
> > > Consider reading KB article Q138434 [ > > > http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. It > > talks > > > about reading individual sectors from a CDROM, but there should be no > > > difference between it and a floppy (except you won't need to do media > > > locking, VirtualAlloc, VirtualFree, and you'll use a different path to > A: > > or > > > B:). Unfortunately for you, it is in C code, but it still will remain > > very > > > similar for VB since they are Win32 APIs. > > > -- > > > Howard Henry Schlunder > > > Winamp is currently playing: > > > sarah mclachlan--possession-112k
> > > > I am looking for some information on the windows api. > > > > I need to read a disk in the floppy drive byte by byte, non MS format. > > > What > > > > api calls are > > > > used to do this? From what I've read, it should be possible to open > the > > > > drive as a device. I'm using createfile and readfile, but > > > > I'm probably not using the correct parameters. I'm on an NT 4.0 > server. > > > > Can anyone help?
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Howard Henry Schlunde #8 / 15
|
 createfile api call
There is nothing wrong with that line of code. Could you show all of your code for doing this, including the API declarations? -- Howard Henry Schlunder Winamp is currently playing: no doubt--you can do it
Quote: > I just got the same information from Microsoft support VirtualAlloc is > unsupported. > fSuccess = ReadFile(fHandle, mybuff(0), BytesToRead, lBytesRead, 0) > So what's wrong with this line of code? > When it executes it throws a DR Watson error and then shuts VB down. How do > I trap this error so I can look at it?
> > By all means, don't even touch VirtualAlloc. VB can do memory allocation > > for you safely and in a much simpler manner. Just: > > Dim LotsOBytes() As Byte > > and then after you know how many bytes to read (which you seem to be > getting > > from GetdiskFreeSpace), you do a: > > ReDim LotsOBytes(BytesToRead-1) > > That will initialize your buffer which can be sent to ReadFile. > > -- > > Howard Henry Schlunder > > Winamp is currently playing: > > tears for fears--shout
> > > Thanks for the tip on the article > > > I've managed to use GetdiskFreeSpace to calculate number of bytes to > read > > > and CreateFile to open the A:\ drive. Now I'm having trouble allocating > > > memory with VirtualAlloc. > > > Does anyone know what the constant values are for MEM_COMMIT, > MEM_RESERVE, > > > and PAGE_READWRITE? > > > When the following lines execute lpSector = 0 and ReadFile causes an > error > > > that shuts down VB. > > > lpSector = VirtualAlloc(ByVal 0&, BytesToRead, MEM_COMMIT Or > MEM_RESERVE, > > > PAGE_READWRITE) > > > fSuccess = ReadFile(fHandle, lpSector, BytesToRead, lBytesRead, ByVal > 0&) > > > Anyone have any ideas what to do next? > > > Thanks
> > > > Consider reading KB article Q138434 [ > > > > http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. It > > > talks > > > > about reading individual sectors from a CDROM, but there should be no > > > > difference between it and a floppy (except you won't need to do media > > > > locking, VirtualAlloc, VirtualFree, and you'll use a different path to > > A: > > > or > > > > B:). Unfortunately for you, it is in C code, but it still will remain > > > very > > > > similar for VB since they are Win32 APIs. > > > > -- > > > > Howard Henry Schlunder > > > > Winamp is currently playing: > > > > sarah mclachlan--possession-112k
> > > > > I am looking for some information on the windows api. > > > > > I need to read a disk in the floppy drive byte by byte, non MS > format. > > > > What > > > > > api calls are > > > > > used to do this? From what I've read, it should be possible to open > > the > > > > > drive as a device. I'm using createfile and readfile, but > > > > > I'm probably not using the correct parameters. I'm on an NT 4.0 > > server. > > > > > Can anyone help?
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Chip #9 / 15
|
 createfile api call
Option Explicit Const GENERIC_READ = &H80000000 Const GENERIC_WRITE = &H40000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const CREATE_ALWAYS = 2 Const CREATE_NEW = 1 Const OPEN_ALWAYS = 4 Const OPEN_EXISTING = 3 Const TRUNCATE_EXISTING = 5 Const FILE_ATTRIBUTE_ARCHIVE = &H20 Const FILE_ATTRIBUTE_HIDDEN = &H2 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_ATTRIBUTE_READONLY = &H1 Const FILE_ATTRIBUTE_SYSTEM = &H4 Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000 Const FILE_FLAG_NO_BUFFERING = &H20000000 Const FILE_FLAG_OVERLAPPED = &H40000000 Const FILE_FLAG_POSIX_SEMANTICS = &H1000000 Const FILE_FLAG_RANDOM_ACCESS = &H10000000 Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000 Const FILE_FLAG_WRITE_THROUGH = &H80000000 Private Type LARGE_INTEGER lowpart As Long highpart As Long End Type Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ lpSecurityAttributes As Any, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ lpBuffer As Any, _ ByVal nNumberOfBytesToRead As Long, _ ByVal lpNumberOfBytesRead As Long, _ ByVal lpOverlapped As Long) As Long Private Declare Function GetLastError Lib "kernel32" () As Long Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As Any, _ ByVal iSize As Long, _ ByVal iAllocType As Long, _ ByVal iflProtect As Long) As Long Private Declare Function VirtualFree Lib "kernel32" Alias "VirtualFreeA" _ (ByVal lpAddress As Long, _ ByVal iSize As Long, _ ByVal iAllocType As Long) Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" _ (ByVal lpRootPathName As String, _ iSectors As LARGE_INTEGER, _ iBytes As LARGE_INTEGER, _ iFreeCluster As LARGE_INTEGER, _ iTotalClusters As LARGE_INTEGER) As Long Private Function CLargeInt(Lo As Long, Hi As Long) As Double 'This function converts the LARGE_INTEGER data type to a double Dim dblLo As Double, dblHi As Double If Lo < 0 Then dblLo = 2 ^ 32 + Lo Else dblLo = Lo End If If Hi < 0 Then dblHi = 2 ^ 32 + Hi Else dblHi = Hi End If CLargeInt = dblLo + dblHi * 2 ^ 32 End Function Private Sub Command1_Click() Dim fHandle As Long Dim fSuccess As Long Dim BytesToRead As Long Dim lBytesRead As Long Dim errTemp As Long Dim iSectors As LARGE_INTEGER Dim iBytes As LARGE_INTEGER Dim iFreeClusters As LARGE_INTEGER Dim iTotalClusters As LARGE_INTEGER Dim dbSectors As Double Dim dbBytes As Double Dim dbFreeClusters As Double Dim dbTotalClusters As Double Dim Result As Boolean 'Get the disk information Result = GetDiskFreeSpace("A:", iSectors, iBytes, _ iFreeClusters, iTotalClusters) 'Convert the return values from LARGE_INTEGER to doubles dbSectors = CLargeInt(iSectors.lowpart, iSectors.highpart) dbBytes = CLargeInt(iBytes.lowpart, iBytes.highpart) dbFreeClusters = CLargeInt(iFreeClusters.lowpart, iFreeClusters.highpart) dbTotalClusters = CLargeInt(iTotalClusters.lowpart, iTotalClusters.highpart) BytesToRead = dbSectors * dbBytes 'Create File Handle fHandle = CreateFile("\\.\" & "A:", _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ 0&, 0&) Dim mybuff() As Byte ReDim mybuff(BytesToRead - 1) 'Blows up right here!!!!!!!!! fSuccess = ReadFile(fHandle, mybuff(0), _ BytesToRead, lBytesRead, 0) errTemp = GetLastError() End Sub
Quote: > There is nothing wrong with that line of code. Could you show all of your > code for doing this, including the API declarations? > -- > Howard Henry Schlunder > Winamp is currently playing: > no doubt--you can do it
> > I just got the same information from Microsoft support VirtualAlloc is > > unsupported. > > fSuccess = ReadFile(fHandle, mybuff(0), BytesToRead, lBytesRead, 0) > > So what's wrong with this line of code? > > When it executes it throws a DR Watson error and then shuts VB down. How > do > > I trap this error so I can look at it?
> > > By all means, don't even touch VirtualAlloc. VB can do memory > allocation > > > for you safely and in a much simpler manner. Just: > > > Dim LotsOBytes() As Byte > > > and then after you know how many bytes to read (which you seem to be > > getting > > > from GetdiskFreeSpace), you do a: > > > ReDim LotsOBytes(BytesToRead-1) > > > That will initialize your buffer which can be sent to ReadFile. > > > -- > > > Howard Henry Schlunder > > > Winamp is currently playing: > > > tears for fears--shout
> > > > Thanks for the tip on the article > > > > I've managed to use GetdiskFreeSpace to calculate number of bytes to > > read > > > > and CreateFile to open the A:\ drive. Now I'm having trouble > allocating > > > > memory with VirtualAlloc. > > > > Does anyone know what the constant values are for MEM_COMMIT, > > MEM_RESERVE, > > > > and PAGE_READWRITE? > > > > When the following lines execute lpSector = 0 and ReadFile causes an > > error > > > > that shuts down VB. > > > > lpSector = VirtualAlloc(ByVal 0&, BytesToRead, MEM_COMMIT Or > > MEM_RESERVE, > > > > PAGE_READWRITE) > > > > fSuccess = ReadFile(fHandle, lpSector, BytesToRead, lBytesRead, ByVal > > 0&) > > > > Anyone have any ideas what to do next? > > > > Thanks
> > > > > Consider reading KB article Q138434 [ > > > > > http://support.microsoft.com/support/kb/articles/Q138/4/34.asp ]. > It > > > > talks > > > > > about reading individual sectors from a CDROM, but there should be > no > > > > > difference between it and a floppy (except you won't need to do > media > > > > > locking, VirtualAlloc, VirtualFree, and you'll use a different path > to > > > A: > > > > or > > > > > B:). Unfortunately for you, it is in C code, but it still will > remain > > > > very > > > > > similar for VB since they are Win32 APIs. > > > > > -- > > > > > Howard Henry Schlunder > > > > > Winamp is currently playing: > > > > > sarah mclachlan--possession-112k
> > > > > > I am looking for some information on the windows api. > > > > > > I need to read a disk in the floppy drive byte by byte, non MS > > format. > > > > > What > > > > > > api calls are > > > > > > used to do this? From what I've read, it should be possible to > open > > > the > > > > > > drive as a device. I'm using createfile and readfile, but > > > > > > I'm probably not using the correct parameters. I'm on an NT 4.0 > > > server. > > > > > > Can anyone help?
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Howard Henry Schlunde #10 / 15
|
 createfile api call
Wow, there are a lot of things wrong with that. Specifically what is causing your crash is the improper ReadFile API declaration. That last two parrameters must be passed ByRef, not ByVal. Aside from that, the GetFreeDiskSpace API declaration is wrong. It almost fits the GetFreeDiskSpaceEx API's declaration. In any event, those free space API calls don't even give you the number you are looking for. In the KB article they were using a custom made GetDriveGeometry function. I reimplemented it here in Visual Basic's Command1_Click sub. I got rid of several things that didn't work or weren't needed. The following is the end result and indeed, I was able to read a raw unformatted floppy (a Linux boot disk) and save an image of it to my hard disk. Option Explicit Const GENERIC_READ = &H80000000 Const GENERIC_WRITE = &H40000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const OPEN_EXISTING = 3 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_FLAG_NO_BUFFERING = &H20000000 Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000 Private Type LARGE_INTEGER lowpart As Long highpart As Long End Type Private Type DISK_GEOMETRY Cylinders As LARGE_INTEGER MediaType As Long TracksPerCylinder As Long SectorsPerTrack As Long BytesPerSector As Long End Type Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Sub Command1_Click() Dim fHandle As Long Dim fSuccess As Long Dim BytesToRead As Long Dim lBytesRead As Long Dim VolumeGeometry As DISK_GEOMETRY Dim mybuff() As Byte 'Create File Handle fHandle = CreateFile("\\.\" & "A:", _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, 0&) 'Obtain the Volume Geometries Call DeviceIoControl(fHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, VolumeGeometry, LenB(VolumeGeometry), lBytesRead, ByVal 0&) 'Calculate the total number of bytes on this volume BytesToRead = VolumeGeometry.Cylinders.lowpart * VolumeGeometry.TracksPerCylinder * VolumeGeometry.SectorsPerTrack * VolumeGeometry.BytesPerSector 'Initialize our buffer to store the volume data ReDim mybuff(BytesToRead - 1) 'Read the raw shiznits from the floppy fSuccess = ReadFile(fHandle, mybuff(0), _ BytesToRead, lBytesRead, ByVal 0&) If fSuccess Then 'Truncate our buffer of all the unread bytes on the end ReDim Preserve mybuff(0 To lBytesRead - 1) 'Write all that floppy data to our hard disk for later usage Open "C:\FloppyImage.dat" For Binary As #1 Put #1, , mybuff Close #1 End If 'Clean up after ourselves Call CloseHandle(fHandle) End Sub Private Function CLargeInt(Lo As Long, Hi As Long) As Double 'This function converts the LARGE_INTEGER data type to a double Dim dblLo As Double, dblHi As Double If Lo < 0 Then dblLo = 2 ^ 32 + Lo Else dblLo = Lo If Hi < 0 Then dblHi = 2 ^ 32 + Hi Else dblHi = Hi CLargeInt = dblLo + dblHi * 2 ^ 32 End Function -- Howard Henry Schlunder Winamp is currently playing: Mariah Carey - Anytime You Need A Friend Quote:
> Option Explicit > Const GENERIC_READ = &H80000000 > Const GENERIC_WRITE = &H40000000 > Const FILE_SHARE_READ = &H1 > Const FILE_SHARE_WRITE = &H2 > Const CREATE_ALWAYS = 2 > Const CREATE_NEW = 1 > Const OPEN_ALWAYS = 4 > Const OPEN_EXISTING = 3 > Const TRUNCATE_EXISTING = 5 > Const FILE_ATTRIBUTE_ARCHIVE = &H20 > Const FILE_ATTRIBUTE_HIDDEN = &H2 > Const FILE_ATTRIBUTE_NORMAL = &H80 > Const FILE_ATTRIBUTE_READONLY = &H1 > Const FILE_ATTRIBUTE_SYSTEM = &H4 > Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000 > Const FILE_FLAG_NO_BUFFERING = &H20000000 > Const FILE_FLAG_OVERLAPPED = &H40000000 > Const FILE_FLAG_POSIX_SEMANTICS = &H1000000 > Const FILE_FLAG_RANDOM_ACCESS = &H10000000 > Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000 > Const FILE_FLAG_WRITE_THROUGH = &H80000000 > Private Type LARGE_INTEGER > lowpart As Long > highpart As Long > End Type > Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" > (ByVal lpFileName As String, _ > ByVal > dwDesiredAccess As Long, _ > ByVal > dwShareMode As Long, _ > lpSecurityAttributes As Any, _ > ByVal > dwCreationDisposition As Long, _ > ByVal > dwFlagsAndAttributes As Long, _ > ByVal > hTemplateFile As Long) As Long > Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ > lpBuffer As Any, _ > ByVal nNumberOfBytesToRead > As Long, _ > ByVal lpNumberOfBytesRead > As Long, _ > ByVal lpOverlapped As > Long) As Long > Private Declare Function GetLastError Lib "kernel32" () As Long > Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As > Any, _ > ByVal iSize As Long, _ > ByVal iAllocType As > Long, _ > ByVal iflProtect As > Long) As Long > Private Declare Function VirtualFree Lib "kernel32" Alias "VirtualFreeA" _ > (ByVal lpAddress As Long, > _ > ByVal iSize As Long, _ > ByVal iAllocType As Long) > Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias > "GetDiskFreeSpaceA" _ > (ByVal lpRootPathName As > String, _ > iSectors As LARGE_INTEGER, > _ > iBytes As LARGE_INTEGER, _ > iFreeCluster As > LARGE_INTEGER, _ > iTotalClusters As > LARGE_INTEGER) As Long > Private Function CLargeInt(Lo As Long, Hi As Long) As Double > 'This function converts the LARGE_INTEGER data type to a double > Dim dblLo As Double, dblHi As Double > If Lo < 0 Then > dblLo = 2 ^ 32 + Lo > Else > dblLo = Lo > End If > If Hi < 0 Then > dblHi = 2 ^ 32 + Hi > Else > dblHi = Hi > End If > CLargeInt = dblLo + dblHi * 2 ^ 32 > End Function > Private Sub Command1_Click() > Dim fHandle As Long > Dim fSuccess As Long > Dim BytesToRead As Long > Dim lBytesRead As Long > Dim errTemp As Long > Dim iSectors As LARGE_INTEGER > Dim iBytes As LARGE_INTEGER > Dim iFreeClusters As LARGE_INTEGER > Dim iTotalClusters As LARGE_INTEGER > Dim dbSectors As Double > Dim dbBytes As Double > Dim dbFreeClusters As Double > Dim dbTotalClusters As Double > Dim Result As Boolean > 'Get the disk information > Result = GetDiskFreeSpace("A:", iSectors, iBytes, _ > iFreeClusters, iTotalClusters) > 'Convert the return values from LARGE_INTEGER to doubles > dbSectors = CLargeInt(iSectors.lowpart, iSectors.highpart) > dbBytes = CLargeInt(iBytes.lowpart, iBytes.highpart) > dbFreeClusters = CLargeInt(iFreeClusters.lowpart, iFreeClusters.highpart) > dbTotalClusters = CLargeInt(iTotalClusters.lowpart, iTotalClusters.highpart) > BytesToRead = dbSectors * dbBytes > 'Create File Handle > fHandle = CreateFile("\\.\" & "A:", _ > GENERIC_READ, _ > FILE_SHARE_READ Or FILE_SHARE_WRITE, _ > ByVal 0&, _ > OPEN_EXISTING, _ > 0&, 0&) > Dim mybuff() As Byte > ReDim mybuff(BytesToRead - 1) > 'Blows up right here!!!!!!!!! > fSuccess = ReadFile(fHandle, mybuff(0), _ > BytesToRead, lBytesRead, 0) > errTemp = GetLastError() > End Sub
> > There is nothing wrong with that line of code. Could you show all of your > > code for doing this, including the API declarations? > > -- > > Howard Henry Schlunder > > Winamp is currently playing: > > no doubt--you can do it
> > > I just got the same information from Microsoft support VirtualAlloc is > > > unsupported. > > > fSuccess = ReadFile(fHandle, mybuff(0), BytesToRead, lBytesRead, 0) > > > So what's wrong with this line of code? > > > When it executes it throws a DR Watson error and then shuts VB
... read more »
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Chip #11 / 15
|
 createfile api call
Thank you very much!!!!
Wow, there are a lot of things wrong with that. Specifically what is causing your crash is the improper ReadFile API declaration. That last two parrameters must be passed ByRef, not ByVal. Aside from that, the GetFreeDiskSpace API declaration is wrong. It almost fits the GetFreeDiskSpaceEx API's declaration. In any event, those free space API calls don't even give you the number you are looking for. In the KB article they were using a custom made GetDriveGeometry function. I reimplemented it here in Visual Basic's Command1_Click sub. I got rid of several things that didn't work or weren't needed. The following is the end result and indeed, I was able to read a raw unformatted floppy (a Linux boot disk) and save an image of it to my hard disk. Option Explicit Const GENERIC_READ = &H80000000 Const GENERIC_WRITE = &H40000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const OPEN_EXISTING = 3 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_FLAG_NO_BUFFERING = &H20000000 Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000 Private Type LARGE_INTEGER lowpart As Long highpart As Long End Type Private Type DISK_GEOMETRY Cylinders As LARGE_INTEGER MediaType As Long TracksPerCylinder As Long SectorsPerTrack As Long BytesPerSector As Long End Type Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Sub Command1_Click() Dim fHandle As Long Dim fSuccess As Long Dim BytesToRead As Long Dim lBytesRead As Long Dim VolumeGeometry As DISK_GEOMETRY Dim mybuff() As Byte 'Create File Handle fHandle = CreateFile("\\.\" & "A:", _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, 0&) 'Obtain the Volume Geometries Call DeviceIoControl(fHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, VolumeGeometry, LenB(VolumeGeometry), lBytesRead, ByVal 0&) 'Calculate the total number of bytes on this volume BytesToRead = VolumeGeometry.Cylinders.lowpart * VolumeGeometry.TracksPerCylinder * VolumeGeometry.SectorsPerTrack * VolumeGeometry.BytesPerSector 'Initialize our buffer to store the volume data ReDim mybuff(BytesToRead - 1) 'Read the raw shiznits from the floppy fSuccess = ReadFile(fHandle, mybuff(0), _ BytesToRead, lBytesRead, ByVal 0&) If fSuccess Then 'Truncate our buffer of all the unread bytes on the end ReDim Preserve mybuff(0 To lBytesRead - 1) 'Write all that floppy data to our hard disk for later usage Open "C:\FloppyImage.dat" For Binary As #1 Put #1, , mybuff Close #1 End If 'Clean up after ourselves Call CloseHandle(fHandle) End Sub Private Function CLargeInt(Lo As Long, Hi As Long) As Double 'This function converts the LARGE_INTEGER data type to a double Dim dblLo As Double, dblHi As Double If Lo < 0 Then dblLo = 2 ^ 32 + Lo Else dblLo = Lo If Hi < 0 Then dblHi = 2 ^ 32 + Hi Else dblHi = Hi CLargeInt = dblLo + dblHi * 2 ^ 32 End Function -- Howard Henry Schlunder Winamp is currently playing: Mariah Carey - Anytime You Need A Friend
> Option Explicit > > Const GENERIC_READ = &H80000000 > Const GENERIC_WRITE = &H40000000 > Const FILE_SHARE_READ = &H1 > Const FILE_SHARE_WRITE = &H2 > Const CREATE_ALWAYS = 2 > Const CREATE_NEW = 1 > Const OPEN_ALWAYS = 4 > Const OPEN_EXISTING = 3 > Const TRUNCATE_EXISTING = 5 > Const FILE_ATTRIBUTE_ARCHIVE = &H20 > Const FILE_ATTRIBUTE_HIDDEN = &H2 > Const FILE_ATTRIBUTE_NORMAL = &H80 > Const FILE_ATTRIBUTE_READONLY = &H1 > Const FILE_ATTRIBUTE_SYSTEM = &H4 > Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000 > Const FILE_FLAG_NO_BUFFERING = &H20000000 > Const FILE_FLAG_OVERLAPPED = &H40000000 > Const FILE_FLAG_POSIX_SEMANTICS = &H1000000 > Const FILE_FLAG_RANDOM_ACCESS = &H10000000 > Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000 > Const FILE_FLAG_WRITE_THROUGH = &H80000000 > > > Private Type LARGE_INTEGER > lowpart As Long > highpart As Long > End Type > > Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" > (ByVal lpFileName As String, _ > ByVal > dwDesiredAccess As Long, _ > ByVal > dwShareMode As Long, _ > > lpSecurityAttributes As Any, _ > ByVal > dwCreationDisposition As Long, _ > ByVal > dwFlagsAndAttributes As Long, _ > ByVal > hTemplateFile As Long) As Long > > > > > Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ > lpBuffer As Any, _ > ByVal nNumberOfBytesToRead > As Long, _ > ByVal lpNumberOfBytesRead > As Long, _ > ByVal lpOverlapped As > Long) As Long > > > > Private Declare Function GetLastError Lib "kernel32" () As Long > > Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As > Any, _ > ByVal iSize As Long, _ > ByVal iAllocType As > Long, _ > ByVal iflProtect As > Long) As Long > > Private Declare Function VirtualFree Lib "kernel32" Alias "VirtualFreeA" _ > (ByVal lpAddress As Long, > _ > ByVal iSize As Long, _ > ByVal iAllocType As Long) > > > > Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias > "GetDiskFreeSpaceA" _ > (ByVal lpRootPathName As > String, _ > iSectors As LARGE_INTEGER, > _ > iBytes As LARGE_INTEGER, _ > iFreeCluster As > LARGE_INTEGER, _ > iTotalClusters As > LARGE_INTEGER) As Long > > Private Function CLargeInt(Lo As Long, Hi As Long) As Double > > 'This function converts the LARGE_INTEGER data type to a double > > Dim dblLo As Double, dblHi As Double > > If Lo < 0 Then > dblLo = 2 ^ 32 + Lo > Else > dblLo = Lo > End If > > If Hi < 0 Then > dblHi = 2 ^ 32 + Hi > Else > dblHi = Hi > End If > > CLargeInt = dblLo + dblHi * 2 ^ 32 > > End Function > > > Private Sub Command1_Click() > > Dim fHandle As Long > Dim fSuccess As Long > Dim BytesToRead As Long > Dim lBytesRead As Long > Dim errTemp As Long > > Dim iSectors As LARGE_INTEGER > Dim iBytes As LARGE_INTEGER > Dim iFreeClusters As LARGE_INTEGER > Dim iTotalClusters As LARGE_INTEGER > > > Dim dbSectors As Double > Dim dbBytes As Double > Dim dbFreeClusters As Double > Dim dbTotalClusters As Double > > Dim Result As Boolean > > 'Get the disk information > Result = GetDiskFreeSpace("A:", iSectors, iBytes, _ > iFreeClusters, iTotalClusters) > > > 'Convert the return values from LARGE_INTEGER to doubles > dbSectors = CLargeInt(iSectors.lowpart, iSectors.highpart) > dbBytes = CLargeInt(iBytes.lowpart, iBytes.highpart) > dbFreeClusters = CLargeInt(iFreeClusters.lowpart, iFreeClusters.highpart) > dbTotalClusters = CLargeInt(iTotalClusters.lowpart, iTotalClusters.highpart) > > > BytesToRead = dbSectors * dbBytes > > > 'Create File Handle > fHandle = CreateFile("\\.\" & "A:", _ > GENERIC_READ, _ > FILE_SHARE_READ Or FILE_SHARE_WRITE, _ > ByVal 0&, _ > OPEN_EXISTING, _ > 0&, 0&) > > > Dim mybuff() As Byte > ReDim mybuff(BytesToRead - 1) > > 'Blows up right here!!!!!!!!! > fSuccess = ReadFile(fHandle, mybuff(0), _ > BytesToRead, lBytesRead, 0) > > > errTemp = GetLastError() > > End Sub >
> > > > There is nothing wrong with
... read more »
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Chip #12 / 15
|
 createfile api call
I have another question. Ultimately, I'm trying to read the disk so that I create a copy of it on another disk. Would it be possible to save this image as a file and then use it to make other disks? Actually I know it's possible, I'm asking what might be the best way? Is it a matter of opening the floppy opening the file and then using the writefile function to transfer the image? At least that's the way I would approach it. What do you think? Thank again for all your help.
Thank you very much!!!!
Wow, there are a lot of things wrong with that. Specifically what is causing your crash is the improper ReadFile API declaration. That last two parrameters must be passed ByRef, not ByVal. Aside from that, the GetFreeDiskSpace API declaration is wrong. It almost fits the GetFreeDiskSpaceEx API's declaration. In any event, those free space API calls don't even give you the number you are looking for. In the KB article they were using a custom made GetDriveGeometry function. I reimplemented it here in Visual Basic's Command1_Click sub. I got rid of several things that didn't work or weren't needed. The following is the end result and indeed, I was able to read a raw unformatted floppy (a Linux boot disk) and save an image of it to my hard disk. Option Explicit Const GENERIC_READ = &H80000000 Const GENERIC_WRITE = &H40000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const OPEN_EXISTING = 3 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_FLAG_NO_BUFFERING = &H20000000 Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000 Private Type LARGE_INTEGER lowpart As Long highpart As Long End Type Private Type DISK_GEOMETRY Cylinders As LARGE_INTEGER MediaType As Long TracksPerCylinder As Long SectorsPerTrack As Long BytesPerSector As Long End Type Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Sub Command1_Click() Dim fHandle As Long Dim fSuccess As Long Dim BytesToRead As Long Dim lBytesRead As Long Dim VolumeGeometry As DISK_GEOMETRY Dim mybuff() As Byte 'Create File Handle fHandle = CreateFile("\\.\" & "A:", _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, 0&) 'Obtain the Volume Geometries Call DeviceIoControl(fHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, VolumeGeometry, LenB(VolumeGeometry), lBytesRead, ByVal 0&) 'Calculate the total number of bytes on this volume BytesToRead = VolumeGeometry.Cylinders.lowpart * VolumeGeometry.TracksPerCylinder * VolumeGeometry.SectorsPerTrack * VolumeGeometry.BytesPerSector 'Initialize our buffer to store the volume data ReDim mybuff(BytesToRead - 1) 'Read the raw shiznits from the floppy fSuccess = ReadFile(fHandle, mybuff(0), _ BytesToRead, lBytesRead, ByVal 0&) If fSuccess Then 'Truncate our buffer of all the unread bytes on the end ReDim Preserve mybuff(0 To lBytesRead - 1) 'Write all that floppy data to our hard disk for later usage Open "C:\FloppyImage.dat" For Binary As #1 Put #1, , mybuff Close #1 End If 'Clean up after ourselves Call CloseHandle(fHandle) End Sub Private Function CLargeInt(Lo As Long, Hi As Long) As Double 'This function converts the LARGE_INTEGER data type to a double Dim dblLo As Double, dblHi As Double If Lo < 0 Then dblLo = 2 ^ 32 + Lo Else dblLo = Lo If Hi < 0 Then dblHi = 2 ^ 32 + Hi Else dblHi = Hi CLargeInt = dblLo + dblHi * 2 ^ 32 End Function -- Howard Henry Schlunder Winamp is currently playing: Mariah Carey - Anytime You Need A Friend
> Option Explicit > > Const GENERIC_READ = &H80000000 > Const GENERIC_WRITE = &H40000000 > Const FILE_SHARE_READ = &H1 > Const FILE_SHARE_WRITE = &H2 > Const CREATE_ALWAYS = 2 > Const CREATE_NEW = 1 > Const OPEN_ALWAYS = 4 > Const OPEN_EXISTING = 3 > Const TRUNCATE_EXISTING = 5 > Const FILE_ATTRIBUTE_ARCHIVE = &H20 > Const FILE_ATTRIBUTE_HIDDEN = &H2 > Const FILE_ATTRIBUTE_NORMAL = &H80 > Const FILE_ATTRIBUTE_READONLY = &H1 > Const FILE_ATTRIBUTE_SYSTEM = &H4 > Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000 > Const FILE_FLAG_NO_BUFFERING = &H20000000 > Const FILE_FLAG_OVERLAPPED = &H40000000 > Const FILE_FLAG_POSIX_SEMANTICS = &H1000000 > Const FILE_FLAG_RANDOM_ACCESS = &H10000000 > Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000 > Const FILE_FLAG_WRITE_THROUGH = &H80000000 > > > Private Type LARGE_INTEGER > lowpart As Long > highpart As Long > End Type > > Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" > (ByVal lpFileName As String, _ > ByVal > dwDesiredAccess As Long, _ > ByVal > dwShareMode As Long, _ > > lpSecurityAttributes As Any, _ > ByVal > dwCreationDisposition As Long, _ > ByVal > dwFlagsAndAttributes As Long, _ > ByVal > hTemplateFile As Long) As Long > > > > > Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ > lpBuffer As Any, _ > ByVal nNumberOfBytesToRead > As Long, _ > ByVal lpNumberOfBytesRead > As Long, _ > ByVal lpOverlapped As > Long) As Long > > > > Private Declare Function GetLastError Lib "kernel32" () As Long > > Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As > Any, _ > ByVal iSize As Long, _ > ByVal iAllocType As > Long, _ > ByVal iflProtect As > Long) As Long > > Private Declare Function VirtualFree Lib "kernel32" Alias "VirtualFreeA" _ > (ByVal lpAddress As Long, > _ > ByVal iSize As Long, _ > ByVal iAllocType As Long) > > > > Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias > "GetDiskFreeSpaceA" _ > (ByVal lpRootPathName As > String, _ > iSectors As LARGE_INTEGER, > _ > iBytes As LARGE_INTEGER, _ > iFreeCluster As > LARGE_INTEGER, _ > iTotalClusters As > LARGE_INTEGER) As Long > > Private Function CLargeInt(Lo As Long, Hi As Long) As Double > > 'This function converts the LARGE_INTEGER data type to a double > > Dim dblLo As Double, dblHi As Double > > If Lo < 0 Then > dblLo = 2 ^ 32 + Lo > Else > dblLo = Lo > End If > > If Hi < 0 Then > dblHi = 2 ^ 32 + Hi > Else > dblHi = Hi > End If > > CLargeInt = dblLo + dblHi * 2 ^ 32 > > End Function > > > Private Sub Command1_Click() > > Dim fHandle As Long > Dim fSuccess As Long > Dim BytesToRead As Long > Dim lBytesRead As Long > Dim errTemp As Long > > Dim iSectors As LARGE_INTEGER > Dim iBytes As LARGE_INTEGER > Dim iFreeClusters As LARGE_INTEGER > Dim iTotalClusters As LARGE_INTEGER > > > Dim dbSectors As Double > Dim dbBytes As Double > Dim dbFreeClusters As Double > Dim dbTotalClusters As Double > > Dim Result As Boolean > > 'Get the disk information > Result = GetDiskFreeSpace("A:", iSectors, iBytes, _ > iFreeClusters, iTotalClusters) > > > 'Convert the return values from LARGE_INTEGER to doubles > dbSectors = CLargeInt(iSectors.lowpart,
... read more »
|
Sun, 29 Dec 2002 03:00:00 GMT |
|
 |
Bill McCarth #13 / 15
|
 createfile api call
Quote: > Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ > lpBuffer As Any, _ > ByVal
nNumberOfBytesToRead Quote: > As Long, _ > ByVal lpNumberOfBytesRead > As Long, _ > ByVal lpOverlapped As > Long) As Long
lpNumberofBytesRead has to be ByRef to return the value to you, not byVal. The lp prefix, I always read as "long pointer to" which means ByRef (except for strings if you want unicode to ansi, but that's another story)
|
Mon, 30 Dec 2002 03:00:00 GMT |
|
 |
Monte Hanse #14 / 15
|
 createfile api call
Has a look at sysinternals.com ----------------------------------- Monte Hansen http://KillerVB.com Please respond to the newsgroups -----------------------------------
I have another question. Ultimately, I'm trying to read the disk so that I create a copy of it on another disk. Would it be possible to save this image as a file and then use it to make other disks? Actually I know it's possible, I'm asking what might be the best way? Is it a matter of opening the floppy opening the file and then using the writefile function to transfer the image? At least that's the way I would approach it. What do you think? Thank again for all your help.
Thank you very much!!!!
Wow, there are a lot of things wrong with that. Specifically what is causing your crash is the improper ReadFile API declaration. That last two parrameters must be passed ByRef, not ByVal. Aside from that, the GetFreeDiskSpace API declaration is wrong. It almost fits the GetFreeDiskSpaceEx API's declaration. In any event, those free space API calls don't even give you the number you are looking for. In the KB article they were using a custom made GetDriveGeometry function. I reimplemented it here in Visual Basic's Command1_Click sub. I got rid of several things that didn't work or weren't needed. The following is the end result and indeed, I was able to read a raw unformatted floppy (a Linux boot disk) and save an image of it to my hard disk. Option Explicit Const GENERIC_READ = &H80000000 Const GENERIC_WRITE = &H40000000 Const FILE_SHARE_READ = &H1 Const FILE_SHARE_WRITE = &H2 Const OPEN_EXISTING = 3 Const FILE_ATTRIBUTE_NORMAL = &H80 Const FILE_FLAG_NO_BUFFERING = &H20000000 Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000 Private Type LARGE_INTEGER lowpart As Long highpart As Long End Type Private Type DISK_GEOMETRY Cylinders As LARGE_INTEGER MediaType As Long TracksPerCylinder As Long SectorsPerTrack As Long BytesPerSector As Long End Type Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Sub Command1_Click() Dim fHandle As Long Dim fSuccess As Long Dim BytesToRead As Long Dim lBytesRead As Long Dim VolumeGeometry As DISK_GEOMETRY Dim mybuff() As Byte 'Create File Handle fHandle = CreateFile("\\.\" & "A:", _ GENERIC_READ, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_NO_BUFFERING, 0&) 'Obtain the Volume Geometries Call DeviceIoControl(fHandle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, VolumeGeometry, LenB(VolumeGeometry), lBytesRead, ByVal 0&) 'Calculate the total number of bytes on this volume BytesToRead = VolumeGeometry.Cylinders.lowpart * VolumeGeometry.TracksPerCylinder * VolumeGeometry.SectorsPerTrack * VolumeGeometry.BytesPerSector 'Initialize our buffer to store the volume data ReDim mybuff(BytesToRead - 1) 'Read the raw shiznits from the floppy fSuccess = ReadFile(fHandle, mybuff(0), _ BytesToRead, lBytesRead, ByVal 0&) If fSuccess Then 'Truncate our buffer of all the unread bytes on the end ReDim Preserve mybuff(0 To lBytesRead - 1) 'Write all that floppy data to our hard disk for later usage Open "C:\FloppyImage.dat" For Binary As #1 Put #1, , mybuff Close #1 End If 'Clean up after ourselves Call CloseHandle(fHandle) End Sub Private Function CLargeInt(Lo As Long, Hi As Long) As Double 'This function converts the LARGE_INTEGER data type to a double Dim dblLo As Double, dblHi As Double If Lo < 0 Then dblLo = 2 ^ 32 + Lo Else dblLo = Lo If Hi < 0 Then dblHi = 2 ^ 32 + Hi Else dblHi = Hi CLargeInt = dblLo + dblHi * 2 ^ 32 End Function -- Howard Henry Schlunder Winamp is currently playing: Mariah Carey - Anytime You Need A Friend
Quote: > Option Explicit > Const GENERIC_READ = &H80000000 > Const GENERIC_WRITE = &H40000000 > Const FILE_SHARE_READ = &H1 > Const FILE_SHARE_WRITE = &H2 > Const CREATE_ALWAYS = 2 > Const CREATE_NEW = 1 > Const OPEN_ALWAYS = 4 > Const OPEN_EXISTING = 3 > Const TRUNCATE_EXISTING = 5 > Const FILE_ATTRIBUTE_ARCHIVE = &H20 > Const FILE_ATTRIBUTE_HIDDEN = &H2 > Const FILE_ATTRIBUTE_NORMAL = &H80 > Const FILE_ATTRIBUTE_READONLY = &H1 > Const FILE_ATTRIBUTE_SYSTEM = &H4 > Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000 > Const FILE_FLAG_NO_BUFFERING = &H20000000 > Const FILE_FLAG_OVERLAPPED = &H40000000 > Const FILE_FLAG_POSIX_SEMANTICS = &H1000000 > Const FILE_FLAG_RANDOM_ACCESS = &H10000000 > Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000 > Const FILE_FLAG_WRITE_THROUGH = &H80000000 > Private Type LARGE_INTEGER > lowpart As Long > highpart As Long > End Type > Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" > (ByVal lpFileName As String, _ ByVal > dwDesiredAccess As Long, _ ByVal > dwShareMode As Long, _ > lpSecurityAttributes As Any, _ ByVal > dwCreationDisposition As Long, _ ByVal > dwFlagsAndAttributes As Long, _ ByVal > hTemplateFile As Long) As Long > Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _ > lpBuffer As Any, _ > ByVal
nNumberOfBytesToRead Quote: > As Long, _ > ByVal lpNumberOfBytesRead > As Long, _ > ByVal lpOverlapped As > Long) As Long > Private Declare Function GetLastError Lib "kernel32" () As Long > Private Declare Function VirtualAlloc Lib "kernel32" (ByVal lpAddress As > Any, _ > ByVal iSize As Long, _ > ByVal iAllocType As > Long, _ > ByVal iflProtect As > Long) As Long > Private Declare Function VirtualFree Lib "kernel32" Alias "VirtualFreeA" _ > (ByVal lpAddress As Long, > _ > ByVal iSize As Long, _ > ByVal iAllocType As Long) > Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias > "GetDiskFreeSpaceA" _ > (ByVal lpRootPathName As > String, _ > iSectors As LARGE_INTEGER, > _ > iBytes As LARGE_INTEGER, _ > iFreeCluster As > LARGE_INTEGER, _ > iTotalClusters As > LARGE_INTEGER) As Long > Private Function CLargeInt(Lo As Long, Hi As Long) As Double > 'This function converts the LARGE_INTEGER data type to a double > Dim dblLo As Double, dblHi As Double > If Lo < 0 Then > dblLo = 2 ^ 32 + Lo > Else > dblLo = Lo > End If > If Hi < 0 Then > dblHi = 2 ^ 32 + Hi > Else > dblHi = Hi > End If > CLargeInt = dblLo + dblHi * 2 ^ 32 > End Function > Private Sub Command1_Click() > Dim fHandle As Long > Dim fSuccess As Long > Dim BytesToRead As Long > Dim lBytesRead As Long > Dim errTemp As Long > Dim iSectors As LARGE_INTEGER > Dim iBytes As LARGE_INTEGER > Dim iFreeClusters As LARGE_INTEGER > Dim iTotalClusters As LARGE_INTEGER > Dim dbSectors As Double > Dim dbBytes As Double > Dim dbFreeClusters As Double > Dim dbTotalClusters As Double > Dim Result As Boolean > 'Get the disk information > Result = GetDiskFreeSpace("A:", iSectors, iBytes, _ > iFreeClusters, iTotalClusters) > 'Convert the return values from LARGE_INTEGER to doubles > dbSectors = CLargeInt(iSectors.lowpart, iSectors.highpart) > dbBytes = CLargeInt(iBytes.lowpart, iBytes.highpart) > dbFreeClusters = CLargeInt(iFreeClusters.lowpart, iFreeClusters.highpart) > dbTotalClusters = CLargeInt(iTotalClusters.lowpart,
iTotalClusters.highpart) Quote: > BytesToRead = dbSectors * dbBytes > 'Create File Handle > fHandle = CreateFile("\\.\" & "A:", _ > GENERIC_READ, _ > FILE_SHARE_READ Or FILE_SHARE_WRITE, _ > ByVal 0&, _ > OPEN_EXISTING, _ > 0&, 0&) > Dim mybuff() As Byte > ReDim mybuff(BytesToRead - 1) > 'Blows up right here!!!!!!!!! > fSuccess = ReadFile(fHandle, mybuff(0), _ > BytesToRead, lBytesRead, 0) > errTemp = GetLastError() > End Sub
> > There is nothing wrong with that line of code. Could you show all of your > > code for
... read more »
|
Mon, 30 Dec 2002 03:00:00 GMT |
|
 |
Chip #15 / 15
|
 createfile api call
Looks like a great site for api stuff. As you can tell I need some help there. Thanks
Quote: > Has a look at sysinternals.com > ----------------------------------- > Monte Hansen > http://KillerVB.com > Please respond to the newsgroups > -----------------------------------
|
Mon, 30 Dec 2002 03:00:00 GMT |
|
|
|