Thread: CreateFile() and UAC

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    2

    CreateFile() and UAC

    Hello everyone,

    This is my first post so please go easy on me .

    I have just started diving into Win32 programming using the book Windows System Programming by Johnson M. Hart. In chapter 2 he develops three versions of a simple file-copy program. The first version uses the straightforward C standard library approach using fopen() and fwrite(). This works without a hitch.

    The second version uses the Windows API CreateFile() function. This program also works but I only in an elevated command prompt or the program returns a file-not-found error.

    I've read that Windows 7 onwards needs admin privileges to access a file's handle. Assuming this was done for security reasons, it seems like a loophole that standard C library file routines bypass this protection. Granted, this is a very simple example program which doesn't show the full benefit of the Windows API or UAC.

    Am I misunderstanding the role of UAC, or is this actually the way it is supposed to work? And if so, how do the built-in Windows file manipulation utilities get round it?
    Last edited by RayLuxuryYacht; 07-18-2020 at 07:24 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    I suppose the first question would be which files were you trying to access?

    Regular files in your "My Documents" folder shouldn't be an issue.

    Trying to mess with C:\Windows\System32 on the other hand....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    2
    Thanks for the reply! I was trying to copy a plain text file 'foo.txt' that I had created manually using Notepad. I tried to copy this file inside the Visual Studio Debug folder and needed to elevate. I then copied my executable and the text file to a folder on my desktop and tried the same copy command. Again I needed to elevate. Using the standard C library functions no elevation was required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateFile problems
    By surgeon in forum C++ Programming
    Replies: 11
    Last Post: 12-24-2015, 06:06 PM
  2. createfile()
    By mr_empty in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2007, 05:50 AM
  3. CreateFile();
    By cgod in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2004, 05:05 AM
  4. for using CreateFile()
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 06:47 AM
  5. Using CreateFile() in Win NT
    By Bazzz in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2002, 12:41 AM

Tags for this Thread