RPC远程过程调用实例详解

IDL文件可以由uuidgen.exe创建。首先找到系统中uuidgen.exe的位置,如:C:“Program Files“Microsoft Visual Studio 9.0“Common7“Tool


IDL文件可以由uuidgen.exe创建。

首先找到系统中uuidgen.exe的位置,如:C:“Program Files“Microsoft Visual Studio 9.0“Common7“Tools。在此目录下运行命令"uuidgen

/i /ohello.idl",即可在该位置生成一个IDL文件:hello.idl。文件内容

//hello.idl

[

uuid(b2617491-ba5a-48a9-b388-9f0cee8ec882),

version(1.0)

]

interface INTERFACENAME

{

}

然后,增加接口。

//hello.idl

[

uuid(b2617491-ba5a-48a9-b388-9f0cee8ec882),

version(1.0)

]

interface INTERFACENAME

{

void HelloProc([in,string]unsigned char* szhello);

void ShutDown(void);

}

hello.acf文件内容

//hello.acf

[

implicit_handle (handle_t hello_IfHandle)

]

interface INTERFACENAME

{

}

注意: 1)hello.idl文件与hello.acf文件中的接口名称(INTERFACENAME)应一致,否则接下来编译的时候会报错。

2)hello.idl文件与hello.acf文件应放在同一目录下。

有资料说可以用"midl hello.idl"命令直接进行编译,但是我试过之后,总是提示MIDL1005 的错误,没办法,用vs2008进行编译的,步骤如下。

首先,创建一个空的项目,如RpcTest将好的hello.idl文件添加至RpcTest项目中。

然后,直接进行编译。

这 时就可以看到RpcTest项目的生成目录下有了hello_h.h, hello_c.c, hello_s.c三个文件。其中,hello_h.h文件是客户端和服务器端程序共同要用到的,hello_c.c是客户端程序需要 的,hello_s.c是服务器程序所需要的。

在hello_h.h文件中可以看到hello.idl中所定义的接口实体,一个全局句柄变 量(handle_t)以及客户端与服务端的接口句柄名INTERFACENAME_v1_0_c_ifspec和 INTERFACENAME_v1_0_s_ifspec。客户端、服务端应用程序在实时调用将使用接口句柄名。

/* interface INTERFACENAME */

/* [implicit_handle][version][uuid] */

void HelloProc(/* [string][in] */ unsigned char *szhello);

void ShutDown( void);

extern handle_t hello_IfHandle;

extern RPC_IF_HANDLE INTERFACENAME_v1_0_c_ifspec;

extern RPC_IF_HANDLE INTERFACENAME_v1_0_s_ifspec;

服 务端通过调用RPC实现函数RpcServerUseProtseqEp 与RpcServrRegisterIf捆绑信息并提供给客户端,例子程序传递接口句柄名给RpcServerRegisterIf,其它的参数被置为 空,客户端然后调用RpcServerListen函数等待客户端的请求。

服务端应用程序必须包含两个内存管理函数midl_user_allocate与midl_user_free。当远端过程调用向服务端传递参数时,调用这两个函数分配及释放内存。

除此之外,服务端还应实现具体的接口函数功能。详细代码如下。

//server.cpp

#include iostream

using namespace std;

#include "hello_h.h"

int main(void)

{

RPC_STATUS status = 0;

unsigned int mincall = 1;

unsigned int maxcall = 20;

status = RpcServerUseProtseqEp(

(unsigned char *)"ncacn_np",

maxcall,

(unsigned char *)"““pipe““hello",

NULL);

if(status != 0){

cout"RpcServerUseProtseqEp returns: "statusendl;

return -1;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值