-
Oct 23rd, 2020, 12:46 AM
#1
Thread Starter
Junior Member
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.
-
Oct 23rd, 2020, 12:51 AM
#2
Thread Starter
Junior Member
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.
-
Oct 23rd, 2020, 12:52 AM
#3
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
-
Oct 23rd, 2020, 12:58 AM
#4
Thread Starter
Junior Member
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
-
Oct 23rd, 2020, 02:58 AM
#5
Re: Correct use CreateDirectory API
Originally Posted by Visualman
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|