Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to lock lock on specific drive till i perform some operation of drive.
when i have captured lock on drive .. no other process should able to write on that drive till i release lock on that drive..
.
.
i have found one api.. ie. DeviceIoControl()..
but.. i want access on drive my process only.. but above API 'MAY' block access of all the process!!
.
i am not quite sure how lockfile() and lockfileex() works..!!
plz suggest me some way / API/ LOgic to do this!!
thank you !! :)
Posted
Comments
[no name] 2-Mar-14 7:19am    
The question is not clear. One process or all? Please proofread and edit.
lokesh.mavale 2-Mar-14 11:57am    
for a single process of my application!!!
and only my process shuld be able to access locked resource

1 solution

You could try FSCTL_LOCK_VOLUME[^].
 
Share this answer
 
Comments
lokesh.mavale 2-Mar-14 12:07pm    
thank you sir!! i have read this .. hope that it will work.!
lokesh.mavale 5-Mar-14 0:20am    
with referrance to your answer i have tried this :-->


HANDLE hdest,hf;
DWORD b,write;
hdest = CreateFile("\\\\?\\E:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
NULL,
NULL);

if (hdest == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK"),TEXT("NOT OK"),0);
}

if (DeviceIoControl( hdest,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&b,NULL))
{
MessageBox(NULL,TEXT("OK"),TEXT("OK"),0);

hf = CreateFile(TEXT("E:\\M_lucky.txt"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hf == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK AFTER LOCK I"),TEXT("NOT OK AFTRE LOCK"),0);
}
WriteFile(hf, TEXT("M very lucky"), 13, &write, NULL);
}

CloseHandle((HANDLE)hdest);
CloseHandle((HANDLE)hf);

DeviceIoControl( hdest,FSCTL_UNLOCK_VOLUME,NULL,0,NULL,0,&b,NULL);

hf = CreateFile(TEXT("E:\\MNOT_lucky.txt"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hf == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK AFTER LOCK"),TEXT("NOT OK AFTRE LOCK"),0);

}
WriteFile(hf, TEXT("M lucky"), 8, &write, NULL);
CloseHandle((HANDLE)hf);


""but........""
after getting exclusive lock on E drive, i am unable to create file.
(i have read that : only hdest(HANDLE) gets exclusive lock on that drive. )

here..!!! i want to create M_LUCKY.txt file..!!
(I need EXLUSIVE access on drive with context to PROCESS)
plz help! thanks a lottt!!

is there any other method for doing this??
Richard MacCutchan 5-Mar-14 4:06am    
What was the error from the call to CreateFile?
lokesh.mavale 8-Mar-14 0:16am    
CreateFile() returns INVALID_HANDLE_VALUE in handle_of_file!!
Richard MacCutchan 8-Mar-14 4:35am    
And what was the error code from GetLastError?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900