1

I have been able to use SetFilePointerEx just fine, when setting the position only.

SetFilePointerEx(PD, 512, @PositionVar, FILE_BEGIN);
SetFilePointerEx(PD, 0, @PositionVar, FILE_CURRENT);

Both work; I can set positions and even check my current one. But when I set FILE_END as per the documentation no matter what the second parameter is and whether or not I provide a pointer for the third parameter, it fails, even on a valid handle that many other operations are able to use without fail.

For example:

SetFailed := SetFilePointerEx(PD, 0, @PositionVar, FILE_END);
SetFailed := SetFilePointerEx(PD, 0, nil, FILE_END);

Whatever I put, it fails. I am working with a handle to a physical disk and it most definitely has an end. SetFilePointer works just fine, but it's just a little more trouble than I would like.

It's not the end of the world, but whats happening.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
SuicideClutchX2
  • 87
  • 2
  • 11
  • Nice edit rob, title isn't necessarily correct though. I was only trying to get the amount of space from the physical media. Which in theory could be done via the method, no longer seems a valid way though. Search terms for someone with the problem where in the old title, but i do appreciate the post clean up even though i doubt someone will be finding any help out of this particular Q & A session. Thanks to whomever voted it up, I can finally vote up :) – SuicideClutchX2 Jan 02 '11 at 06:20
  • @Suicide, read the answers you've got on this question: http://stackoverflow.com/questions/4567422/ I allready told you SetFilePointer doesn't work with FILE_END and Disk devices, I gave you a routine that helps you move the file pointer anywhere you want using an int64, David showed you a nice way of getting the device size without using Hacky code. Why do you need to ask the same question again? – Cosmin Prund Jan 02 '11 at 09:05
  • @Cosmin, well this questions old and answered anyways. I cant get IOCTL_DISK_GET_LENGTH_INFO to work either and thats the current situation. I should have and probably will be posting a new question with the new problem when i put up the code to the current problem. – SuicideClutchX2 Jan 02 '11 at 23:17
  • @Suicide, that question was 3 days old yesterday when you said it's old: that's not old, that's pretty young if you ask me. It's also ANSWERED: You ticked an answer as accepted, how would anyone know it's not ANSWERED? – Cosmin Prund Jan 04 '11 at 08:21
  • I must have used the wrong term. Outdated? The question was a new one, it was a problem that didn't work, David answered I accepted it, it WAS answered? We moved onto the fact that I couldn't get IOCTL_DISK_GET_LENGTH_INFO to work and I made a new question (http://stackoverflow.com/questions/4581387/delphi-using-deviceiocontrol-passing-ioctl-disk-get-length-info-to-get-flash-me) which has also been solved and checked. I appreciate your help, I know what was said but the documentation doesn't say it doesn't work with disks, which is why I tried, I had not tried it before. – SuicideClutchX2 Jan 04 '11 at 23:35

1 Answers1

3

You can't do this as you have found out. You have to seek from the beginning or current. Use DeviceIoControl to find out the size of the device.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That was the next thing I was gonna work on and I appreciate you pointing that out before. I figured you would wish to slap me for keeping down what seemed like the same path. Microsoft you failed, oh wait thats not news. Answer will be excepted in a few minutes when it allows me. – SuicideClutchX2 Jan 01 '11 at 23:53
  • @suicide if it works with the non ex version then why not use that. The GpHugeFile unit I linked to will give you an example of how to get it to work >2gb – David Heffernan Jan 01 '11 at 23:56
  • I will look at the unit I only looked at that one page before. I have a little trouble with the concept of the high order and low order numbers with SetFilePointer and how to combine them into a 64 bit variable so I can read the ending value. I understand it at the bit level just cant work it out YET... I will though. – SuicideClutchX2 Jan 02 '11 at 00:02
  • Something else I have noticed. If you set FILE_END with SetFilePointer and then check it with SetFilePointerEx via the pointer, it returns a 0 (not the function return which is boolean and returning -1). SetFilePointer when you set it returns a max 32 int value like it was successful. Is the Ex API badly broken? – SuicideClutchX2 Jan 02 '11 at 00:03
  • @suicide I doubt that setfilepointer is working when seeking to the end. I think you probably don't know how to detect errors in setfilepointer. – David Heffernan Jan 02 '11 at 00:10
  • Probably not, and probably not. Only thing I use a debugger for is to check variable values and the like for small problems and trouble shooting. Thats cool alright I surrender and will not use File_End for any purpose :-p – SuicideClutchX2 Jan 02 '11 at 00:11
  • Thats something I meant to do actually, was create an 8gb file (or actually finish a dumb) and then see if it worked. I was starting to wonder if the problem was with the physical disk. There are only so many programs that need that sort of access. I really only need to work within the first couple of megabytes and maybe the end of the disk. All I am doing is moving up a partition structure to make way for Initial Program Loader code for a device. There is software like EASEUS PM that does a quick job but this is a part of a program and id like it integrated. – SuicideClutchX2 Jan 02 '11 at 00:19
  • @suicide i am sure that windows is behaving exactly as designed and there are no bugs other than our lack of skill and knowledge! – David Heffernan Jan 02 '11 at 00:22
  • Wait was that a joke? Even though my skill an knowledge sucks, windows behaving as it was designed? Never, the way it was compiled maybe hehe. Anyways thanks dude. – SuicideClutchX2 Jan 02 '11 at 00:29
  • @suicide windows is excellent. Remember that you can't read the api docs effectively or even work out how to piece together 2 DWORDS into a LARGE_INTEGER so how are you in a position to criticise. Your problems are your own. I didn't mean that to sound as harsh as it does! – David Heffernan Jan 02 '11 at 00:33
  • The funny part is the pun of "Can't piece to words together". But yeah initially that was harsh, I take no offense and your respect remains intact with me. I failed english twice, several forms of math many times. I learned pascal with only the knowledge of BASIC and reading source code, the first programming book I ever bought was on C++. I do have a copy of Mastering Delphi 5 that I acquired when the company I worked for went out of business. I should have went to school or something I did not learn correctly. Hey MSDN is the only thing that keeps me going. Still hatin on windows :-p – SuicideClutchX2 Jan 02 '11 at 00:39
  • DeviceIoControl(PD, IOCTL_DISK_GET_LENGTH_INFO, nil, 0, @DriveSize, SizeOf(DriveSize), BytesReturned, nil); seems to fail and calling GetLastError isnt telling me anything. The bytesreturned isnt being written to so I know the thing is surely not working at all. the handle as always is valid i do a check before any code uses a handle and the rest of the codes uses it just fine. I have looked into the source of an app that uses this for drive info and so far had no luck. I did notice they were using a FileStream on the physical device for the handle im too tired will figure it out later. – SuicideClutchX2 Jan 02 '11 at 05:34
  • @suicide what is the type of DriveSize? – David Heffernan Jan 02 '11 at 08:27
  • Int64 for drivesize and dword for bytes returned. I had attempted manually specifying the buffer size with no luck, but knew it wasnt the problem. I got the variable type from the source to DiskImage, it was one of my big questions I couldn't quite answer. – SuicideClutchX2 Jan 02 '11 at 13:05
  • @Suicide How about you strip your code down to the bare minimum and post it in a pastebin somewhere, we should then be able to work out what's wrong. – David Heffernan Jan 02 '11 at 13:15
  • I actually planned todo that. I apologize I was very tired last night. In case your wondering the reason I came across the source to DiskImage is it came up when i was looking for the constant for IOCTL_DISK_GET_LENGTH_INFO. After this hurdle I would have to assume all the answers are actually in that application I will have to load it up in 07 since its older code. Within the next 6 hours I will post up the unit code as well as a download link to an entire project. I am currently working in a minimal fashion to solve problems but it does need cleaning up. – SuicideClutchX2 Jan 02 '11 at 23:16
  • Im almost done, something funny. DiskSize is actually already used by sysutils. I didn't actually use that variable name, I changed it when I posted, when I am in a hurry my variable names are usually a random obscenity. I will post the code as a new question as its not the same problem as this question. – SuicideClutchX2 Jan 03 '11 at 00:07
  • Well new info, I do not see what I was doing wrong but I do have an error code finally, 50 ERROR_NOT_SUPPORTED. Will post new question with more detail in next 30 min. I want to play with it more I may find out what I am doing wrong. – SuicideClutchX2 Jan 03 '11 at 00:30
  • Alright new question, unit source, app source and executable all posted here - http://stackoverflow.com/questions/4581387/delphi-using-deviceiocontrol-passing-ioctl-disk-get-length-info-to-get-flash-me – SuicideClutchX2 Jan 03 '11 at 00:52