By Josh Orenberg


2018-07-11 21:12:03 8 Comments

Hi I am writing a C++ program using the Qt framework and the Win API. I am trying to lock a symbolic link that I create in my program to prevent a user from renaming/deleting it. I use this symbolic link to programmatically traverse a volume shadow copy service snapshot, however this situation could be useful for any symbolic link that needs to be locked.

In my code, I create the symbolic link with the Win API CreateSymbolicLink function, get the HANDLE for it using the Win API CreateFileA function, and lock it using the Win API LockFileEx function. However when I run the program CreateFileA fails with the error CreateFileA failed with error 5: Access is denied. I am running the program with administrative privileges. Here's the code I use edited for simplicity with the win32Error function on bottom:

#include <Windows.h>
#include <strsafe.h>
#include <fileapi.h>
#include <WinBase.h>

void lockLink()
{
    if (CreateSymbolicLinkA("C:\\BBVSSVolC", "C:\\Users\\Josh\\Desktop\\awef\\awef3", SYMBOLIC_LINK_FLAG_DIRECTORY) == 0)
        win32Error(TEXT("CreateSymbolicLink"));
    HANDLE hFile = CreateFileA("C:\\BBVSSVolC", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, 0);
    if (hFile == INVALID_HANDLE_VALUE)
        win32Error(TEXT("CreateFileA")); //shows failure with "CreateFileA failed with error 5: Access is denied."
    OVERLAPPED overlapped;
    memset(&overlapped, 0, sizeof(overlapped));
    if (!LockFileEx(hFile,LOCKFILE_EXCLUSIVE_LOCK,0,1000000,0,&overlapped))
        win32Error(TEXT("LockFileEx"));
}

void win32Error(LPCTSTR lpszFunction)
{
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dw,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) &lpMsgBuf,
                0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                                      (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
    StringCchPrintf((LPTSTR)lpDisplayBuf,
                    LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                    TEXT("%s failed with error %d: %s"),
                    lpszFunction, dw, lpMsgBuf);
    qDebug() << QString::fromWCharArray((LPCTSTR)lpDisplayBuf);
    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}

0 comments

Related Questions

Sponsored Content

27 Answered Questions

2 Answered Questions

[SOLVED] Create Symbolic Link on Windows using C++

  • 2015-03-26 13:11:34
  • scap3y
  • 2179 View
  • 1 Score
  • 2 Answer
  • Tags:   winapi symlink

1 Answered Questions

[SOLVED] Unresolved external symbol with ICU

10 Answered Questions

[SOLVED] Why does the order in which libraries are linked sometimes cause errors in GCC?

  • 2008-09-05 02:24:19
  • Landon
  • 142243 View
  • 374 Score
  • 10 Answer
  • Tags:   c++ gcc linker

10 Answered Questions

[SOLVED] How do I list the symbols in a .so file

0 Answered Questions

Linking error when compliling legacy Oracle OCI code

  • 2016-02-10 22:29:44
  • Imperator
  • 20 View
  • 0 Score
  • 0 Answer
  • Tags:   c++ oracle api oci

1 Answered Questions

[SOLVED] SetWindowDisplayAffinity fails with error "Access denied"

  • 2015-06-06 11:27:00
  • Vishnu
  • 206 View
  • 0 Score
  • 1 Answer
  • Tags:   java winapi jna

2 Answered Questions

[SOLVED] GetModuleFileNameEx, Access Denied Error

  • 2015-02-14 07:39:38
  • Pedrumj
  • 701 View
  • 0 Score
  • 2 Answer
  • Tags:   c++ winapi

2 Answered Questions

2 Answered Questions

Sponsored Content