Shell

From Team Developer SqlWindows Wiki
Jump to: navigation, search

Windows Shell


Contents


Pointer2.png How to determine if the current logged-in user is an admin Pointer.png

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 authorizations 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 powerful 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:
Down.png IsUserAnAdmin.zip


Pointer2.png How to delete a whole directory Pointer.png

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.


Down.png SHFileOperation.zip


Pointer2.png AssocQueryString : get associated application for file extension Pointer.png

A file extension may or may not be associated with an application on the system.
For instance, the extension .bmp could be associated with Paint.exe and .xml has no association at all.


When opening a file from Explorer, the associated application is started and depending on the action it can open, print or edit the file.
The context menu on a file in Explorer shows the actions which are registered for that file type.


When using ShellExecute with the specific command (open, edit etc) it uses the registered association for the file type.


You might want to know from your application which application is associated with a particular file type.
This can be done using the WinApi function AssocQueryString


It can query for each command, for instance on .bmp the "edit" command will return Paint.exe but the "open" command is associated with ImageViewer.exe.


This sample shows how to implement this:
Down.png AssocQueryString.zip


Pointer2.png ShellOpenWith : use default Windows dialog for unknown file extensions Pointer.png

Using ShellExecute you can open
a file using the default application registered for the file extension.


But what to do when the file extension is not registered on the system?
When using Windows Explorer, double clicking on such a file will automatically open the "Open with" dialog.
There you can select from a list of applications to open this file or a way to browse for the application.


When you need this from your TD application, the default "Open with" dialog can be started using:

ShellExecuteA( hWndNULL, "open", "rundll32.exe", "shell32.dll,OpenAs_RunDLL " || sFile, STRING_Null, SW_SHOWNORMAL )


ShellOpenWith.png


Here you can download a sample:
Down.png WIKI_ShellOpenWith.zip


Pointer2.png ShellRunAs : run programs under admin or specific user rights Pointer.png

Using ShellExecute you can start an application which should run under admin rights or under specific user rights.


The operation determines how to start the program:

1) ShellExecuteA( hWndNULL, "open", "regedt32", STRING, STRING_Null, SW_SHOWNORMAL ) -> under current user rights
2) ShellExecuteA( hWndNULL, "runas", "regedt32", STRING, STRING_Null, SW_SHOWNORMAL ) -> under admin rights
3) ShellExecuteA( hWndNULL, "runasuser", "regedt32", STRING, STRING_Null, SW_SHOWNORMAL ) -> under specific user credentials


ShellRunAs.png


Here you can download a sample:
Down.png WIKI_ShellExecuteRunAs.zip