Windows RPC编程入门(已动手实践按红体字部分修改可以正常运行)
2013-08-05 23:09
489 查看
Windows本身支持成为一个RPC服务器。MSDN里面有个很好的例子,但是,WindowsXP上面默认的RPC/tcpip功能没有打开,必须运行gpedit.msc,计算机配置->管理模版->系统->远程过程调用->用于未验证的RPC...->选择"已启动",限定项选择"无"。用VC2010express建立两个工程,按照MSDN的例子:
首先,idl文件my.idl:
编译以后,相应的文件放到服务器端和客户端程序里面去。
服务器端实现:
启动以后显示:
UseProtseqEq ok
Register if ok
客户端代码:
运行显示:
szString=ncacn_ip_tcp:localhost[4747]
此时,服务器端显示:
server:Hello RPC World!
多次启动客户端,上面这句话显示多次。
首先,idl文件my.idl:
[ uuid(ba209999-0c6c-11d2-97cf-00c04f8eea45), version(1.0) ] interface hw // The interface is named hw { // A function that takes a zero-terminated string. void Hello( [in, string] const char* szOutput); } |
服务器端实现:
#include"stdafx.h" #include<stdio.h> #include<stdlib.h> #include"my_h.h" #pragma comment(lib,"rpcrt4") #pragma comment(lib,"ole32") extern "C"{ void Hello(handle_t IDL_handle,const unsigned char*psz){ printf("server:%s\n",psz); } void Shutdown(handle_t IDL_handle){ RpcMgmtStopServerListening(NULL); RpcServerUnregisterIf(NULL,NULL,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); } } int main() { RPC_STATUS status; status = RpcServerUseProtseqEp( (RPC_CSTR)("ncacn_ip_tcp"), // Use TCP/IP protocol. RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP. (RPC_CSTR)("4747"), // TCP/IP port to use. NULL); // No security. if (status){ printf("ServerUse failed\n"); exit(status);} else printf("UseProtseqEq ok\n"); status = RpcServerRegisterIf( hw_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){ printf("Register failed\n"); exit(status);} else printf("Register if ok\n"); // 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){ printf("Server listen failed\n"); exit(status);} else printf("listen ok\n"); return 0; } |
UseProtseqEq ok
Register if ok
客户端代码:
// myclient.cpp : Defines the entry point for the console application. #include"stdafx.h" #include<windows.h> #include<stdio.h> #include"my_h.h" #pragma comment(lib,"rpcrt4") #pragma comment(lib,"ole32") int main(void){ RPC_STATUS status; RPC_BINDING_HANDLE hwBinding; unsigned char* szStringBinding=NULL; status=RpcStringBindingCompose(//建立一个String Binding句柄,并不连接 NULL, (RPC_CSTR)("ncacn_ip_tcp"), (RPC_CSTR)("localhost"), (RPC_CSTR)("4747"), NULL, (RPC_CSTR*)&szStringBinding); if(status){ printf("StringBinding failed\n"); exit(status);} printf("szString=%s\n",szStringBinding); status=RpcBindingFromStringBinding( szStringBinding, &hwBinding); if(status){ printf("Bind from String failed:%d\n",GetLastError()); exit(status);} RpcTryExcept{ Hello(hwBinding,(RPC_CSTR)("Hello RPC World!")); } RpcExcept(1){ printf("Runtime reported exception:%d,except=%d\n",GetLastError(),RpcExceptionCode()/*RPC_S_ACCESS_DENIED==5L*/);//这里返回了5 } RpcEndExcept status = RpcBindingFree(&hwBinding); // Frees the binding handle. if (status){ printf("Bind free failed\n"); exit(status);} return 0; } 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); } |
szString=ncacn_ip_tcp:localhost[4747]
此时,服务器端显示:
server:Hello RPC World!
多次启动客户端,上面这句话显示多次。
相关文章推荐
- Python编程 从入门到实践 第四章 作业参考答案 (部分)
- “在我的机器上可以运行”症状自查(Windows编程)
- 自己的程序编程变成windows可以运行的.exe文件----py2exe
- windows下修改文件时的换行符引起在linux运行不正常的解决
- python 编程 入门到实践 第三章 列表部分 (课后题加原书)
- TCP/IP 动手实践1-windows驱动开发入门
- 在OD中修改代码可以正常运行,尝试各种补丁却不行
- python 编程从入门到实践11章 测试代码 11.2测试类11.2.2 修改
- Windows编程入门(4)--修改MFC的图标
- VS2010/MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)
- 附录A:Windows编程入门
- Windows Server 2003 下Msdtc不通导致工作流Windows Workflow Foundation无法正常运行问题的解决
- eclipse 下,使用正常模式可以运行,DEBUG模式就卡住的解决方案
- 问题描述:war包发布——在windows平台可以运行,在linux平台却失败的问题
- 从零开始,学习windows编程(8)--自己动手,编译CRT
- Windows编程入门(3)--MFC中给按钮添加位图
- 龙芯处理器是否可以运行windows操作系统
- 简单的RPC编程实践——HelloWorld的实现
- 13.MapReduce第3部分(编程实践WordCount)
- linux学习入门2-3——bash命令基本操作(linuxcast.net)(bash部分快捷键、uname、history、通配符、切换用户、作业管理(前台、后台运行,暂停等操作))