EaseFilter Demo Project
EaseFltCSConsoleDemo/FilterWorker.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.Collections.Generic;
13 using System.Text;
14 using System.IO;
15 using Microsoft.Win32.SafeHandles;
16 using System.Runtime.InteropServices;
17 using System.Security.Principal;
18 using System.Threading;
19 using System.Diagnostics;
20 using System.ServiceProcess;
21 using System.Management;
22 
24 
25 
26 namespace EaseFltCSConsoleDemo
27 {
28  public class FilterWorker
29  {
30 
31  public static bool StartService()
32  {
33  //Purchase a license key with the link: http://www.easefilter.com/Order.htm
34  //Email us to request a trial key: info@FilterAPI.com //free email is not accepted.
35  string registerKey = "**************************************";
36 
37  bool ret = false;
38 
39  try
40  {
41  string lastError = string.Empty;
42 
43  EventManager.WriteMessage(37, "StartFilter", EventLevel.Information, "Starting filter service.....");
44 
45  ret = FilterAPI.StartFilter( (int)GlobalConfig.FilterConnectionThreads
46  , registerKey
47  , new FilterAPI.FilterDelegate(FilterCallback)
48  , new FilterAPI.DisconnectDelegate(DisconnectCallback)
49  , ref lastError);
50  if (!ret)
51  {
52  EventManager.WriteMessage(43, "StartFilter", EventLevel.Error, "Start filter service failed with error " + lastError);
53  return ret;
54  }
55 
57  EventManager.WriteMessage(102, "StartFilter", EventLevel.Information, "Start filter service succeeded.");
58  }
59  catch (Exception ex)
60  {
61  EventManager.WriteMessage(104, "StartFilter", EventLevel.Error, "Start filter service failed with error " + ex.Message);
62  }
63 
64  return ret;
65  }
66 
67 
68 
69  public static bool StopService()
70  {
71  FilterAPI.StopFilter();
73 
74  return true;
75  }
76 
77 
78  public static Boolean FilterCallback(IntPtr sendDataPtr, IntPtr replyDataPtr)
79  {
80  Boolean ret = true;
81 
82  try
83  {
84  FilterAPI.MessageSendData messageSend = new FilterAPI.MessageSendData();
85  messageSend = (FilterAPI.MessageSendData)Marshal.PtrToStructure(sendDataPtr, typeof(FilterAPI.MessageSendData));
86 
87  if (FilterAPI.MESSAGE_SEND_VERIFICATION_NUMBER != messageSend.VerificationNumber)
88  {
89  EventManager.WriteMessage(139, "FilterCallback", EventLevel.Error, "Received message corrupted.Please check if the MessageSendData structure is correct.");
90  return false;
91  }
92 
93  MessageInfo.DisplayFilterMessage(messageSend);
94 
95  if (replyDataPtr.ToInt64() != 0)
96  {
97 
98  FilterAPI.MessageReplyData messageReply = (FilterAPI.MessageReplyData)Marshal.PtrToStructure(replyDataPtr, typeof(FilterAPI.MessageReplyData));
99 
100  //here you can control the IO behaviour and modify the data.
101  FilterService.IOAccessControl(messageSend, ref messageReply);
102 
103  messageReply.MessageId = messageSend.MessageId;
104  messageReply.MessageType = messageSend.MessageType;
105  messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_SUCCESS;
106 
107 
108  //to comple the PRE_IO
109  //messageReply.ReturnStatus = (uint)FilterAPI.NTSTATUS.STATUS_ACCESS_DENIED;
110  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
111 
112  Marshal.StructureToPtr(messageReply, replyDataPtr, true);
113  }
114 
115 
116  return ret;
117  }
118  catch (Exception ex)
119  {
120  EventManager.WriteMessage(134, "FilterCallback", EventLevel.Error, "filter callback exception." + ex.Message);
121  return false;
122  }
123 
124  }
125 
126  static void DisconnectCallback()
127  {
128  EventManager.WriteMessage(697, "DisconnectCallback", EventLevel.Information, "Filter Disconnected." + FilterAPI.GetLastErrorMessage());
129  }
130 
131 
132  }
133 
134 }
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
static Boolean FilterCallback(IntPtr sendDataPtr, IntPtr replyDataPtr)
#define registerKey

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com