The WINDOWS housing extension protection folder is sandwiched in Win32 operating system (including Win9x, Windows NT, Windows 2000) not only has a convenient graphical user (GUI) interface, but Microsoft also retains powerful expansion for the Windows user interface. Among them, for the operating environment of the Windows interface (hereinafter referred to herein as a shell shell), Microsoft provides a function called a shell extensions to implement the programmability of file system operations. If you have a version of Word 7.0 in your machine, when your mouse right click on a Doc file, select the Properties item in the pop-up menu, not only display the size of the file, create a date, etc., but also Added information about the DOC documentation, statistics, etc., for example, after the WinZip 6.0 or more, when the one or more files or folders are selected, click the right mouse button after the one or more files or folders, add "Add to" ZIP "and other ZIP file compression options. These functions are implemented through Windows housing extensions. The Windows shell extension is achieved. First, you must write a housing extension, and a housing extension is based on a COM (Component Object Model) component model. The housing is to access objects through an interface (Interface). The outer casing extension is designed into a 32-bit process server program, and is provided in the form of a dynamic link library to serve the operating system. After writing the enclosure extension, they must register them to take effect. All housing extensions must be registered under the HKEY_CLASSES_ROOT / CLSID key of the Windows registry. In this key, you can find a number of keywords like {ACDE002F-0000-0000-C000-000000000046}, which is the global unique class identifier. Each housing extension must have a global unique class identifier, and Windows is using this unique class identifier to find the housing extension handler. The location of the housing extension dynamic link library in the system is recorded under the INPROCSERVER32 sub-key under the class identifier.
The Windows system supports the following 7 classes: (1) Context menu handlers add a context-related menu to a specific type of file object; (2) Drag-and-drop handlers is used to support the user to perform some type of file object OLE data transmission at the time of dragging and dropping; (3) icon handlers is used to provide a unique icon to a file object, or you can give an icon for a type of file object; (4) Property Sheet Handlers adds an attribute page to the file object The property page can be used for the same class file object, or give a file object to specify a unique property page; (5) Copy-Hook Handlers When the folder object or printer object is copied, mobile, delete, and rename it, Will be called by the system, can be called or disabled by adding Copy-Hook Handlers to Windows; (6) DROP TARGET HANDLERS is called when an object is dragged and dropped to another object; (7) Data Object handlers will be called by the system when the file is dragged, copy or paste. The folder protection function introduced in this article is achieved by Copy-Hook Handlers through the top 5 above. A program that supports COPY-HOOK HANDLERS needs to be registered in HKEY_CLASSES_ROOT / DIRECTORY / SHELLEX / COPYHOKHANDLERS / SHELLEX / COPYHOOKHANDLERS / SHELLEX / COPYHOKHANDLERS / SHELLEX / COPYHOOKHANDLERS. Since the Windows Shell Server program is based on the COM component model, the writing shell is to construct a COM object. Since the version of Delphi4.0 supports the Windows housing extension and COM component model, you can use Delphi to write the housing extension. Write Copy-Hook Handle with Delphi needs to implement the ICOPYHOOK interface. ICopyHook is a very simple interface that is only a CopyCallback method to be implemented. ICopyHook CopyCallBack method defined as follows: UINT CopyCallback (HWND hwnd, file: // Handle of the parent window for displaying UI objects UINT wFunc, file: // Operation to perform UINT wFlags, file:. // Flags that control 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); hwnd parameter which is A window handle, Copy-Hook Handle uses this as a parent window. Parameter wfunc Specifies the operation to be executed, with one of the following tables: Constant Value Meaning FO_COPY $ 2 Copy the file specified by pszsrcfile to the location specified by pszdestfile. FO_DELETE $ 3 Delete the file specified by pszsrcfile. FO_MOVE $ 1 Moves the file specified by pszsrcfile to the location specified by pszdestfile. FO_RENAME $ 4 Rename the file name specified by pszsrcfile to the file name specified by pszdestfile.
PO_DELETE $ 13 Deletes the printer specified by pszsrcfile. PO_PORTCHANGE $ 20 changes the printer port. Pszsrcfile and pszdestfile specify the current and new printer port names for two strings ending with NULL. PO_RENAME $ 14 Rename the printer port specified by pszsrcfile. PO_REN_PORT $ 34 PO_RENAME and PO_PORTCHANGE combination. Parameter wflags Specifies the logo of the operation; parameter pszsrcfile and pszdestfile specify the source folder and destination folder. Parameters dwsrcattribs and dwdesattribs specify the properties of the source folder and the destination folder. The function return value can be iDyes, IDNO, and IDCANCEL. Indicates that the Windows housing allows operation, block operation, but other operations continue, prevent the current operation, and cancel the operation of the execution. The following is the specific program implementation: first select the file | new option in the Delphi menu, select the DLL icon, press the OK button to create a DLL project file, add the following code: library copyhook;
Uses Comserv, Copymain in 'CopyMain.Pas';
Exports DllgetClassObject, DllcanunloadNow, DllRegisterServer, DllunregisterServer;
{$ R * .tlb}
{$ R * .res}
Save the file as CopyHook.dpr. Then select the file | new option in the Delphi menu, select the Unit icon, press the OK button to create a PAS file, add the following code: 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 Performs a folder or printer port operation, the copyCallbackFile: // method will be 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 of fo_copy: SOP: = Format ('you sure you want to copy% s to% s?', [pszsrcfile, pszdestfile]); fo_delete: SOP: = Format ('you sure you want Is it deleted with% s? ', [Pszsrcfile]); fo_move: SOP: = Format (' You have to transfer% S to% s? ', [Pszsrcfile, pszdestfile]); fo_rename: SOP: = format (' You have determined that the% s is renamed% s? ', [Pszsrcfile, pszdestfile]); Else Sop: = Format (' Unrecognized operation code% d ', [wflags]); end; // Tip, let Users decide whether to execute result: = MessageBox (WND, PCHAR (SOP), 'file hook demonstration', 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: // added to the registry clsid CreateRegKey in CopyHookHandlers / ' ClassName,' ', CLSID ELSE DELETEREGKEY (' Directory / Shellex / CopyHookHandlers / ' ClassName);
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 operations linked presentation', ciMultiInstance, tmApartment);. end Save the file as CopyMain.Pas file, and then compile the program for the CopyHook.Dll file, and then register CopyHook .Dll file, you can register with the regsvr32.exe provided by Windows, the registration method is to enter the system subdirectory of Windows in the DOS window, then enter regsvr32 x: /xxx/xxx/copyHook.dll, where x: / xxx / xxx / is the full path name of compiled CopyHOK.DLL. You can also select Register ActiveX Server to register in the Run menu. When the file is registered successfully, any change in the name of the folder or move a directory in the Windows Explore, there will be a prompt box to pop up, prompting the user to determine the execution operation. as the picture shows:
Press "Yes" to perform folder operation, press "No" or "Cancel" will cancel the corresponding folder operation. The above introduction is only one of Delphi to implement a Windows housing extension. Refer to the above program and Delphi's programming of the Windows COM component model, you can write a very specialized Windows housing extension.