Click here to Skip to main content
6,596,602 members and growing! (21,023 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » Tabs and Property Pages     Advanced

Property Sheet Shell Extension in C#

By Hadi A

A managed .NET user-control that implements Win32 (COM) property sheet shell extensions.
C#.NET 1.0, .NET 1.1, Win2K, Win2003VS.NET2003, Dev
Posted:22 Aug 2004
Views:93,933
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.70 Rating: 4.22 out of 5
1 vote, 7.7%
1
1 vote, 7.7%
2
1 vote, 7.7%
3
3 votes, 23.1%
4
7 votes, 53.8%
5

Introduction

According to the platform SDK: "OLE property pages enable an object to display its properties in a tabbed dialog box known as a property sheet. An end user can then view and change the object�s properties."

These property pages are used extensively in Windows. Examples are the tabs shown when viewing the properties of a file, or when viewing the properties of an object in Active Directory (or any other MMC in fact); or pages used in some Control Panel applets and wizards, etc.

By default, there is no support for creating these controls in the .NET Framework. Here, we present a user control that can be used for this purpose.

Sample Image

Background

When you select the properties of a file, Explorer looks up all the registered property pages to load for that file type in the registry, and loads them. Equally, when you view the properties of a User in Active Directory, the MMC looks at the display-specifiers, and calls all the extension pages. The technology that enables all this, is COM. Traditionally, in Visual C++, you would use ATL for the COM server and the MFC CPropertyPage control for the interface. CPropertyPage, is basically a CDialog (equivalent to a Form in .NET), that fills in a PROPSHEETPAGE structure.

Our goal was to write an Active Directory MMC extension in C#. This has many obvious benefits, but proved to be quite a tricky task. In this article, we will describe some of the main points involved, and present a user-control that wraps the tricky parts. It should be noted that our team has used this component successfully and commercially (including a similar control for Wizards), but our work is in no way complete, and improvements are highly welcome.

Using the code

The sample project will add a tab-page to the properties of .EXE file in Windows Explorer. To use the sample, just extract the program files and run INSTALL.BAT. To change the sample, you should do the following:

  1. Create an inherited user control, inheriting from SheetControl. (Sheet control is a base user control class, containing all code for handling TABs and the other messages.) Your control will be the property page that will be added. Design it according to your needs.
  2. Actual loading of the page happens in SheetLoader class. You must generate a GUID for your pages, and also create a new instance of it in IShellPropSheetExt.AddPages(), as demonstrated in the sample.
  3. Finally, build the DLL, and register it according to the place it is intended to be used. In our sample, install.bat does this.

Points of Interest

There are actually quite a few tricks here!

  1. First of all, we have a COM server which inherits and exports the needed IShellPropSheetExt, IShellExtInit, and IDataObject interfaces. If you look up these COM interfaces, you will notice that marshaling has been used extensively in the definitions.
  2. After our COM is built and registered, when Win32 needs to show our property page, it will call IShellPropSheetExt.AddPages(). This is were we initialize our user-control, and ...
  3. We use a plain resource file (i.e., an empty Win32 dialog window) as a "placeholder" for our .NET Framework classes placed controls (i.e., the user control). This is how:
    1. The PROPSHEETPAGE and DLGTEMPLATE structures are filled minimally. (Again, marshaling is extensively used to keep the code safe.)
    2. Using SetParent() API call, the hWND handle to the empty dialog is set as the parent of our user-control.
    3. We can design this user-control as needed. The reason we create and pass our form this way is that Win32 expects a dialog, and Windows Forms aren't dialogs.
  4. Setting the parent of a form to any unmanaged container except IE is officially unsupported! This causes some form events to break. For example, tab stops work almost randomly, and focus events don't get called. (I have tried adding another layer in between, but that didn't work either.) Any workaround or documentation that explains what is happening is appreciated.
  5. Our user-control, contains its own message loop to handle WM_NOTIFY correctly.
  6. Our user-control, SheetControl, encapsulates all needed logic. You should simply inherit, and visually design your page as usual.
  7. To have Win32 show the pages, a passed pointer function (lpfnAddPage) must be called. Unfortunately, a C++ pointer function can't be marshaled as C# delegates - it will cause a runtime error. This means we can't call lpfnAddPage in managed code! A very small C++ wrapper does this for us.
  8. We are almost finished, except that as soon as AddPages() finishes, our form will be deleted by the Garbage Collector! This is because there are no active references to it. .KeepAlive() fixes this by fooling the garbage collector.
  9. Extending the wizards has similar concepts involved. You should be able to write the code for that yourself. :-)

History

Our source is based on a Microsoft sample presented in PDC 2001, which was later removed from GOTDOTNET.

Dotware Technologies is an independent software vendor based in Iran.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hadi A


Member

Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Tabs & Property Pages articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 49 (Total in Forum: 49) (Refresh)FirstPrevNext
GeneralNo need for dwWrapper.dll PinmemberEric Garnier7:03 7 Oct '09  
Generalthis code is not working on 64 bit windows XP. Pinmembershreekruth17:35 27 Nov '08  
GeneralExample dont work PinmemberMember 432977415:50 5 Jun '08  
QuestionHow to access AD objects ? Pinmembermudshark20033:34 5 Oct '07  
QuestionShow AD-attributes on property sheet Pinmembermanulei4:41 5 Jun '07  
QuestionRe: Show AD-attributes on property sheet Pinmembersmyers5:45 18 Mar '08  
GeneralHow to find a name of a file? PinmemberVglaz1233:04 10 Mar '07  
GeneralRe: How to find a name of a file? PinmemberVglaz12310:41 10 Mar '07  
GeneralRe: How to find a name of a file? Pinmemberkesfaw16:15 28 Oct '08  
Questionshow property sheets from C# application Pinmemberkillers0715:11 21 Dec '06  
GeneralDo not write in-process shell extensions in managed code PinmemberSteveKing3:17 20 Dec '06  
GeneralRe: Do not write in-process shell extensions in managed code PinmemberRay Cassick20:55 15 Sep '07  
GeneralCurrent Object. PinmemberMichael Sync5:49 25 Sep '06  
GeneralOne Issue. PinmemberMichael Sync0:41 25 Sep '06  
GeneralRe: One Issue. PinmemberMichael Sync1:02 25 Sep '06  
GeneralAbout this project PinmemberHadi A14:05 13 Feb '06  
QuestionHow to add(register) as a Extended property sheet in C# & MMC 2.0? PinmemberAlpha Davis9:38 2 Feb '06  
GeneralAdding To AD User Properties Page Pinmemberjcairns12:01 1 Feb '06  
GeneralRe: Adding To AD User Properties Page PinmemberMichael Sync2:04 25 Sep '06  
GeneralHunt the filename Pinmembermat1t0:31 6 Dec '05  
GeneralRe: Hunt the filename Pinmemberformat133710:15 14 Jul '06  
GeneralRe: Hunt the filename Pinmembertomas_ruzicka0:04 12 Sep '06  
GeneralCOM registration problems Pinmembergxdata19:35 23 Jul '05  
GeneralADIComponent Code Pinmemberwkc0734066:48 9 Jun '05  
GeneralRe: ADIComponent Code Pinmemberwkc07340614:02 21 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2004
Editor: Smitha Vijayan
Copyright 2004 by Hadi A
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project