Zhou Yi (Longsoft@ah163.com, Anhui Province)
I. Introduction
The .NET platform is the environment in which Microsoft is launched as a future software operation and development, C # is the preferred language recommended by Microsoft to develop applications under the .NET platform. This article will discuss how to develop Windows Shell extensions in the .NET environment. Today, the Windows family has developed to the XP generation. If each programmer will not feel unfamiliar with Shell Extension, here I don't want to spend too much time to introduce the principle of shell Extension, this article will introduce an instance with C # Create a shell extension, which will also briefly introduce some of the principles of Shell Extension (if you want to learn more about the SHELL extension principle, see MSDN).
Second, the development environment
(1) WINDOWS2000 Professional Edition. (2) Visual Studio.NET Beta 2.0 or official version 1.0.
Third, the principle introduction
This example implements a hook operation called for a shellexecuteex Win32, Windows Explorer often uses this call, such as opening, editing, printing, etc. SHELL operations to use this call. All component information that implements the shell extension is installed under the Windows Registry HKLM / Software / Microsoft / Windows / CurrentVersion / Explorer / ShellexecuteHooks. When Windows Explorer executes shell operation, first find the registered shell extension component in the registration, and instantly, each shell extension component must implement at least the ISHELLEXECUTEHOOK interface, this interface provides an Execute () function, Explorer The execute () function will be invoked by the component instance object, so that the function returns to S_FALSE continues to continue, if returns S_OK, stop all of the following operations. According to the above principles, this example must implement the shell extension. You must implement a COM component that supports the ISHELLEXECUTEHOOK interface.
Interface declaration
Because C # cannot use a #include "shlguid.h" statement like C , you can complete the ISHELLEXECUTEHOOK interface declaration, which must be required to declare the specific information of the interface in the program, as follows:
[ComImpor, InterfaceType (ComInterfaceType.InterfaceIsIUnknown), Guid ( "000214FB-0000-0000-C000-000000000046")] / * Guid ( "000214FB-0000-0000-C000-000000000046") corresponds to the shlguid.h DEFINE_SHLGUID ( IID_ISHELLEXECUTEHOKW, 0X000214FBL, 0, 0); * / public interface ishellexecutehook {[preservesig ()] / * Allow the return value to com HRESULT * / INT EXECUTE (ShellexecuteInfo SI);}
Structural statement
There is a structure parameter SHELLEXECUTEINFO sei in Execute () method, the next step is to declare the structure: [StructLayout (LayoutKind.Sequential)] public class SHELLEXECUTEINFO {public int cbSize; public int fMask; public int hwnd; [MarshalAs (UnmanagedType. LPWSTR)] Public String Lpverb; / * Action, such as Edit, Open, Print ... * / [Marshalas (unmanagedType.lpwstr)] public string lpfile; / * is based on the value of LPVERB, often for file name * / [ MarshalAs (UnmanagedType.LPWStr)] public string lpParameters; / * parameter string * / [MarshalAs (UnmanagedType.LPWStr)] public string lpDirectory; / * path name * / public int nShow; public int hInstApp; public int lpIDList; public string Public Int dwhotkey; public int hic; public int hic;} shellexecuteInfo structure element is not enough, and their specific instructions are not only introduced, if you have time, you can take a look at MSDN .
Fourth, implement steps
After introducing the declaration of the ISELLEXECUTEHOOK interface and the statement of the shellexecuteInfo structure, we started to implement this application instance, this instance is very simple, whenever the Explorer performs an action to a shell object, a dialog will pop up, on it The action content, object name, and parameter content.
Open vs.net, follow these steps: 1. Create an empty project (project name: extenshell). 2. Add a new class (class name: extenshell.cs). 3. Put the following code as the contents of Extenshell.cs.
/ * Extenshell.cs * / using system; using system.reflection; using system.runtime.interopservices; using system.windows.form;
[assmbly: assemblykeyKeyFile (@ "../../ eskey.snk")] / * Key file * / namespace shellextension {// interface declaration. [ComImport, InterfaceType (ComInterfaceType.InterfaceIsIUnknown), Guid ( "000214FB-0000-0000-C000-000000000046")] / * Guid ( "000214FB-0000-0000-C000-000000000046") corresponds to the shlguid.h DEFINE_SHLGUID ( IID_ID_IDELLEXECUTEHOKW, 0X000214FBL, 0, 0); * / public interface ishellexecutehook {[preservesig ()] / * Allows the return value to COM HRESULT * / INT EXECUTE (SHELLEXECUTEINFO SI);} // Structure declaration. [StructLayout (LayoutKind.Sequential)] public class SHELLEXECUTEINFO {public int cbSize; public int fMask; public int hwnd; [MarshalAs (UnmanagedType.LPWStr)] public string lpVerb; [MarshalAs (UnmanagedType.LPWStr)] public string lpFile; [MarshalAs (UnmanagedType.LPWStr)] public string lpParameters; [MarshalAs (UnmanagedType.LPWStr)] public string lpDirectory; public int nShow; public int hInstApp; public int lpIDList; public string lpClass; public int hkeyClass; public int dwHotKey; public int hIcon; Public int hprocess;}
[GUID ("027F9368-A83E-42CC-85B2-1DC5E23C4608"), Comvisible (TRUE)] / * Create a new GUID as a GUID identity of the class object with the GUID Build Tool. * / Public class ExtenShell: IShellExecuteHook {private int S_OK = 0; private int S_FALSE = 1; public int Execute (SHELLEXECUTEINFO sei) {try {MessageBox.Show (null, "[Verb]:" sei.lpVerb "/ n [File]: " sei.lpfile " / n [parameters]: " sei.lpparameters " / n [directory]: sei.lpdirectory, "shelleXtensionHook", MessageBoxButtons.ok, MessageBoxicon.information;} Catch (Exception E) {Console.Error.writeline ("Unknown Exception:" E.toTString ());
Return S_FALSE; // If the return value is S_ok, the shell will stop all the actions of the shell object. }}}
4. Run on the command line: Sn -k eskey.snk (sn.exe can be found under C: / ProGrame files / microsoft.net / frameworksdk / bin), add EsKey.snk to the project. 5. Open
5. This is a simple shell exemplary example. Although it is not a complete application, the author wants to introduce the SHELL extension and the COM component development technology under the .NET platform through this example, I hope it can stand the role of the jade.