Results 1 to 5 of 5

Thread: Correct use CreateDirectory API

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2020
    Posts
    23

    Correct use CreateDirectory API

    Good day!

    I have a problem using CreateDirectory API.


    I have the code:
    Code:
    Option explicit
    
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    
    Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
    
    Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    Private Const LANG_NEUTRAL = &H0
    
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
    
    Public Function FS_PathExist(ByVal Location As String) As Boolean
        FS_PathExist = CBool(PathFileExists(Location))
    End Function
    
    Private Function FS_CreateFolder(ByVal Location As String) As Boolean
        Dim Security As SECURITY_ATTRIBUTES
        Dim RetVal As Long
        Dim Buffer As String
    
        If FS_PathExist(Location) = False Then
            RetVal = CreateDirectory(Location, Security)
    
            If RetVal = 0 Then
                Buffer = Space(200)
                FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
    'temporary MsgBox          
    MsgBox Buffer
                FS_CreateFolder = False
            End If
        Else
            FS_CreateFolder = True
        End If
    End Function

    A problem with FS_CreateFolder.

    If Exe is run from some non-system directory (aka 'Program files'), the directories I need are created. But if run from system catalogs, my catalogs are not created. (Directories should be created in the program startup directory. I use the homemade function 'GetPathByPID', everything is fine here.)

    For correct work in the system catalogs, I think, you need to correctly fill in the SECURITY_ATTRIBUTES structure and, in particular, the lpSecurityDescriptor structure.


    GetLastError always returns 1 - Operation completed successfully

    Does anyone have a similar code? Thanks!


    links:
    SA - https://docs.microsoft.com/en-us/pre...79560(v=vs.85)
    SD - https://docs.microsoft.com/ru-ru/win...ectedfrom=MSDN
    Last edited by Visualman; Oct 23rd, 2020 at 12:49 AM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jul 2020
    Posts
    23

    Re: Correct use CreateDirectory API

    I forgot. If the program is run from system directories on behalf of the Administrator, then all the subdirectories I need are created. Therefore, I think about the correct filling of the SA structure.

    Also, if a run exe from Locked SD card, GetLastError (after CreateDirectory), returns 1 - Operation completed successfully. Why?
    Last edited by Visualman; Oct 23rd, 2020 at 01:50 AM.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

    Re: Correct use CreateDirectory API

    It has nothing to do with SecurityAttributes, but with Access-Rights the User, who's running the Program, has.

    It's the age old problem with write-access to "c:\Program Files" and similiar folders

    On a sidenote: Nowadays, any program shouldn't have to create any folders/place any local files inside its own root-folder (a.k.a "c\Program Files (x86)\MyProgram\MyFolder")
    If you need a machine-wide Folder, use "c:\ProgramData\MyProgram"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2020
    Posts
    23

    Re: Correct use CreateDirectory API

    No. This option does not suit me at all. This is a portable program (admin part of a large complex). And it should create directories next to the exe.

    Regardless of where it was launched from

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Correct use CreateDirectory API

    Quote Originally Posted by Visualman View Post
    No. This option does not suit me at all. This is a portable program (admin part of a large complex). And it should create directories next to the exe.

    Regardless of where it was launched from
    If the user can copy the portable exe to C:\Windows\System32 then his security context must allow him to create directories there so you application will be able to as well.

    If the CreateDirectory is failing in the app obviously the user starting the portable application is not the one that copied it to its current location as this file-copy operation would fail as well.

    The application can detect CreateDirectory failure and request enough rights granted by the current end user by attempting to run a copy of itself elevated.

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width