 |
|
|
 |
|
 |
if (!bStatus || !CloseHandle(hDrive)) return CDDRIVE_UNKNOWN;
If this undergoes early evaluation and bStatus is false, you will not close the file handle and will result in a file handle leak in your application.
Probably should change order.
if( !CloseHandle(hDrive) || !bStatus ) return CDDRIVE_UNKNOWN;
To make sure that file handle gets closed.
Also, why would you not evaluate the buffer just because you could not close the file handle? If bStatus is true, doesn't that mean you actually obtained valid results?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks Blake. You're right. I'll fix that today. I never thought about it not being able to close the handle and yet something still be right in the buffer. I guess that's a possibility. To be honest, I've never had the call fail so I guess I don't know if that would work or not. I may have been a little too paranoid. Of course, you would think that if I receieved valid results I would always be able to close the handle so perhaps it's moot.
Thanks for the tip on the handle thing. That statement used to be an && and I changed it a while back to make the code more concise.
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
As long as we're on the subject, this is potentially problematic as well:
if(hDrive == INVALID_HANDLE_VALUE || GetLastError() != NO_ERROR) return CDDRIVE_UNKNOWN;
According to the CreateFile API documentation, GetLastError() will only return a valid error code under two circumstances:
1. If you set dwCreationDisposition to CREATE_ALWAYS or OPEN_ALWAYS, GetLastError() will return ERROR_ALREADY_EXISTS if the file exists, otherwise it returns zero. This obviously doesn't apply in this case, though, as you're using OPEN_EXISTING.
2. If the function fails, GetLastError() will return "extended error information".
Nowhere does the documentation say that GetLastError() will return NO_ERROR - or anything else, for that matter - upon a successful call to CreateFile. The fact that it does appear to do so notwithstanding, it's never a good idea to count on undocumented behaviors, IMHO.
On the other hand, the documentation does clearly state that "[i]f the function succeeds, the return value is an open handle to a specified file." So if hDrive != INVALID_HANDLE_VALUE, we can safely assume the function succeeded.
So let's assume for the moment that CreateFile has succeeded, and yet GetLastError() returns an error code of some kind - in this case, you will return without closing the handle. It would be much better to simply do this:
if( hDrive == INVALID_HANDLE_VALUE ) return CDDRIVE_UNKNOWN;
But, of course, I am most likely just being extremeley paranoid here - I am going by a very strict interpretation of the current version of the API documentation. Chances are this problem will never occur in actual practice. But I always figure it's better to be paranoid than to potentially introduce a bug that would be very difficult to track down.... 
IMO, YMMV, etc.
*Real* programmers use "copy con:progname.exe"!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
CDs and DVDs are all right. But soon they'll be obsolete. I suggest to expand this function to new drive types. If you want of course. Anyway, thank you for contribution to the community.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I doubt that CD's or DVD's will ever go away, but your point is well taken. I'll try to add code for some of the newer similar drives.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It's posible you can translate this code to another language (like C# or VB.NET)...???
Cause i don't understand the code what you published...
I hope you can help me with this...
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
Oops, that link didn't work. Try this or just cut and paste it into your browser:
http://vbnet.mvps.org/index.html?code/disk/getmediatype.htm
Tom
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
It might be prudent (even wise) for you to become read-familiar with unmanaged C++.
It's not that I am without simpathy, as I was always cursing under by breath when the only code examples were given in VB: I never learned Basic (Fortran -> Vax Assembler-> C-> C++. . .).
One reason I've 'bought in' to .NET is that, finally, the same calls are useable in all languages*. VB.NET snippets are now pretty much readable.
You're familar, it would seem, with C#. C++ isn't so different, although it does keep you in touch with what you're actually doing (at the price of some extra typing). Think of it as a visit to your ancestoral home.
Balboos
*(occasionally requiring 15 .Missing arguments, and no documentation, but what the hell, right?)
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |