.NET
  Home arrow .NET arrow Page 5 - Introduction to RPC on Windows: Part I
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introduction to RPC on Windows: Part I
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 20
    2005-01-10

    Table of Contents:
  • Introduction to RPC on Windows: Part I
  • Meet IDL
  • Doing the RPC stuff
  • Writing Server Side code
  • Writing Client Side Code
  • Points of Interest

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introduction to RPC on Windows: Part I - Writing Client Side Code


    (Page 5 of 6 )

    Similar to the server application we can write the client application code as below.

    // File DoRPC_Client.cpp
    #include <stdio.h>
    #include "..\RPC1_IDL\DoRPC.h"

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

       // Creates a string binding handle.
       // This function formats the passed values in a
       // predefined format for use by RPC. Just like printf
       // Connection is not done here.
       status = RpcStringBindingCompose(
          NULL, // UUID to bind to.
          (unsigned char*)("ncacn_ip_tcp"),
    // Use TCP/IP protocol.
          (unsigned char*)("localhost"), // TCP/IP network // the same machine as server
          (unsigned char*)("9191"),
    // TCP/IP port to use.
          NULL,     // Protocol dependent network
    options to use.
          &szStringBinding);   // String binding
    output.

       if (status)
          exit(status);

       // 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.
          &hDoRPCBinding); // Put the result in the implicit binding
                             
    // handle defined in the IDL file.

     if(status)
     {
      exit(status);
     }

     RpcTryExcept
     {
      // Calls the RPC function. The hDoRPCBinding
    binding handle
      // is used implicitly.
      // Connection is done here.
      const unsigned char szMsg[] = "Client: I Can
    RPC Now!";
      Show(szMsg);
     }
     RpcExcept(1)
     {
      printf("Runtime exception occured: %d\n",RpcExceptionCode());
     }
     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(
     &hDoRPCBinding); // Frees the implicit binding
    handle defined in
          // the IDL file.

     if (status)
     {
      exit(status);
     }

     return 0;
    }

    // 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);
    }

    See it happening

    Now open up two command prompt consoles and execute server on one and Client on the other and you’ll see what we’ve accomplished with the help of MIDL and RPC. Wasn’t that easy?

    The figure below shows the demo running on my computer:

    Introduction to RPC on Windows Part I

    Figure 2: The output

    Looking at the whole picture

    Let’s go back and recall the steps that we performed to achieve this.  First we need to compile the IDL file to get the client proxy(DoRPC_c.c), the server stub (DoRPC_s.c) and the common header file (DoRPC.h). Next the proxy and the stub are compiled, with the client and server implementations of the interface producing standalone client and server executables. If nothing goes wrong and the client and server applications can communicate, we just completed our first RPC client/server application.

    More .NET Articles
    More By Digvijay Chauhan


       · Hi,I was able to compile the *.idl file. But I am getting an error while trying to...
       · i successfully compile the code...but when i running server program i got pop up...
       · First of all I would like to thank you for such wonderful articles on smart card and...
       · You may use the Smart Client like approach to execute Smart Card commands via the...
       · Its very kind of you to respond to my query. We were going through a office move and...
       · It's simple,a C# or J# script would do the job ...... only required thing is...
       · Hi DigvijayThanks very much for the response, but I am still struggling how to...
       · Hi,I believe i conveyed something wrong.What i meant was ........ after...
       · I am so sorry, this is my first ever attempt on .net and I am still not quite sure ...
       · First ..... You need not register the script if you have it running on the client...
       · I think I now understand whats happening.This is my latest codeprivate void...
       · Dear Friend,You've done it finally.Just enable Smart Clients on the client...
       · Hithanks for good article.I have some problem with MIDLhow I can get...
       · I am not able to compile the server code its stating that the function does not...
       · I am not able to compile the server code its stating that the function does not...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT