[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 956
  • Last Modified:

Clear Floppy drive DISK cache when using DeviceIoControl

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
Axter
Asked:
Axter
  • 12
  • 5
  • 2
1 Solution
 
jkrCommented:
Have you tried 'FSCTL_DISMOUNT_VOLUME'?
0
 
AxterAuthor Commented:
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
 
AxterAuthor Commented:
>>Have you tried 'FSCTL_DISMOUNT_VOLUME'?

Just read some info on the web on this, and this does not work for Windows95.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
jkrCommented:
It'll definitely invalidate the cache. But, since you need that for Win95 also, this CTLCODE seems out of question...
0
 
AxterAuthor Commented:
>>this CTLCODE seems out of question...

Any other ideas?
0
 
AxterAuthor Commented:
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
 
MadshiCommented:
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
 
AxterAuthor Commented:
>>That should do what you asked for.

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

0
 
MadshiCommented:
Let's wait until Axter tried my suggestion...
0
 
AxterAuthor Commented:
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
 
AxterAuthor Commented:
Code usage:
void func()
{
      LockPhysicalVolume();
      CreateDos6FormattedFloppyDiskViaImage();
      UnlockPhysicalVolume();
}

When I run this code LockPhysicalVolume has an error code of 583 in reg_Flags.
0
 
AxterAuthor Commented:
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
 
AxterAuthor Commented:
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
 
AxterAuthor Commented:
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
 
MadshiCommented:
Well, thanks for a C grade.
0
 
AxterAuthor Commented:
>>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
 
MadshiCommented:
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
 
AxterAuthor Commented:
>>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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 12
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now