lockfile is not working



我已经锁定了我的文件从0到5个字节,我写一些字符字节,但它不停止写入,即使文件被锁定,我不知道是什么错了,我已经尝试了createFile函数中的每种类型的标志,但没有成功,请帮助。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <stdlib.h>
void __cdecl _tmain(int argc, TCHAR *argv[])
{
DWORD dwBytesWritten;
HANDLE hFile;
BOOL filesize= FALSE;
LARGE_INTEGER pl_int;
PLARGE_INTEGER pli_int = &pl_int;
char * Buffer = (char *)malloc (1);
Buffer[0]='0';
printf("Detect Lock Process Startedn");
hFile = CreateFile(argv[1],         // open One.txt
          GENERIC_WRITE|GENERIC_READ,             // open for reading
          FILE_SHARE_READ | FILE_SHARE_WRITE,           // do not share
          NULL,                     // no security
          OPEN_EXISTING,            // existing file only
          FILE_ATTRIBUTE_NORMAL,    // normal file
          NULL);                    // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
   printf("Could not open %sn",argv[1]); 
   return;
}
filesize = GetFileSizeEx(hFile, pli_int);
__int64 fsizes= (__int64)(pli_int->QuadPart);
if (filesize == FALSE)
    printf("Could not get file sizen");
else
    _tprintf(TEXT("%s (%d bytes) n"), argv[1], fsizes);
bool lock = LockFile(hFile,5,0,10,0);
if (lock == FALSE)
    printf("lockfailedn");
for(int x=0; x < fsizes; x++)
{
    if (!WriteFile(hFile,Buffer,1,&dwBytesWritten,NULL))
    //if (!ReadFile(hFile,Buffer,1,&dwBytesWritten,NULL))
    {
        printf("File Locked-could not Write file at byte %dn",x);
    }
    else
        printf("File Written at byte %dn",x);
}
free(Buffer);
CloseHandle(hFile);
}

调用这个进程的主进程在下面,我之前没有提到,很抱歉。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <stdlib.h>
void DisplayError(LPTSTR lpszFunction);
void __cdecl _tmain(int argc, TCHAR *argv[])
{
HANDLE hFile;

if(argc < 4 || argc%2 != 0) //check if low count or odd count
{
    printf("Wrong set of parameters n");
    printf("Parameters=>  <lockfile> <File Name> (<Lock region Start value> <Lock        reigion Stop value>......)n");
    return;
}
if (argc > 12)
{
    printf("Too many argumentsn");
    return;
}
// Open the existing file.
hFile = CreateFile(argv[1],         // open One.txt
          GENERIC_READ,             // open for reading
          FILE_SHARE_READ | FILE_SHARE_WRITE,           // do not share
          NULL,                     // no security
          OPEN_EXISTING,            // existing file only
          FILE_ATTRIBUTE_NORMAL,    // normal file
          NULL);                    // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
   printf("Could not open %sn",argv[1]); 
   return;
}

int i=argc-2;
int count[10];
for (int j=0; j<i; j++)
{
    count[j]=_ttoi(argv[2+j]);
}

for(int z=0; z<i; z=z+2)
{
    if(!LockFile(hFile,count[z],0,count[z+1]-count[z],0))
        DisplayError(TEXT("LockFile"));
    else
        printf("file locked from %d to %d bytesn",count[z],count[z+1]);
} 
STARTUPINFO si;
PROCESS_INFORMATION pi;
printf("Calling Detect Lockn");
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
LPTSTR szCmdline = _tcsdup(TEXT("C:\detectlock\Debug\detectlock.exe ass23.txt"));
if( !CreateProcess( NULL,   // No module name (use command line)
    szCmdline,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d)n", GetLastError() );
    return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle(hFile);
//CloseHandle(hAppend);

}

来自MSDN文章中的LockFile():

锁定指定的文件,让调用进程独占访问。

好吧,那确实有效。您的测试没有验证另一个进程是否被锁定在文件之外。

锁文件并不阻止锁持有者写文件:锁的目的是防止其他进程访问被锁的部分。

相关内容

  • 没有找到相关文章

最新更新