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 Example1Server.cpp
#include <iostream>
#include "../Example1/Example1.h"

// Server function.
void Output( 
   /* [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 Example1 interface.
   status = RpcServerRegisterIf(
      Example1_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)

Share

About the Author

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

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