|
分類:[C/C++]
LockFileExを使ってファイルをロックしようとしたのですがロックできません。 どこが問題なのでしょうか? よろしくお願いします。
開発環境:visual studio 2019
int main(int argc, char* argv[]) { BOOL fSuccess = FALSE;
HANDLE hFile = CreateFile(TEXT("datafile.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed (%d)\n", GetLastError()); return (1); } DWORD dwNumBytesWritten = 0;
for (int i = 0; i < NUMWRITES; i++) { fSuccess = WriteFile(hFile, TestData[i], TESTSTRLEN-1, &dwNumBytesWritten, NULL); if (!fSuccess) { printf("WriteFile failed (%d)\n", GetLastError()); return (2); } }
FlushFileBuffers(hFile);
OVERLAPPED sOverlapped; sOverlapped.Offset = TESTSTRLEN * 3; sOverlapped.OffsetHigh = 0;
fSuccess = LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, TESTSTRLEN, 0, &sOverlapped); if (!fSuccess) { printf("LockFileEx failed (%d)\n", GetLastError()); return (3); } else printf("LockFileEx succeeded\n");
return (0); }
|