wxRenameFile - Access denied (error 5)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
beschbesch
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Oct 21, 2021 4:22 pm

wxRenameFile - Access denied (error 5)

Post by beschbesch »

When I try to rename files located in the C:\User directory, e.g. in a subfolder on the desktop (C:\User\...\Desktop\sub\file.ext), wxWidgets output Error 5 (Access denied). I know that this is due to missing access rights because it works just fine on a non-system volume in a normal directory (e.g. D:\sub\file.ext).

Does wxWidgets come with any functions that can provide me the necessary elevated access?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxRenameFile - Access denied (error 5)

Post by doublemax »

beschbesch wrote: Thu Oct 21, 2021 4:29 pm Does wxWidgets come with any functions that can provide me the necessary elevated access?
No.

Here's Win32 code to do it:
https://github.com/microsoftarchive/msd ... vation.cpp

Beware that this code does not elevate an app "in-place", it re-starts itself with elevated level. I don't know if this is a general limitation or only a limitation of the presented demo code.
Use the source, Luke!
beschbesch
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Oct 21, 2021 4:22 pm

Re: wxRenameFile - Access denied (error 5)

Post by beschbesch »

Thank you, I will take a look at it.

Yes, the windows security system is built like that. It is meant to provide a line of defense line against malware that requires human interaction - in theory at least. Malware should not be able to launch itself as admin on startup - the user always needs to initiate the elevation. I am unsure, if an elevation dialog will pop up when I use this code, or if Microsoft provides a ready-made dialog with some other API.

Lastly, I remember that MP-HC uses elevation for some of its settings.
If anyone is interested, it is the follwoing settings page ('Format'):
.
s9Gr6HU - Imgur.png
s9Gr6HU - Imgur.png (48.7 KiB) Viewed 1292 times
Image Text:
Example of user right elevation from the MP-HC Media Player: The application is launched with SECURITY_MANDATORY_MEDIUM_RID (probably) and so cannot change the file type association. Due to that, MP-HC (probably) disables the 'Formats' settings page after checking the process security token.
Elevation - probably to SECURITY_MANDATORY_HIGH_RID - is only possible once the human user presses the 'Run as administrator' button. Given the symbol, I do not know if Microsoft acutally offers a ready made Common Control, or if the devs have modified a push button to look like "the real thing".
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxRenameFile - Access denied (error 5)

Post by doublemax »

Use the source, Luke!
Post Reply