Shell
From TeamDeveloperWiki
This page covers Windows shell tips & tricks.
Contents |
How to determine if the current logged-in user is an admin
If you want to determine if the current logged-in user (read Windows user) has admin rights, use the function IsUserAnAdmin.
An admin has more authorisations compared to regular users. Some system settings could only be accessible by admins or they are allowed to install software, to register COM libraries or create new network drives.
When your application detects the user is an admin, it could unlock more powerfull features compared to normal users.
For instance, if you want to display more technical error messages or debugging info while running applications, depending
on which kind of user has logged on.
First declare this in external functions section
Library name: SHELL32.DLL
Function: IsUserAnAdmin
Export Ordinal: 0
Returns
Boolean: BOOL
Parameters
Usage is simple:
If IsUserAnAdmin( )
Call SalMessageBox( "The current user is an admin", "IsUserAnAdmin", MB_Ok )
Else
Call SalMessageBox( "The current user is not an admin", "IsUserAnAdmin", MB_Ok )
Here you can download a sample:
How to delete a whole directory
Neither VisFileDelete() nor SalFileOpen(..., OF_Delete ) are able to remove a directory from a given file system.
For this purpose (and for a number of others) we can use the Windows Shell Function ShFileOperation
Declare the external function:
Library name: Shell32.dll
Function: SHFileOperationA
Description:
Export Ordinal: 0
Returns
Number: INT
Parameters
structPointer
Window Handle: HWND
Number: UINT
String: LPSTR
String: LPSTR
Number: DWORD
Receive Boolean: BOOL
Number: LPVOID
String: LPSTR
We will need at least these constants:
Number: FO_DELETE = 0x0003 Number: FOF_NOCONFIRMATION = 0x0010
Now, we are able to build a method like this:
Function: RemoveCompleteDirectory
Description: Removes a complete Directory with all
subdirectories and files.
No user confirmation required!
Returns
Boolean:
Parameters
String: p_sDirectory
Static Variables
Local variables
Boolean: l_bCancelled
Number: l_nRet
Actions
Set l_nRet = SHFileOperationA( hWndForm,
FO_DELETE,
p_sDirectory,
STRING_Null,
FOF_NOCONFIRMATION,
l_bCancelled,
NUMBER_Null,
"Deleting Directory..." )
!
Return l_bCancelled = FALSE AND l_nRet = 0
Have a look at ShFileOperation in MSDN for more sophisticated usages and options.

