Home Company Development Blog Lightweight RPC for calling a driver from user mode
Lightweight RPC for calling a driver from user mode PDF Print E-mail
Wednesday, 29 June 2011 14:48

We describe a lightweight C++ RPC implementation in this article. The RPC is intended to fulfill the pretty specific purpose to call driver functions from user mode code. It’s really lightweight and it doesn’t have any advanced features, which many general-purpose RPC implementations have. However, the RPC library (which we intricately named RpcLib) turned out to be very helpful.

 

 

Written by:

Andrey Bisikalo, Intern Developer of Driver Development Team
Volodymyr Shamray, Development Coordinator of Driver Development Team

  1. Introduction
    1. Why RPC?
    2. Why C++ in driver?
  2. RPC library: client part
    1. Transport
  3. RPC library: server part
    1. RPC objects
    2. RPC server
  4. RPC library: serialization
  5. RPC library: exception processing
  6. RPC library: executing calls
  7. Code Sample
  8. Conclusion

Introduction

This is quite a common task to control a kernel-mode driver from a user-mode application or service. But anyone who wrote drivers knows that it’s impossible to simply execute some piece of driver code directly from user mode. To make  sending control commands to a driver possible, the operating system provides a mechanism of IO control codes (IOCTL’s). The library described in this article uses IOCTL as underlying transport.

Why RPC?

“Aren’t you, guys, complicating things ?” - the reader may ask, - “Why DeviceIoControl isn’t good enough for you?”. At first look, RPC really may seem an overcomplicated approach. But you can have certain difficulties using naked DeviceIoControl and IOCTL’s.

The conventional way to send control commands to a driver is to declare an IOCTL for each command. You can look over the Driver to Hide Processes and Files article by Ivan Romanenko and Sergey Popenko or the How to develop a virtual disk for Windows article by Gena Mairanichenko. These articles are the examples of this approach.

The problem is that a real-world driver can have dozens of control codes. It means that the dispatch routine that processes the IOCTLs can be so long that you can get lost there.

Another thing is data transfer. If you need to send some complex data between a driver and an application, packing it into a buffer and unpacking it back can be a kind of difficult and annoying task. RPC has a serialization mechanism that does the dirty work for you.

So, the RPC actually makes complicated things a bit less complicated.

Why C++ in driver?

RpcLib was designed to be used in the C++ code and it heavily relies on the C++ features such as templates and exceptions. It currently doesn’t support the drivers written in plain C (because we’ve never actually needed it). Using C++ in the kernel-mode drivers has its pros and cons, but they are not the subject of this article.

To make it possible to write the kernel-mode drivers in C++ we used the modified versions of CppLib and STLPort.

The complete article text is available only for the registered users. Please Log In or Register.