Saturday, November 5, 2011

Windows RPC on C++

RPC is a famous technique for IPC(Inter-process Communication).I think u all know about RPC and how its basically works.And that is why u r here reading this blog.So without further talking about RPC lets see how we can use Microsoft Remote Procedure Call (RPC) can be used to develop application.

Microsoft Remote Procedure Call (RPC) is a integral service of most windows operating systems(Have tested on Windows XP SP2 and later versions). If u can see the windows services there two process called RpcSs and RpcLocater is running. Those are the two RPC processes that is ships with windows. If there are not there and RpcSsis not running there is a problem with ur RPC service.

What do u needed to run RPC program on ur computer?

None.It all comes with windows

What do u need to develop a application using RPC?

C/C++ compiler for windows(I used the VC90 that ships with Visual Studio 2008)
Microsoft Windows SDK.
-Version 6.0A is Ships with Visual Studio 2008.(Do not have examples)
-Microsoft Windows SDK for Windows 7 and .NET Framework 4 can be download here with all the examples.

Basic RPC example is in the directory is.\Examples\netd\rpc\hello

It uses a make file to compile and that make file is doing all the thing for u like generating C interfaces(header file with all the methods) ,client and server stubs ,compiling and linking.
It will take u some time to figure out the make file and manually convert them to visual studio project.

In this topic I will show u how to run the same example code with visual studio and some common problems I have encountered while developing a RPC application and solutions for them.

How to configure Visual Studio project

  • set additional include path as \includes
  • set linker additional directory path as \libs
  • .lib files to use by the linker -

MIDL compiler for generating stubs.

MIDL compiler is used to generate client and server stubs like in normal RPC model. 3 files are generated
-Client stub
-Server stub
-Header file(.h) for the interface
Header file has included in both client and server side code. In Client side user has to call the functions defined in the header file and it will call the client stub.In server side you have to make implementations for the .h header that u have included in C file (or cpp) and server will automatically call the implementations u provided in above .c (.cpp) file. Don`t worry about how the RPC service in windows call ur code. U will understand it eventually.


midl /Os /win32 /robust -oldnames -cpp_cmd cl -cpp_opt "-E" hello.idl

/Os --- is used for returning complex types in functions. U can use /Oi for returning complex structures like C struct.

/win32 --- Use to tell the midl compiler to build stubs for win32 system architectures.

/robust --- Is used to make run-time checks on the data that is passing in between the stubs.
This will make stubs to complain about the bad data received at the stubs.(EC 1783)


Compiler options to make build for target version of windows

Ex- For windows XP build 501
-D_WIN32_WINNT=0x0501 -DNTDDI_VERSION=0x05010000 -DWINVER=0x0501
for more