Article

From:
To:
Ronald Hoek
Subject:
Re: On demand elevation in Delphi applicatio to create folder in'Program Files' folder
Newsgroup:
embarcadero.public.delphi.com.activex.using

Re: On demand elevation in Delphi applicatio to create folder in'Program Files' folder

Found the solution:

{code} procedure TestFolderCraeation(sDir: string); var   lFileOp: IFileOperation;   siFolder: IShellItem; begin   // Try simple way...   if CreateDir(sDir) then   begin     Log('Dir created');     if RemoveDir(sDir) then       Log('Dir removed')     else       Log('Dir not removed!');   end else     if CheckWin32Version(6) then // Try with elevation on Vista+   begin     // Create elevated IFileOperation COM object, to allow all operations to     // be on a elevated level!
CoCreateInstanceAsAdmin(Self.Handle, CLSID_FileOperation, IFileOperation, @lFileOp); // See code earlier

    // Normal creation of IFileOp object, without elevation
    // - This wil require the user to 'elevate' the actions on a per operation
    // (aka 'PerformOperations') execution
    // lFileOp := CreateComObject(CLSID_FileOperation) as IFileOperation;


    // Forceer 'elevation'
OleCheck( lFileOp.SetOperationFlags(FOFX_SHOWELEVATIONPROMPT or FOFX_REQUIREELEVATION) );

    // Get ShellItem interface for target folder and perform 'NewItem'operation
OleCheck( SHCreateItemFromParsingName(PChar(ExtractFilePath(sDir)), nil, IShellItem, siFolder) ); OleCheck( lFileOp.NewItem(siFolder, FILE_ATTRIBUTE_DIRECTORY, PChar(ExtractFileName(sDir)), nil, nil) );
    OleCheck( lFileOp.PerformOperations );
    if DirectoryExists(sDir) then
      Log('Dir created')
    else begin
      Log('Dir not created!');
      Exit;
    end;

    // Get ShellItem interface of created folder and perform 'Delete' operation
OleCheck( SHCreateItemFromParsingName(PChar(sDir), nil, IShellItem, siFolder) );
    OleCheck( lFileOp.DeleteItem(siFolder, nil) );
    OleCheck( lFileOp.PerformOperations );
    if DirectoryExists(sDir) then
      Log('Dir not removed!')
    else
      Log('Dir removed');
  end else
    Log('Dir not created!');
end;
{code}

Thanks to all for helping me out on this!
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Mon, 13 May 2024 03:35:07 UTC
Copyright © 2009-2024
HREF Tools Corp.