Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15628

Correct use CreateDirectory API

$
0
0
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

Viewing all articles
Browse latest Browse all 15628

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>