How to lock a file when it is in use?
-
Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.
-
Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.
You can use CreateFile to open an existing file with without Sharing. i.e. passing dwShareMode of CreateFile()[^] as zero. Or you can open a file using OpenFile()[^] by passing 'OF_SHARE_DENY_READ |OF_SHARE_DENY_WRITE' to deny sharing.
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:**** -
Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.
[LockFile] locks the second file to prevent another process from accessing it while writing to it. Sample code:
#include
#includevoid main()
{
HANDLE hFile;
HANDLE hAppend;
DWORD dwBytesRead, dwBytesWritten, dwPos;
BYTE buff[4096];
char DataBuffer[] = "This is some test data to write to the file.";
DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);BOOL bErrorFlag = FALSE;
// Open the existing file.
hFile = CreateFile(TEXT("one.txt"), // open One.txt
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
CREATE_ALWAYS, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. templateif (hFile == INVALID_HANDLE_VALUE)
{printf("Could not open One.txt."); return;
}
bErrorFlag = WriteFile(
hFile, // open file handle
DataBuffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were written
NULL); // no overlapped structure// Open the existing file, or if the file does not exist,
// create a new file.hAppend = CreateFile(TEXT("two.txt"), // open Two.txt
FILE_APPEND_DATA, // open for writing
FILE_SHARE_READ, // allow multiple readers
NULL, // no security
CREATE_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. templateif (hAppend == INVALID_HANDLE_VALUE)
{
printf("Could not open Two.txt.");
return;
}// Append the first file to the end of the second file.
// Lock the second file to prevent another process from
// accessing it while writing to it. Unlock the
// file when writing is complete.while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
&& dwBytesRead > 0)
{
dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NUL