【问题标题】:lockfile is not working锁定文件不工作
【发布时间】:2011-10-17 00:01:21
【问题描述】:

我已将我的文件从 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 Started\n");
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 %s\n",argv[1]); 
   return;
}
filesize = GetFileSizeEx(hFile, pli_int);

__int64 fsizes= (__int64)(pli_int->QuadPart);

if (filesize == FALSE)
    printf("Could not get file size\n");
else
    _tprintf(TEXT("%s (%d bytes) \n"), argv[1], fsizes);

bool lock = LockFile(hFile,5,0,10,0);
if (lock == FALSE)
    printf("lockfailed\n");
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 %d\n",x);

    }
    else
        printf("File Written at byte %d\n",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 arguments\n");
    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 %s\n",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 bytes\n",count[z],count[z+1]);
} 
STARTUPINFO si;
PROCESS_INFORMATION pi;
printf("Calling Detect Lock\n");
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);



}

【问题讨论】:

  • 还有其他方法可以测试哪些字节被锁定了吗?
  • 你是不是误解了LockFile的用途?是防止其他进程写入文件,而不是防止你的程序写入文件。
  • 另外值得一提的是,您并没有将字节从 0 锁定到 5,而是从 5 锁定到 15。
  • 抱歉,我没有添加放置锁的调用进程。并且锁取自参数。

标签: c++ c winapi createfile lockfile


【解决方案1】:

来自 LockFile() 的 MSDN 文章:

锁定指定文件以供调用进程独占访问。

嗯,这确实奏效了。您的测试未验证 另一个 进程是否被锁定在文件之外。

【讨论】:

  • 是的,我确实测试错了,但实际上锁是由创建这个锁的调用进程放置的。
  • 但我仍然想知道我设置了什么标志让它像这样工作
  • 代码似乎不确定文件名应该是“one.txt”、“ass23.txt”还是 argv[1]。我建议你先调试一下。
  • 感谢 HANS PASSANT 的帮助...一段时间后我发现我没有在 CreateProcess 函数的参数中提供正确的路径。
  • 换行,成功了,再次感谢大家。 LPTSTR szCmdline = _tcsdup(TEXT("C:\\detectlock\\Debug\\detectlock.exe C:\\detectlock\\Debug\\ass23.txt"));
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 2018-04-04
    • 2021-03-29
    • 1970-01-01
    • 2023-04-06
    • 2016-08-17
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多