Use Windows Shell extension to protect folders

Source: Internet
Author: User

In the Win32 operating system (including Win9x, Windows NT, and Windows 2000), Microsoft not only has a convenient graphical user (GUI) interface, but also retains powerful scalability for the Windows user interface. For the operating environment on the Windows interface (Shell), Microsoft provides a function called shell extensions to implement the programmability of file system operations. If your machine has Versions later than Word 7.0 installed, right-click a DOC file and select "attribute" in the pop-up menu, the property page displays not only the file size, creation date, and other information, but also the summary and statistics of the doc document. For example, after WinZip 6.0 or later is installed, after selecting one or more files or folders, right-click the folder and add a zip file compression option, such as "add to zip", to the context menu. The above functions are implemented through Windows Shell extension.
Windows Shell extension is implemented in this way. First, write a shell extension program. A shell Extension Program is based on the COM (Component Object Model) component model. The shell accesses objects through interfaces. The shell extension is designed as a 32-bit server program in the process and serves the operating system in the form of a dynamic link library.
After the shell extensions are written, they must be registered to take effect. All shell extensions must be registered under the hkey_classes_root/CLSID key in the Windows registry. Under this key you can find many keys named like {ACDE002F-0000-0000-C000-000000000046}, which are globally unique Class Identifiers. Each shell extension must have a globally unique Class Identifier. Windows uses this unique Class Identifier to find the shell extension handler. The inprocserver32 sub-key under the Class Identifier records the location of the shell extension dynamic link library in the system.

Windows supports the following 7 types of shell extension:
(1) Context Menu handlers adds context menus to specific file objects;
(2) drag-and-drop handlers is used to support Ole data transmission when users perform drag-and-drop operations on certain types of file objects;
(3) icon handlers is used to provide a unique icon to a file object. You can also specify an icon for A Class of file objects;
(4) property sheet handlers adds an attribute page to a file object. The property page can be shared by the same class of file objects, or a specific property page can be specified for a file object;
(5) copy-hook handlers will be called by the system when the folder object or printer object is copied, moved, deleted, and renamed. By adding copy-hook handlers for windows, some operations can be allowed or prohibited;
(6) drop target handlers is called by the system when an object is dragged to another object;
(7) Data Object handlers will be called by the system when the file is dragged, copied, or pasted.
The folder protection function described in this article is implemented through the above 5th class, both copy-hook handlers. A program that supports copy-hook handlers must be registered under hkey_classes_root/CLSID in the registry, you also need to register the server program class under hkey_classes_root/directory/shellex/copyhookhandlers.
Because the Windows Shell server program is based on the COM component model, compiling the shell program is to construct a COM Object. Because the versions above Delphi4.0 support Windows Shell extension and COM component model, therefore, you can use Delphi to write shell extensions.
You need to implement the icopyhook interface when writing copy-hook handle using Delphi. Icopyhook is a very simple interface, and only the copycallback method must be implemented. The copycallback method of icopyhook is defined as follows:
Uint copycallback (
Hwnd, file: // handle of the parent window for displaying UI objects
Uint wfunc, file: // operation to perform.
Uint wflags, file: // flags that controls the operation
Lpcstr pszsrcfile, file: // pointer to the source file
DWORD dwsrcattribs, file: // source file attributes
Lpcstr pszdestfile, file: // pointer to the destination file
DWORD dwdestattribs file: // destination file attributes
);
The hwnd parameter is a window handle, and the copy-hook handle serves as the parent window. The wfunc parameter specifies the operation to be executed. The value is one of the values listed in the following table:
Constant Value Meaning
Fo_copy $2 copy the file specified by pszsrcfile to the position specified by pszdestfile.
Fo_delete $3 delete the file specified by pszsrcfile.
Fo_move $1 move the file specified by pszsrcfile to the position specified by pszdestfile.
Fo_rename $4 rename the file specified by pszsrcfile to the file name specified by pszdestfile.
Po_delete $13 delete the printer specified by pszsrcfile.
Po_portchange $20 change the printer port. Pszsrcfile and pszdestfile are two strings ending with null, respectively specifying the current and new printer port names.
Po_rename $14 rename the printer port specified by pszsrcfile.
Po_ren_port $34 combination of po_rename and po_portchange.

The wflags parameter specifies the operation flag. The pszsrcfile and pszdestfile Parameters specify the source and target folders. The dwsrcattribs and dwdesattribs Parameters specify the attributes of the source and target folders. The return values can be idyes, IDNO, and idcancel. Indicates that the Windows Shell allows and blocks operations, but other operations continue and stop the current operation, cancel the operation as executed.
The specific program implementation is as follows:
First, select the file | new option in the menu of Delphi, select the DLL icon, press OK to create a DLL project file, and add the following code:
Library copyhook;

Uses
Comserv,
Copymain in 'copymain. pa ';

Exports
Dllgetclassobject,
Dllcanunloadnow,
Dllregisterserver,
Dllunregisterserver;

{$ R *. TLB}

{$ R *. Res}

Begin
End.
Save the file as copyhook. DPR. Select the file | new option in the Delphi menu, select the unit icon, and press OK to create a PAS file. Add the following code to it:
Unit copymain;

Interface

Uses Windows, comobj, shlobj;

Type
Tcopyhook = Class (tcomobject, icopyhook)
Protected
Function copycallback (WND: hwnd; wfunc, wflags: uint; pszsrcfile: pansichar;
Dwsrcattribs: DWORD; pszdestfile: pansichar; dwdestattribs: DWORD): uint; stdcall;
End;

Tcopyhookfactory = Class (tcomobjectfactory)
Protected
Function getprogid: string; override;
Procedure approveshellextension (register: Boolean; const CLSID: string );
Virtual;
Public
Procedure updateregistry (register: Boolean); override;
End;

Implementation

Uses comserv, sysutils, registry;

{Tcopyhook}

File: // when the Windows shell program performs folder or printer port operations, copycallback
File: // The method is called.
Function tcopyhook. copycallback (WND: hwnd; wfunc, wflags: uint;
Pszsrcfile: pansichar; dwsrcattribs: DWORD; pszdestfile: pansichar;
Dwdestattribs: DWORD): uint;
Const
Fo_copy = 2;
Fo_delete = 3;
Fo_move = 1;
Fo_rename = 4;
VaR
Sop: string;
Begin
Case wfunc
Fo_copy: sop: = format ('Are you sure you want to copy % s to % s? ', [Pszsrcfile, pszdestfile]);
Fo_delete: sop: = format ('Are you sure you want to delete % s? ', [Pszsrcfile]);
Fo_move: sop: = format ('Are you sure you want to transfer % s to % s? ', [Pszsrcfile, pszdestfile]);
Fo_rename: sop: = format ('Are you sure you want to rename % s to % s? ', [Pszsrcfile, pszdestfile]);
Else
Sop: = format ('unrecognized operation code % d', [wflags]);
End;
// Prompt, asking the user to decide whether to perform the operation
Result: = MessageBox (WND, pchar (SOP ),
'File hook demo', mb_yesnocancel );
End;

{Tcopyhookfactory}

Function tcopyhookfactory. getprogid: string;
Begin
Result: = ';
End;

Procedure tcopyhookfactory. updateregistry (register: Boolean );
VaR
CLSID: string;
Begin
CLSID: = guidtostring (classid );
Inherited updateregistry (Register );
Approveshellextension (register, CLSID );
If register then
File: // Add the CLSID to copyhookhandlers of the Registry.
Createregkey ('Directory/shellex/copyhookhandlers/'+ classname ,',
CLSID)
Else
Deleteregkey ('Directory/shellex/copyhookhandlers/'+ classname );
End;

Procedure tcopyhookfactory. approveshellextension (register: Boolean;
Const CLSID: string );
Const
Sapprovekey = 'Software/Microsoft/Windows/CurrentVersion/Shell extensions/approved ';
Begin
With Tregistry. Create do
Try
Rootkey: = HKEY_LOCAL_MACHINE;
If not openkey (sapprovekey, true) Then exit;
If register then writestring (CLSID, description)
Else deletevalue (CLSID );
Finally
Free;
End;
End;

Const
Clsid_copyhook: tguid = '{66cd5f60-a044-11d0-a9bf-00a024e3867f }';
Libid_copyhook: tguid = '{D2F531A0-0861-11D2-AE5C-74640BC10000 }';

Initialization
Tcopyhookfactory. Create (comserver, tcopyhook, clsid_copyhook,
'Cr _ copyhook ', 'file operation hook demo', cimultiinstance, tmapartment );
End.
Register by entering the Windows system subdirectory in the DOS window and then entering regsvr32 X:/XXX/copyhook. DLL, where X:/XXX/is the compiled copyhook. the full path name of the DLL. You can also select register ActiveX Server from the run menu for registration.
After the file is successfully registered, you can change the name of a folder or move a directory in Windows explore. A prompt box is displayed, prompting you whether to perform the operation. :

 

Press yes to perform folder operations. Press No or cancel to cancel the corresponding folder operations.
The above section only describes how to implement Windows Shell extension using Delphi. You can compile a very professional Windows Shell extension program by referring to the above program and Delphi programming on the Windows COM component model.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.