NET Framework creates shell extensions with C #

Source: Internet
Author: User
Tags command line empty guid identifier implement interface net visual studio
Create

First, the preface

. NET platform is Microsoft launched as the future of software operation and development environment, C # is Microsoft recommend in. NET platform, the preferred language for developing application software. This article will discuss the. NET environment, use the C # language to develop a Windows shell extension problem. Now the Windows family has developed to XP generation, presumably every programmer will not be unfamiliar with shell extension, where I do not want to spend too much time to introduce the principle of shell extension, this article will be an example of using C # to create a shell Extension, this process also provides a brief introduction to some of the principles of Shell Extension (see MSDN for a detailed understanding of shell extension principles).

Second, development environment

(1) , Windows2000 Professional Edition.
(2) , Visual Studio.NET Beta 2.0 or official version 1.0.

Iii. Introduction to the principle

This example implements a hook operation called ShellExecuteEx Win32 that Windows Explorer often uses for this call, such as open, edit, print, and so on shell operations. All component information that implements the shell extension is installed under the Windows registry hklm\software\microsoft\windows\currentversion\explorer\shellexecutehooks. Before Windows Explorer performs a shell operation, it finds the registered Shell extension component in the registry and instantiates it, and each shell extension component must implement at least the Ishellexecutehook interface, which provides an execute ( function, the Explorer will invoke the Execute () function through the component instance object, so the function returns to S_FALSE to continue the operation, such as returning S_OK to stop all subsequent operations. Based on the above principles, to implement shell extensions, this example must implement a COM component that supports the Ishellexecutehook interface.

Interface declarations

Because C # cannot complete the Ishellexecutehook interface declaration with a #include "shlguid.h" statement like C + +, it must require specific information about the interface to be declared in the program, declared as follows:

[Comimpor,interfacetype (Cominterfacetype.interfaceisiunknown), Guid ("000214fb-0000-0000-c000-000000000046")]
The/* GUID ("000214fb-0000-0000-c000-000000000046") is equivalent to the define_shlguid in Shlguid.h (iid_ ISHELLEXECUTEHOOKW, 0X000214FBL, 0, 0); */
public interface ishellexecutehook{
[PreserveSig ()]/* Allow the return value to be COM HRESULT */
int Execute (Shellexecuteinfo sei);
}

Structure Declaration

The


[structlayout (layoutkind.sequential)]
public class Shellexecuteinfo {
public int cbsize;
public int fmask;
Public int hwnd;
[MarshalAs (UNMANAGEDTYPE.LPWSTR)]
public string lpverb;/* edit,open,print ... */
[MarshalAs (UNMANAGEDTYPE.LPWSTR)]
public string lpfile; * Depending on the value of Lpverb , often the filename */
[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 Lpclass;
public int hkeyclass;
public int dwhotkey;
public int hicon;
public int hprocess;
}

The elements of the SHELLEXECUTEINFO structure are not enough, and their specifics are not covered, and you can look at MSDN if you are free.

Iv. implementation Steps

After introducing the declaration of the Isellexecutehook Interface and the declaration of the SHELLEXECUTEINFO structure, we proceeded to implement the application instance, which is very simple, and a dialog box pops up whenever the Explorer performs an action on a Shell object. The action content, object name, and parameter contents are displayed on it.

Open Vs.net and follow these steps:

1. Create a new Empty project (project name: Extenshell).

2. Add a new Class (class name: ExtenShell.cs).
  
3. Use the following code as the content of the ExtenShell.cs.

  /* ExtenShell.cs */
   using System
   using System.Reflection;
   using System.Runtime.InteropServices;
   using System.Windows.Forms;

   [Assembly:assemblykeyfile (@). \.. \eskey.snk ")]/* key file */
   namespace Shellextension
   {
    /interface declaration.
        /* GUID ("000214fb-0000-0000-c000-000000000046")      public interface Ishellexecutehook
     {
          [PreserveSig ()]/* Allows the return value to be com HRESULT */
          int Execute (Shellexecuteinfo sei);
    }

     //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 (" 027F 9368-a83e-42cc-85b2-1dc5e 23C 4608"), ComVisible (true)]
/* create a new GUID with the GUID generation tool as the GUID identifier for the class object. */
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.tostring ());
              }

               return S _false;
              //

4.

    5.
 
    6. compilation complete.

    7.  

8. Register components. Run on the command line: regasm {Project path}\bin\debug\extenshell.dll. (Regasm.exe can be found under c:\Winnt\Microsoft.NET\Framework\v1.0.2914 )
 
9. Finally, under Hklm\software\microsoft\windows\currentversion\explorer\shellexecutehooks, create a new string value named { 027F 9368-a83e-42cc-85b2-1dc5e 23C 4608}, the value can be empty or a string of descriptive text can be added.

V. End

This is an example of a simple shell extension, though not a complete application, but the author would like to introduce to the reader the development of the COM component under shell extension and. NET platform, hoping that it can play a role.



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.