Click here to Skip to main content
Licence CPOL
First Posted 28 Sep 2005
Views 44,681
Bookmarked 24 times

ActiveSync File Filter in C#

By | 28 Sep 2005 | Article
This article shows how to write an ActiveSync file filter in C#.

Background

I have been working on a project which reads and displays HXS and CHM help files on a Pocket PC with .NET Compact Framework installed. An old version of this program (CHM Reader) was posted here at Code Project. On my web site, I have a more up to date version of the CHM Reader program. While working on the new version, I had an idea. Once a help file has been opened and decoded, why not save that data to a file so that all that processing does not have to be done each time the file is opened. Then, I thought it would be even better if this pre-processing takes place on the PC instead of Pocket PC because of the extra processing power of the PC. I thought it would be a good idea if pre-processing takes place while the user copies the help files from the PC to the Pocket PC – and so the search to find out how to write an Active-Sync File Filter in C# began.

Notes

  • I will not explain the working of active-sync file filter as this is documented in MSDN. (Search for Active-Sync File Filter.)
  • The code for the HelpFileFilter is only intended as a guide and will not compile as I have not included all the libraries that I use to make my file filter.
  • This code has been tested only on Active-Sync 3.7.1.

The projects

The solution files included with this project include a basic test program to test the C# code and the file filter code.

The Active-Sync File Filter project builds a DLL class library which performs all the processing on the file as it copies. This DLL basically creates a COM object that the Active-Sync manager will load and call when the defined file types are to be copied to/from the Pocket PC. Because of this, the DLL needs to be installed into the GAC when you install your application.

The interface

At the very core, the Active-Sync File Filter is the COM interface which makes it all work, it is here where I had the most trouble getting the C# interop code to match the COM definitions. The interfaces are all defined in Interfaces.cs (what a surprise!) I am not totally convinced that I have all the bugs out if it, but what I do have seems to work without any errors.

IceFileFilter is the main interface. It has three methods and the most important one is the 'NextConvertFile'.

int NextConvertFile(int nConversion, ref CFF_CONVERTINFO pci, 
              ref CFF_SOURCEFILE psf, ref CFF_DESTINATIONFILE pdf, 
              ref bool pbCancel, ref Int32 perr)
  • nConversion – This is an index value which determines the number of times this function is called for current file copy. (You can copy multiple files and class it as a single operation.)
  • pci – This is a structure which contains information about file copy. (Pocket PC to PC or PC to Pocket PC, a hWnd to the parent app, a flag bYesToAll, and pointer to IceFileFilterSite object. This object allows you to open and close files on source and target files and report progress.)
  • psf – This is a structure which contains the source file name.
  • pdf – This is a structure which contains the target file name.

When a file is to be copied, this method is called with nConvertsion =0. You can then detect this and perform any operation that you like to init your code. When I pre-process the help files it is when nConversion =0 where the help files are loaded and processed, and some extra files get created. I also create a list of files which need to be copied. I then return RESULT.NOERROR. This tells Active-Sync to continue copying the file, and the function is called again with nConversion=1. It is here I copy the files in my list from the source location to the target location.

Note: These file locations are not on the Pocket PC, but virtual locations. The file transfer gets performed from within Active-Sync code.

If all the files have been copied, I return HRESULT.E_ERROR_NO_MORE_ITEMS.

Other notes

Here are some notes on the other methods that are included in the interfaces. These methods do not get called, or if they do, they do not have any effect on the conversion process.

  • IceFileFilterOptions.SetFilterOptions - Set options for conversion but options ignored.
  • IceFileFilter.FilterOptions - Never called.
  • IceFileFilter.FormatMessage - Never called.

The final bits

There are a few bits left over:

  1. You need to declare your DLL as strongly named DLL, so you need to specify:
    [assembly: 
    AssemblyKeyFile(@"..\..\HelpFileFilter.snk")]
  2. You need to make your class COM visible:
    [ ProgId("MSHelReader.CHMPreProcessor"),
        Guid("B424CF38-10C0-46ec-A29C-8F78A5990B5F")] 
    Class CMyFileFilter : ICeFileFilter, ICeFileFilterOptions 
    {.... 
    }
  3. You need to specify COM Register and COM UnRegister functions so that you can add/remove the registry information which maps your file filter to Active-Sync.
  4. The ActiveSync test project is a small C++ project which allows you to create and play with the file filter COM objects when they are called, and to test your code.

Details of the new version of PPCHelpReader which can read both HXS and CHM help files can be found here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Peter Tewkesbury

Software Developer (Senior)

United Kingdom United Kingdom

Member

Hi, I am a developer working in Cheshire, England. I started programming on a Vic-20 back in the 80's with only 5K of ram), and I have not looked back since. I have developed software using Visual C/C++, C#/.NET, MFC, ATL & ASP.NET, SQL Server, Crystal Reports, DevExpress .NET Components, WinForms, SQL Server client side reporting.
 
My favourite language is C# on .NET V3.5 and I am always trying to learn new stuff like LINQ.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General.NET Service Provider for ActiveSync PinmemberEshva14:47 16 May '09  
GeneralRe: .NET Service Provider for ActiveSync PinmemberPeter Tewkesbury5:51 17 May '09  
GeneralRe: .NET Service Provider for ActiveSync PinmemberEshva9:00 17 May '09  
GeneralRe: .NET Service Provider for ActiveSync PinmemberPeter Tewkesbury22:59 17 May '09  
QuestionActiveSync Service Provider on C#? PinmemberWilliam Yeung21:49 22 Sep '07  
AnswerRe: ActiveSync Service Provider on C#? PinmemberPeter Tewkesbury8:49 23 Sep '07  
QuestionIceFileFilterSite Interface Pinmembercspencer7:42 24 Mar '06  
General1 out of 5 for formatting PinmemberJon Pawley4:44 29 Sep '05  
GeneralRe: 1 out of 5 for formatting PinmemberPeter Tewkesbury0:27 23 Aug '06  
GeneralPlease reformat your article Pinmemberfwsouthern17:57 28 Sep '05  
GeneralRe: Please reformat your article PinmemberPeter Tewkesbury0:24 23 Aug '06  
GeneralThanks -- there seems to be a rash of this ..... Pinmemberfwsouthern16:21 23 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 28 Sep 2005
Article Copyright 2005 by Peter Tewkesbury
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid