Introduction to RPC on Windows: Part I - Points of Interest
(Page 6 of 6 )
Here are a few points of interest that you might like to know before developing any other RPC applications.
Debugging RPC Applications
If you encounter problems when debugging and you end up finding that the problem is in a MIDL generated file, start over; the real problem is in the client or in the server and not in the MIDL generated code. This article is meant to get you started using RPC, but in a future article I will describe these topics in detail.
Types of binding handles
When using RPC, the binding handles can be implicit (implicit_handle) or explicit (explicit_handle). I always use explicit handles since I'm sometimes connected to multiple servers that do not work with the implicit handle. To use explicit handles, you'll have to change the IDL file, the server and the client. The difference will be in the interface member definition. Now each method defined in the interface will have a handle as its first argument.
// File DoRPCExp.idl
[
// A unique identifier that distinguishes this
// interface from other interfaces.
// this is version 1.0 of this interface.
uuid(2BC900B0-9E3F-451c-A134-581B6328E2CA),
version(1.0),
// this interface uses explicit binding handle.
explicit_handle
]
interface DoRPCExplicit // The interface is named DoRPCExplicit
{
// A function that takes a binding handle and a char string.
void Show( [in] handle_t hBinding, [in, string] const char* szMsg);
}
There is also a handle type known as auto_handle, that allows you to connect and invoke RPC functions on the server automatically without maintaining a RPC handle.
The Application Configuration File (ACF)
The DoRPC example uses an implicit_handle that is a Microsoft extension. You may use the explicit_handle directly in the IDL file, and in that case you won’t need an ACF file because each interface method will absorb the handle as its first argument (as shown above). You may try that by modifying the code provided with this article just to see it working. In case of implicit handles, one usually needs to use a separate Application Configuration File that contains the handles that you wish to use. We’ll see more on ACF files later.
Trust MIDL
One should not modify the MIDL generated files to make them compile; they ought to be correct. Please do check the switches to midl.exe if you feel that they are incorrect and you need some results other than default. Sometimes when compiling MIDL generated source code, you may get a lot of warning messages. You may lower the warning level to two in the C/C++ tab in Project Settings to make them silent.
Shutting down the server
The server we just wrote runs indefinitely until it is forced to shut down by closing the console. That truly isn’t the best way of doing it. A better way is to call the RpcMgmtStopServerListening function that makes the server stop listening. You might be thinking: “If I don’t get back from the RpcServerListen function, how am I supposed to call this function?” You could add another function to the interface (StopServer() is a good candidate!!) that will call RpcMgmtStopServerListening or you could create another thread in the server before calling RpcServerListen that will call RpcMgmtStopServerListening after a predefined time interval or on a particular event.
Summary
This article introduced you to the world of RPC and using it to develop client/server applications. If you really want to take advantage of this technology, just glance through the examples in the Platform SDK samples. They’re a great place to start. I shall discuss more complex issues in the RPC world in my future articles.
References
- MSDN - MIDL and RPC
- MSDN - Remote Procedure Call
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|