Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Introduction to RPC - Part 1

, 22 Dec 2012
An introduction to RPC programming. A simple RPC client/server application is explained.
rpcintro1_demo.zip
Example1Client
Example1Explicit
Example1Explicit.acf
Example1ExplicitClient
Example1ExplicitServer
Example1Server
ReleaseExe
Example1Client.exe
Example1Server.exe
Example1ExplicitClient.exe
Example1ExplicitServer.exe
Example1
Example1.acf
rpcintro1_demo_v2.zip
Example1Explicit.acf
Example1.acf
Example1Client.exe
Example1ExplicitClient.exe
Example1ExplicitServer.exe
Example1Server.exe
rpcintro1_src.zip
Example1Explicit.acf
Example1.acf
rpcintro1_src_v2.zip
Example1Explicit.acf
Example1.acf
// File Example1ExplicitClient.cpp
#include <iostream>
#include "../Example1Explicit/Example1Explicit.h"

int main()
{
   RPC_STATUS status;
   unsigned char* szStringBinding = NULL;

   // Creates a string binding handle.
   // This function is nothing more than a printf.
   // Connection is not done here.
   status = RpcStringBindingCompose(
      NULL, // UUID to bind to.
      reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP
                                                        // protocol.
      reinterpret_cast<unsigned char*>("localhost"), // TCP/IP network
                                                     // address to use.
      reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use.
      NULL, // Protocol dependent network options to use.
      &szStringBinding); // String binding output.

   if (status)
      exit(status);

   handle_t hExample1ExplicitBinding = NULL;

   // Validates the format of the string binding handle and converts
   // it to a binding handle.
   // Connection is not done here either.
   status = RpcBindingFromStringBinding(
      szStringBinding, // The string binding to validate.
      &hExample1ExplicitBinding); // Put the result in the explicit
                                  // binding handle.

   if (status)
      exit(status);

   RpcTryExcept
   {
      // Calls the RPC function. The hExample1ExplicitBinding binding handle
      // is used explicitly.
      // Connection is done here.
      Output(hExample1ExplicitBinding, "Hello Explicit RPC World!");
   }
   RpcExcept(1)
   {
      std::cerr << "Runtime reported exception " << RpcExceptionCode()
                << std::endl;
   }
   RpcEndExcept

   // Free the memory allocated by a string.
   status = RpcStringFree(
      &szStringBinding); // String to be freed.

   if (status)
      exit(status);

   // Releases binding handle resources and disconnects from the server.
   status = RpcBindingFree(
      &hExample1ExplicitBinding); // Frees the binding handle.

   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)

Share

About the Author

Anders Dalvander
Software Developer (Senior) Umetrics
Sweden Sweden
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 22 Dec 2012
Article Copyright 2003 by Anders Dalvander
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid