Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Win32

Introduction to RPC - Part 1

Rate me:
Please Sign up or sign in to vote.
4.85/5 (115 votes)
22 Dec 2012CPOL6 min read 1.1M   21.8K   258  
An introduction to RPC programming. A simple RPC client/server application is explained.
// File Example1ExplicitServer.cpp
#include <iostream>
#include "../Example1Explicit/Example1Explicit.h"

// Server function.
void Output( 
   /* [in] */ handle_t /*hBinding*/,
   /* [string][in] */ const char* szOutput)
{
   std::cout << szOutput << std::endl;
}

int main()
{
   RPC_STATUS status;

   // Uses the protocol combined with the endpoint for receiving
   // remote procedure calls.
   status = RpcServerUseProtseqEp(
      reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP
                                                        // protocol.
      RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP.
      reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use.
      NULL); // No security.

   if (status)
      exit(status);

   // Registers the Example1Explicit interface.
   status = RpcServerRegisterIf(
      Example1Explicit_v1_0_s_ifspec, // Interface to register.
      NULL, // Use the MIDL generated entry-point vector.
      NULL); // Use the MIDL generated entry-point vector.

   if (status)
      exit(status);

   // Start to listen for remote procedure calls for all registered interfaces.
   // This call will not return until RpcMgmtStopServerListening is called.
   status = RpcServerListen(
      1, // Recommended minimum number of threads.
      RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Recommended maximum number of threads.
      FALSE); // Start listening now.

   if (status)
      exit(status);
}

// Memory allocation function for RPC.
// The runtime uses these two functions for allocating/deallocating
// enough memory to pass the string to the server.
void* __RPC_USER midl_user_allocate(size_t size)
{
    return malloc(size);
}

// Memory deallocation function for RPC.
void __RPC_USER midl_user_free(void* p)
{
    free(p);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions