RPC远程过程调用编程练习

 1.首先使用uuidgen工具($(VS)/Common7/Tools)生成idl接口文件

    uuidgen /i /ohello.idl

    注意"/o"和文件名之间没有空白!

2.然后打开生成的接口文件,在里面添加接口

[ uuid(d1717d33-8e03-456c-a79b-57fc212f5042), version(1.0), pointer_default(unique) ] interface hello { void HelloProc([in, string] unsigned char * pszString); void ShutDown(void); }

3.编写acf文件,此文件要与idl文件同名且放在同一目录下。此文件是可选的。

[ implicit_handle (handle_t hello_Binding) ] interface hello { }

4.使用midl编译idl文件

    midl hello.idl

    编译生成hello.h, hello_c.c, hello_s.c三个文件。hello.h为客户端和服务端共用头文件,hello_c.c要放到客户端,hello_s.c要放到服务端。

5.服务端部分代码

#include #include using namespace std; int main(void) { RPC_STATUS status = 0; unsigned int nMinCalls = 1; unsigned int nMaxCalls = 20; status = RpcServerUseProtseqEp( (unsigned char *)"ncacn_np", nMaxCalls, (unsigned char *)"//pipe//{a5194558-21a6-4978-9610-2072fcf1dc6e}", NULL ); if ( status != 0 ) { cout<<"RpcServerUseProtseqEp return "<

6.客户端代码

 

#include #include #include using namespace std; void doRpcCall(); int main(int argc, char** argv) { RPC_STATUS status; unsigned char * pszNetworkAddress = NULL; unsigned char * pszStringBinding = NULL; for ( int i = 1; i < argc; ++i ) { if ( strcmp(argv[i], "-ip") == 0 ) { pszNetworkAddress = (unsigned char *)argv[++i]; } } status = RpcStringBindingCompose( NULL, (unsigned char *)"ncacn_np", pszNetworkAddress, (unsigned char *)"//pipe//{a5194558-21a6-4978-9610-2072fcf1dc6e}", NULL, &pszStringBinding ); if ( status != 0 ) { cout<<"RpcStringBindingCompose return "< succeed!"<

你可能感兴趣的:(RPC远程过程调用编程练习)