Solved

Clear Floppy drive DISK cache when using DeviceIoControl

Posted on 2004-08-20
20
918 Views
Last Modified: 2013-12-03
I'm testing out some code that writes to the floppy disk sectors using DeviceIoControl.
My problem is, that after I write the data to the sectors, any program that reads/writes via IO to the floppy disk, still sees the previous data.
Also, if I open the explore and press F5 to refresh, that does NOT clear the cache.

If I take the floppy disk in and out of the drive, then it seems to clear the cache.

How would I programmatically clear the floppy disk cache so as to see any changes made via DeviceIoControl API call?
0
Comment
Question by:Axter
  • 12
  • 5
  • 2
20 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11851632
Have you tried 'FSCTL_DISMOUNT_VOLUME'?
0
 
LVL 30

Author Comment

by:Axter
ID: 11851696
No.
How would I use that?

The target Operating system is Windows 95.

I did try some code you had in another question, but it didn't work for Windows 95.
0
 
LVL 30

Author Comment

by:Axter
ID: 11851757
>>Have you tried 'FSCTL_DISMOUNT_VOLUME'?

Just read some info on the web on this, and this does not work for Windows95.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11851786
It'll definitely invalidate the cache. But, since you need that for Win95 also, this CTLCODE seems out of question...
0
 
LVL 30

Author Comment

by:Axter
ID: 11852151
>>this CTLCODE seems out of question...

Any other ideas?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 500 total points
ID: 11858086
0
 
LVL 30

Author Comment

by:Axter
ID: 11952020
Sorry Madshi, but I don't see a usable method in above post.

If there is a working method in above links, please provide very specific details, and example usuage.

Otherwise, I will be requesting this question be deleted.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 11952123
Use "Interrupt 21h Function 440Dh Minor Code 4Bh" (Lock Physical Volume) before writing the sectors and "Interrupt 21h Function 440Dh Minor Code 6Bh" (Unlock Physical Volume) afterwards. That should do what you asked for. Details are explained in the links I posted in my previous comment.
0
 
LVL 30

Author Comment

by:Axter
ID: 11952193
>>That should do what you asked for.

I'll test it out, and get back to you today.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Expert Comment

by:Madshi
ID: 11954379
Let's wait until Axter tried my suggestion...
0
 
LVL 30

Author Comment

by:Axter
ID: 11968138
Madshi,
I tried using your method, and it didn't work.
Since you did not provide a working model, I'm not sure if it's failing because of incorrect implementation, or because it's not a functional method.

Here's the code I used.

void LockPhysicalVolume()
{
      HANDLE hDevice;
      DIOC_REGISTERS reg;
      BOOL fResult;
      DWORD cb;
      int nDrive = 0;  // Drive A:
      
      hDevice = CreateFile("\\\\.\\vwin32",0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);
      
      reg.reg_EAX = 0x440D;       // IOCTL for block devices
      reg.reg_EBX = nDrive;       // zero-based drive identifier
      reg.reg_ECX = 0x084B;
      reg.reg_EDX = 6;
      reg.reg_Flags = 0x0001;     // assume error (carry flag is set)
      
      fResult = DeviceIoControl(hDevice,
            VWIN32_DIOC_DOS_IOCTL,
            &reg, sizeof(reg),
            &reg, sizeof(reg),
            &cb, 0);
      
      if (!fResult || (reg.reg_Flags & 0x0001))
      {
            return; // error if carry flag is set
      }
      
      CloseHandle(hDevice);
}

void UnlockPhysicalVolume()
{
      HANDLE hDevice;
      DIOC_REGISTERS reg;
      BOOL fResult;
      DWORD cb;
      int nDrive = 0;  // Drive A:
      
      hDevice = CreateFile("\\\\.\\vwin32",0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL);
      
      reg.reg_EAX = 0x440D;       // IOCTL for block devices
      reg.reg_EBX = nDrive;       // zero-based drive identifier
      reg.reg_ECX = 0x086B;
      reg.reg_EDX = 6;
      reg.reg_Flags = 0x0001;     // assume error (carry flag is set)
      
      fResult = DeviceIoControl(hDevice,
            VWIN32_DIOC_DOS_IOCTL,
            &reg, sizeof(reg),
            &reg, sizeof(reg),
            &cb, 0);
      
      if (!fResult || (reg.reg_Flags & 0x0001))
      {
            return; // error if carry flag is set
      }
      
      CloseHandle(hDevice);
}
0
 
LVL 30

Author Comment

by:Axter
ID: 11968150
Code usage:
void func()
{
      LockPhysicalVolume();
      CreateDos6FormattedFloppyDiskViaImage();
      UnlockPhysicalVolume();
}

When I run this code LockPhysicalVolume has an error code of 583 in reg_Flags.
0
 
LVL 30

Author Comment

by:Axter
ID: 11968184
Madshi,

If you have use this method before, please provide some of your code, so that I can use it as an example for the proper implementation.

The links you posted do not have all the required information for usage in a win32 application.

Please provide as much detail as possible via actual code that can compile a VC++ 6.0.
0
 
LVL 30

Author Comment

by:Axter
ID: 11968482
After some search on google, I got the following method to work.

#define VWIN32_DIOC_DOS_IOCTL 1

short    lock_volume( HANDLE vwin32_handle, unsigned short permissions, unsigned
                               char drive )
{
      DIOC_REGISTERS regs;
      DWORD    returned;
      
      /*************************************************************
      *  Set the function code; 0x440D, IOCTL.
      *************************************************************/
      regs.reg_EAX = 0x440D;
      regs.reg_EBX = ( DWORD )( drive + 1 );
      regs.reg_ECX = 0x084A; /* unlock disk */
      regs.reg_EDX = permissions;
      
      /*************************************************************
      *  Call interrupt 0x21 IOCTL through Vwin32.
      *************************************************************/
      DeviceIoControl( vwin32_handle,
            VWIN32_DIOC_DOS_IOCTL,
            ( LPVOID )&regs, sizeof( regs ),
            ( LPVOID )&regs, sizeof( regs ),
            &returned, 0 );
            /*************************************************************
            *  Check the carry flag.
      *************************************************************/
      if ( ( regs.reg_Flags & 1L ) != 0 )
            return( -1 );
      else
            return( 0 );
      // may want to make it return a real error code..
}

short    unlock_volume( HANDLE vwin32_handle, unsigned char drive )
{
      DIOC_REGISTERS regs;
      DWORD    returned;
      
      /*************************************************************
      *  Set the function code; 0x440D, IOCTL.
      *************************************************************/
      regs.reg_EAX = 0x440D;
      regs.reg_EBX = ( DWORD )( drive + 1 );
      regs.reg_ECX = 0x086A; /* unlock disk */
      regs.reg_EDX = 0; // Not really necessary.
      
                                /*************************************************************
                                *  Call interrupt 0x21 IOCTL through Vwin32.
      *************************************************************/
      DeviceIoControl( vwin32_handle, VWIN32_DIOC_DOS_IOCTL,
            ( LPVOID )&regs, sizeof( regs ),
            ( LPVOID )&regs, sizeof( regs ),
            &returned, 0 );
      
            /*************************************************************
            *  Check the carry flag.
      *************************************************************/
      if ( ( regs.reg_Flags & 1L ) != 0 )
            return( -1 );
      else
            return( 0 );
      // may want to return a real error code here.
}

int  ClearFloppyDiskCache()
{
    HANDLE    vwin32_handle;
    unsigned    char    drive;
    int i,erri=0;
    /*************************************************************
      *  Open the VWIN32 device.
      *************************************************************/
    vwin32_handle = CreateFile( "\\\\.\\VWIN32", 0,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,  0, FILE_FLAG_DELETE_ON_CLOSE, NULL );
      if ( vwin32_handle == INVALID_HANDLE_VALUE )
    {
            erri=1;
            return erri;
    }
      
    // Prompt the user for drive number, 0=A:, 1=B:.
    // ...
    drive=0;
      
    // Lock the volume - or try to.
    i=lock_volume( vwin32_handle, 0,  drive );
    if (i!=0)
            erri=2;
      
    // Now lock the volume for formatting - or try to.
    i=lock_volume( vwin32_handle, 4,  drive );
      /*do something like formatting a disk here. Or don't do anything */
      
    // Always unlock the volume, even if your process failed.
    i=unlock_volume( vwin32_handle, drive );
    i=unlock_volume( vwin32_handle, drive );
    if (i!=0)
            erri=4;
      CloseHandle(vwin32_handle);
      return erri;
}
0
 
LVL 30

Author Comment

by:Axter
ID: 11968502
Code usage:
void func()
{
     CreateDos6FormattedFloppyDiskViaImage();
     ClearFloppyDiskCache();
}

The ClearFloppyDiskCache clears the cache, so that when I view the floppy drive via explorer, it contains the correct contents.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 11970877
Well, thanks for a C grade.
0
 
LVL 30

Author Comment

by:Axter
ID: 11971627
>>Well, thanks for a C grade.

Sorry Madshi, but I rarely ever give a C grade.

I gave a C grade because of two reasons.

First, I requested specific detailed information on my first followup post to your comments.

You replied with very small amount of information, that was not specific and had no details.

Moreover, I was not able to use the information you gave me for a working solution.

The only reason I accepted your post as an answer was because I was able to use information there to do a keyword search on Google, which then did turn up with the correct working method.

So your post help me find the answer, but it did not give me the answer.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 11971831
Experts-Exchange is not about doing all the work for you. I have a job and not the time to do just that. The answer I gave you is more or less correct. Your final solution uses (Un)LockLogicalVolume instead of (Un)LockPhysicalVolume. Apart from that your final solution is exactly what I said.
0
 
LVL 30

Author Comment

by:Axter
ID: 11972534
>>Experts-Exchange is not about doing all the work for you.

I didn't ask you to do the work.  I asked you for detailed information.  You failed to provide that.

>>The answer I gave you is more or less correct.

It's either correct or incorrect.  In this case, it's incorrect.

The finnal solution uses 4A and 6A.
Lock Logical Volume (4A)
Unlock Logical Volume (6A)

Your propose solution was
Lock Physical Volume (4B)
Unlock Physical Volume (6B)

>>Apart from that your final solution is exactly what I said.
You also stated, I had to perform the lock before writing to the drive.  
You also stated, I had to perform the lock before writing to the drive.  
The final solution does not perform any action until after writing to the drive.

This is a 500 point question.  I expect a lot more input for a 500 point question.
Your last comment had more content then any of your previous comments attempting to answer my question.
In other words, you put more effort into complaining about your grade, then you did in trying to help me.

If your answer was correct, I still would have awarded you an A grade.
If you had given an acceptable amount of detailed information, and your answer was wrong, I still would have given you an A grade for effort.

Your answer was wrong, and your effort was minimal.
Minimal effort deserves a minimal grade, and this is especially so, when the answer is wrong.

I graded you IAW the EE grading guidelines.
Please read the guidelines:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hi73
***********************************************************************
C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.
***********************************************************************

IAW with above guidelines, I have given you the grade you deserve.
If you have a problem with this, then you should take it up with EE and not me, because I don't make the rules.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now