Last Updated 2002/11/06
 Programming Tips  Windows95系  索 引 
DeviceIoControlのボリュームロック
2002/11/06

Windows95 の VWIN32.Vxd を DeviceIoControl を使って
ファンクション 0x400D
サブファンクション 61 or 41
にてディスク書込みする場合,ボリュームロックをする必要がある.
もちろん書込終了後にはアンロックする必要がある.


int GetVolumeLockState( int iVolume, BOOL bFlatFAT, WORD *pwLockLevel, WORD *pwPermission )
{
    DIOC_REGISTERS diocReg;
    BOOL bResult;
    DWORD dwCb;

    *pwPermission = *pwLockLevel = 0;

    diocReg.reg_EAX   = 0x440D;
    diocReg.reg_ECX   = bFlatFAT ? 0x4870 : 0x0870;
    diocReg.reg_EBX   = iVolume;
    diocReg.reg_Flags = 1;

    bResult = DeviceIoControl( hVWin32Device, VWIN32_DIOC_DOS_IOCTL,
                &diocReg, sizeof( diocReg ), &diocReg, sizeof( diocReg ), &dwCb, 0 ); 

    if( !bResult || (diocReg.reg_Flags & 1) ) {
        return (diocReg.reg_EAX & 0xffff);
    }

    *pwLockLevel  = (WORD)diocReg.reg_EAX & 0xFFFF;
    *pwPermission = (WORD)diocReg.reg_ECX & 0xFFFF;

    return 0;
}


int LockLogicalVolume( int iVolume, int iLockLevel, int iPermission, BOOL bFlatFAT )
{
    DIOC_REGISTERS diocReg;
    BOOL bResult;
    DWORD dwCb;

    diocReg.reg_EAX   = 0x440D;
    diocReg.reg_ECX   = bFlatFAT ? 0x484A : 0x084A;
    diocReg.reg_EBX   = iVolume | (iLockLevel << 8);
    diocReg.reg_EDX   = iPermission;
    diocReg.reg_Flags = 1;

    bResult = DeviceIoControl( hVWin32Device, VWIN32_DIOC_DOS_IOCTL,
                &diocReg, sizeof( diocReg ), &diocReg, sizeof( diocReg ), &dwCb, 0 ); 

    if( !bResult || (diocReg.reg_Flags & 1) ) {
        return (diocReg.reg_EAX & 0xffff);
    }

    return 0;
}


BOOL SetLock0(HWND hDlg, int iVol)
{
   int iRet;

   hVWin32Device = CreateFile( "\\\\.\\vwin32", 0, 0, NULL, 0, FILE_FLAG_DELETE_ON_CLOSE, NULL );

   if(hVWin32Device == INVALID_HANDLE_VALUE) {
      PlaySound(MAKEINTRESOURCE(IDR_WAVE5), hInst, SND_RESOURCE | SND_SYNC );
      printf("Failed to open VWIN32.vxd");
      return FALSE;
   }

   printf("Level0 exclusive ロックを実行");

   iRet = LockLogicalVolume( iVol, 0, 0, FALSE );
   if( iRet != 0 ) {
       PlaySound(MAKEINTRESOURCE(IDR_WAVE5), hInst, SND_RESOURCE | SND_SYNC );
       printf("level0 first lock failed");
       CloseHandle( hVWin32Device );
       return FALSE;
   }

   iRet = LockLogicalVolume( iVol, 0, 4, FALSE );
   if( iRet != 0 )  {
       PlaySound(MAKEINTRESOURCE(IDR_WAVE5), hInst, SND_RESOURCE | SND_SYNC );
	   printf("level0 second lock failed");
       UnlockLogicalVolume( iVol, FALSE );
	   CloseHandle( hVWin32Device );
	   return FALSE;
   }


参照
前後のTips
DeviceIoControlのボリュームロック

DSS ProgrammingTipsCGI Ver2.00