Windows RPC编程入门(已动手实践按红体字部分修改可以正常运行)
来源:互联网 发布:ubuntu 修改用户组 编辑:程序博客网 时间:2024/06/10 21:35
首先,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!
多次启动客户端,上面这句话显示多次。
- Windows RPC编程入门(已动手实践按红体字部分修改可以正常运行)
- TCP/IP 动手实践2-windows驱动编程入门2
- TCP/IP 动手实践1-windows驱动开发入门
- Python编程:从入门到实践的动手试一试答案
- 动手实践--lcd_1602驱动编程
- Python编程:从入门到实践的动手试一试答案(第二章)
- Python编程:从入门到实践的动手试一试答案(第三章)
- Python编程:从入门到实践的动手试一试答案(第四章)
- Python编程:从入门到实践的动手试一试答案(第五章)
- Python编程:从入门到实践的动手试一试答案(第六章)
- Python编程:从入门到实践的动手试一试答案(第七章)
- Python编程:从入门到实践的动手试一试答案(第八章)
- Python编程:从入门到实践的动手试一试答案(第九章)
- Python编程:从入门到实践的动手试一试答案(第十章)
- Python编程:从入门到实践的动手试一试答案(第十一章)
- Python编程:从入门到实践的动手试一试答案(第十一章)
- Python编程:从入门到实践的动手试一试答案(第九章)
- Python编程:从入门到实践的动手试一试答案(第六章)
- [LeetCode]Jump Game II、Jump Game
- oracle数据类型
- 子对话框获取父对话框句柄几种解决方法
- Lock_sga 和 pre_page_sga 参数详解
- 使用BeanUtils操作javaBean类
- Windows RPC编程入门(已动手实践按红体字部分修改可以正常运行)
- 最大流水题集锦
- C语言中的整数自动转换原则
- NYOJ 23题 取石子(一)
- (一)验证微信公众平台
- NSPR学习笔记(一):PR_LOG的使用
- [Android] App在三星某些机子上闪退:"不保留活动"
- 微软系统工具套件SysinternalsSuite各个工具功能说明
- USACO 1.2.3 Name That Number