Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Mehmet Caner
Thu, 27 Mar 2003 12:48
Locking drives and folders
Author: Mehmet Caner
Date: Thu, 27 Mar 2003 12:48
Date: Thu, 27 Mar 2003 12:48
114 lines
4367 bytes
4367 bytes
Hi,
I am trying to lock drives and folders on a Win98 machine.
I have two questions:
1. The computer has three logical volume (C:, D: and E:).
Volume E: is completely empty.
I can lock Floppy Drive (A:), CD-ROM Drives(F: and G:) , and Drive E:
using Level 0 lock, but can not C: and D:
Borland Help says:
The level 0 lock cannot be obtained on volumes that have any open files
or handles.
Because the system always has open files, an application cannot take a
level 0 lock
on the drive that contains the Windows 95 system files. The locking
hierarchy can be
used on volumes with open files and handles.
Any tip or sample code for Level1, Level2 and Level3 locking hierarcyy
please?
#define VWIN32_DIOC_DOS_IOCTL 1
typedef struct DIOCRegs {
DWORD reg_EBX;
DWORD reg_EDX;
DWORD reg_ECX;
DWORD reg_EAX;
DWORD reg_EDI;
DWORD reg_ESI;
DWORD reg_Flags;
} DIOC_REGISTERS;
BOOL LockDrive(int DriveNum, int LockLevel);
BOOL UnlockDrive(int DriveNum);
HANDLE hDevice;
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
bool d1 = LockDrive(3, 0); // Drive D: My second Logical Disk
if (d1) ListBox1->Items->Add ("Lock operation successfull!");
else ListBox1->Items->Add ("Lock operation failed!");
}
//-------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
bool d1 = UnlockDrive(3); // // Drive D: My second Logical Disk
if (d1) ListBox1->Items->Add ("Unlock operation successfull!");
else ListBox1->Items->Add ("Unlock operation failed!");
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
CloseHandle (hDevice);
}
//--------------------------------------------------------------------------
-
BOOL LockDrive (int DriveNum, int LockLevel)
{
BOOL fResult;
DIOC_REGISTERS regs;
BYTE bDeviceCat = 0x08;
DWORD cb;
regs.reg_Flags = 0x0001;
hDevice = CreateFile("\\\\.\\vwin32", 0, 0, (LPSECURITY_ATTRIBUTES) NULL,
0,
FILE_FLAG_DELETE_ON_CLOSE, (HANDLE) NULL);
if (hDevice == (HANDLE) INVALID_HANDLE_VALUE) return FALSE;
regs.reg_EAX = 0x440D;
regs.reg_EBX = MAKEWORD(DriveNum, LockLevel);
regs.reg_ECX = MAKEWORD(0x4A, bDeviceCat);
regs.reg_EDX = 0;
fResult = DeviceIoControl (hDevice, VWIN32_DIOC_DOS_IOCTL, ®s,
sizeof(regs), ®s, sizeof(regs), &cb, 0);
if (!fResult || (regs.reg_Flags & 0x0001)) return FALSE;
else return TRUE;
}
//--------------------------------------------------------------------------
BOOL UnlockDrive (int DriveNum)
{
BOOL fResult;
DIOC_REGISTERS regs;
BYTE bDeviceCat = 0x08;
DWORD cb;
regs.reg_Flags = 0x0001;
regs.reg_EAX = 0x440D;
regs.reg_EBX = DriveNum;
regs.reg_ECX = MAKEWORD(0x6A, bDeviceCat);
fResult = DeviceIoControl (hDevice, VWIN32_DIOC_DOS_IOCTL, ®s,
sizeof(regs), ®s, sizeof(regs), &cb, 0);
if (!fResult || (regs.reg_Flags & 0x0001)) return FALSE;
else return fResult;
}
//--------------------------------------------------------------------------
-
2. I tried CreateFile and DeviceIoControl functions for locking folders,
but it failed. Any tip please?
Best regards!
Re: Locking drives and folders
Author: Early Ehlinger
Date: Thu, 27 Mar 2003 21:25
Date: Thu, 27 Mar 2003 21:25
28 lines
1807 bytes
1807 bytes
"Mehmet Caner" <fekinci37@hotmail.com> wrote:
> fResult = DeviceIoControl (hDevice, VWIN32_DIOC_DOS_IOCTL, ®s,
> sizeof(regs), ®s, sizeof(regs), &cb, 0);
>
> if (!fResult || (regs.reg_Flags & 0x0001)) return FALSE;
> else return fResult;
> }
> 2. I tried CreateFile and DeviceIoControl functions for locking folders,
> but it failed. Any tip please?
Are you sure DeviceIoControl failed? DeviceIoControl returns an HRESULT.
From MSDN:
If the method succeeds, the return value is S_OK.
From winerror.h:
#define S_OK ((HRESULT)0x00000000L)
Therefore using !fResult to check the return from DeviceIoControl is going
to make you think that it failed when it really succeeded.
--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697
-- www.respower.com -- 500+ GHz Supercomputer Starting At USD$0.50/GHz*Hour
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads