summaryrefslogtreecommitdiff
path: root/lib/ShadowCopy/LockFile.cpp
blob: 701b84ecf70a1dd4712c66f6f8aaaae8a37b1375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) ZenJu (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************


#include <string>
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include "windows.h"

int wmain(int argc, wchar_t* argv[])
{
    if (argc <= 1)
    {
        std::wcout << "Please enter the filename to be locked as %1 parameter!" << "\n\n";
        system("pause");
        return -1;
    }
    std::wstring filename = argv[1];

    HANDLE hFile = ::CreateFile(filename.c_str(),
                                GENERIC_READ,
                                0, //obtain *exclusive* lock on test file
                                nullptr,
                                OPEN_EXISTING,
                                FILE_FLAG_BACKUP_SEMANTICS,
                                nullptr);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        std::wcout << "Error obtaining exclusive lock on test file: " << filename <<  "\n\n";
        system("pause");
        return -1;
    }

    std::wcout << "File " << filename << " is locked! Press a key to unlock." << "\n\n";
    system("pause");

    ::CloseHandle(hFile);
    return 0;
}

bgstack15