如何在本地进程中调用远端计算机上的进程

回答这个问题其实就是RPC的编程:开发RPC应用程序,一个重要的要素就是接口。显然在 客户 和服务器的接口存根必须基于完全相同的函数定义上;否则RPC会失败。  
  1.定义接口(HELLO.IDL):  
  [  
          uuid(0EB13191-F7C4-11d3-BF4A-00104B17A4FB),/*注意你必须在自己的程序中生成自己的UUID,利用GUIDGEN 应用 程序,在VISUAL   STUDIO中*/  
   
          version(1.0)  
  ]  
  2.应用 程序 配置文件(HELLO.ACF)  
  [  
          implicit_handle(handle_t   hello_IfHandle)  
          //implicit_handle   的使用规定作为全局变量 维护 句柄  
  ]  
  interface   hello  
  {  
  }  
  3.编译HELLO.IDL  
  MIDL   HELLO.IDL  
  编译的结果生成HELLO_C.C,HELLO_S.C,HELLO.H  
  你看一下这些文件,发现了什么?!  
  4.编写 服务 器程序  
  #include   <stdlib.h>  
  #include   <stdio.h>  
  #include   "hello.h"  
   
  void   HelloProc(const   unsigned   char*   pszString)  
  {  
  printf("%s/n",pszString);  
  }  
   
  void   Shutdown(void)  
  {  
  RpcMgmtStopServerListening(NULL);  
  RpcServerUnregisterIf(NULL,NULL,FALSE);  
  }  
   
  void   main(int   argc,char*   argv[])  
  {  
  RpcServerUseProtseqEp((unsigned   char   *)"ncacn_ip_tcp",20,(unsigned   char   *)"8000",NULL);  
  RpcServerRegisterIf(hello_v1_0_s_ifspec,NULL,NULL);  
  RpcServerListen(1,20,FALSE);  
  }  
   
  void   __RPC_FAR*   __RPC_USER   midl_user_allocate(size_t   len)  
  {  
  return(malloc(len));  
  }  
   
  void   __RPC_USER   midl_user_free(void   __RPC_FAR   *ptr)  
  {  
  free(ptr);  
  }  
  interface   hello  
  {  
        void   HelloProc([in,string]const   unsigned   char*   pszString);  
        void   Shutdown(void);  
  }  
  5.编译服务器程序  
  CL   HELLOS.C   HELLO_S.C   RPCRT4.LIB  
  生成HELLOS.EXE  
  6.编写客户程序  
  #include   <stdlib.h>  
  #include   <stdio.h>  
  #include   <string.h>  
  #include   "hello.h"  
   
  void   main(int   argc,char*   argv[])  
  {  
  unsigned   char*   pszStringBinding;  
   
  if(argc!=3)  
  {  
  printf("Usage:%s   hostname   string-to-print/n",argv[0]);  
  exit(1);  
  }  
   
  RpcStringBindingCompose(NULL,(unsigned   char*)"ncacn_ip_tcp",(unsigned   char*)argv[1],(unsigned   char*)"8000",NULL,&pszStringBinding);  
  RpcBindingFromStringBinding(pszStringBinding,&hello_IfHandle);  
  if(strcmp(argv[2],"SHUTDOWN"))HelloProc((unsigned   char*)argv[2]);  
  else   Shutdown();  
  RpcStringFree(&pszStringBinding);  
  RpcBindingFree(&hello_IfHandle);  
  exit(0);  
  }  
   
  void   __RPC_FAR*   __RPC_USER   midl_user_allocate(size_t   len)  
  {  
  return(malloc(len));  
  }  
   
  void   __RPC_USER   midl_user_free(void   __RPC_FAR   *ptr)  
  {  
  free(ptr);  
  }  
  7.编译客户程序  
  CL   HELLOC.C   HELLO_C.C   RPCRT4.LIB  
  生成HELLOC.EXE  
  8. 测试 程序  
  在两个DOS窗口中运行,一个运行HELLOS,在同一台机器上,另一个运行HELLOC   LOCALHOST   “HELLO”  
  看看发生了什么?   
    
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值