EaseFilter Demo Project
Tools.cpp
Go to the documentation of this file.
1 //
3 // (C) Copyright 2011 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 #include "stdafx.h"
12 #include "Tools.h"
13 
14 #define MAX_ERROR_MESSAGE_SIZE 1024
15 
16 using namespace std;
17 
18 void ChangeColour(WORD theColour)
19 {
20  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get handle to standard output
21  SetConsoleTextAttribute(hConsole,theColour); // set the text attribute of the previous handle
22 }
23 
24 void
25 PrintMessage(WCHAR* message,WORD theColour)
26 {
27  ChangeColour(theColour);
28  wprintf(message);
29  ChangeColour(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
30 }
31 
32 void
33 PrintPassedMessage(WCHAR* message)
34 {
35  PrintMessage(message,FOREGROUND_GREEN);
36 }
37 
38 void
39 PrintFailedMessage(WCHAR* message)
40 {
41  PrintMessage(message,FOREGROUND_RED);
42 }
43 
44 
45 //
46 //Get the last error message returned from FilterAPI, and print it.
47 //
48 void
49 PrintLastErrorMessage(WCHAR* message)
50 {
51  ULONG messageLength = 0;
52  WCHAR* buffer = NULL;
53 
54  if( !GetLastErrorMessage(buffer,&messageLength ))
55  {
56  buffer = (WCHAR*)malloc(messageLength);
57 
58  GetLastErrorMessage(buffer,&messageLength );
59 
60  }
61 
62  PrintFailedMessage(message);
63  PrintFailedMessage(buffer);
64  printf("\n");
65 
66  free(buffer);
67 }
68 
69 //
70 //To display message in WinDbg or DbgView application.
71 //
72 void
74  const WCHAR* pszFormat,
75  ...)
76 {
77 
78  va_list arglist;
79  WCHAR pBuffer[MAX_ERROR_MESSAGE_SIZE];
80 
81  memset( pBuffer, 0,MAX_ERROR_MESSAGE_SIZE*sizeof(WCHAR) );
82 
83  va_start(arglist, pszFormat);
84  vswprintf(pBuffer,MAX_ERROR_MESSAGE_SIZE, pszFormat, arglist);
85  va_end(arglist);
86 
87  OutputDebugStringW(pBuffer);
88  OutputDebugStringW(L"\n");
89 
90 }
91 
92 void
94  LPWSTR message,
95  DWORD errorCode )
96 {
97  LPVOID lpMsgBuf = NULL;
98  WCHAR errorMessage[MAX_ERROR_MESSAGE_SIZE];
99 
100  __try
101  {
102 
103  if( errorCode != 0 )
104  {
105  FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
106  NULL, errorCode,
107  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
108  (LPTSTR) &lpMsgBuf, 0, NULL );
109 
110  swprintf_s( errorMessage, MAX_ERROR_MESSAGE_SIZE, L"%ws,errorCode:0x%0x,%ws\n", message,errorCode,lpMsgBuf);
111 
112  }
113  else
114  {
115  swprintf_s( errorMessage, MAX_ERROR_MESSAGE_SIZE, L"%ws\n", message);
116  }
117 
118  ChangeColour(FOREGROUND_RED);
119  wprintf(errorMessage);
120  OutputDebugStringW (errorMessage);
121  ChangeColour(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
122 
123  }
124  __except( EXCEPTION_EXECUTE_HANDLER )
125  {
126  OutputDebugStringW(L"PrintErrorMessage exception.");
127  }
128 
129  if( lpMsgBuf )
130  {
131  LocalFree( lpMsgBuf );
132  }
133 }
void PrintErrorMessage(LPWSTR message, DWORD errorCode)
Definition: Tools.cpp:93
void ToDebugger(const WCHAR *pszFormat,...)
Definition: Tools.cpp:73
void ChangeColour(WORD theColour)
Definition: Tools.cpp:18
void PrintLastErrorMessage(WCHAR *message)
Definition: Tools.cpp:49
void PrintPassedMessage(WCHAR *message)
Definition: Tools.cpp:33
void PrintMessage(WCHAR *message, WORD theColour)
Definition: Tools.cpp:25
void PrintFailedMessage(WCHAR *message)
Definition: Tools.cpp:39
#define MAX_ERROR_MESSAGE_SIZE
Definition: Tools.cpp:14

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