WDK Mini Filter Example
user/utility.c
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) 2011 Microsoft Corporation
4 
5 Module Name:
6 
7  utility.c
8 
9 Abstract:
10 
11  The commonly used routine by the user program.
12  It will display the HRESULT in a formated message.
13 
14 Environment:
15 
16  User mode
17 
18 --*/
19 
20 #include <windows.h>
21 #include <Strsafe.h>
22 #include <stdio.h>
23 #include "utility.h"
24 
25 VOID
27  _In_ DWORD Code
28  )
29 
30 /*++
31 
32 Routine Description:
33 
34  This routine will display an error message based off of the Win32 error
35  code that is passed in. This allows the user to see an understandable
36  error message instead of just the code.
37 
38 Arguments:
39 
40  Code - The error code to be translated.
41 
42 Return Value:
43 
44  None.
45 
46 --*/
47 
48 {
49  _Null_terminated_ WCHAR buffer[MAX_PATH] = { 0 };
50  DWORD count;
51  HMODULE module = NULL;
52  HRESULT status;
53 
54  count = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
55  NULL,
56  Code,
57  0,
58  buffer,
59  sizeof(buffer) / sizeof(WCHAR),
60  NULL);
61 
62 
63  if (count == 0) {
64 
65  count = GetSystemDirectory( buffer,
66  sizeof(buffer) / sizeof( WCHAR ) );
67 
68  if (count==0 || count > sizeof(buffer) / sizeof( WCHAR )) {
69 
70  //
71  // In practice we expect buffer to be large enough to hold the
72  // system directory path.
73  //
74 
75  printf(" Could not translate error: %u\n", Code);
76  return;
77  }
78 
79 
80  status = StringCchCat( buffer,
81  sizeof(buffer) / sizeof( WCHAR ),
82  L"\\fltlib.dll" );
83 
84  if (status != S_OK) {
85 
86  printf(" Could not translate error: %u\n", Code);
87  return;
88  }
89 
90  module = LoadLibraryExW( buffer, NULL, LOAD_LIBRARY_AS_DATAFILE );
91 
92  //
93  // Translate the Win32 error code into a useful message.
94  //
95 
96  count = FormatMessage (FORMAT_MESSAGE_FROM_HMODULE,
97  module,
98  Code,
99  0,
100  buffer,
101  sizeof(buffer) / sizeof(WCHAR),
102  NULL);
103 
104  if (module != NULL) {
105 
106  FreeLibrary( module );
107  }
108 
109  //
110  // If we still couldn't resolve the message, generate a string
111  //
112 
113  if (count == 0) {
114 
115  printf(" Could not translate error: %u\n", Code);
116  return;
117  }
118  }
119 
120  //
121  // Display the translated error.
122  //
123 
124  printf(" %ws\n", buffer);
125 }
126 
NcLoadRegistryStringRetry NULL
Definition: ncinit.c:53
VOID DisplayError(_In_ DWORD Code)
Definition: user/utility.c:26

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