RPC

 远程过程调用的详细信息:http://msdn.microsoft.com/en-us/library/windows/desktop/aa378651(v=vs.85).aspx

[
    uuid(7a98c250-6808-11cf-b73b-00aa00b677a7),
    version(1.0)
]
interface hello
{
    void HelloProc([in, string] unsigned char * pszString);
    void Shutdown(void);
}


 

[
    implicit_handle (handle_t hello_IfHandle)
] 
interface hello
{
}


copy好后hello.idl和hello.acf运行midl hello.idl生成hello.h、hello_c.c、hello_s.c,编写helloc.c

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "hello.h" 
#include "rpc.h"
#include <windows.h>

void main()
{
	RPC_STATUS status;
	unsigned char * pszUuid             = NULL;
	unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
	unsigned char * pszNetworkAddress   = "127.0.0.1";
	unsigned char * pszEndpoint         = "12345";
	unsigned char * pszOptions          = NULL;
	unsigned char * pszStringBinding    = NULL;
	unsigned char * pszString           = "hello, world";
	unsigned long ulCode;

	status = RpcStringBindingCompose(pszUuid,
		pszProtocolSequence,
		pszNetworkAddress,
		pszEndpoint,
		pszOptions,
		&pszStringBinding);
	if (status) exit(status);

	status = RpcBindingFromStringBinding(pszStringBinding, &hello_IfHandle);

	if (status) exit(status);

	RpcTryExcept  
	{
		HelloProc(pszString);
		//Shutdown();
	}
	RpcExcept(1) 
	{
		ulCode = RpcExceptionCode();
		printf("Runtime reported exception 0x%lx = %ld\n", ulCode, ulCode);
	}
	RpcEndExcept

	status = RpcStringFree(&pszStringBinding); 

	if (status) exit(status);

	status = RpcBindingFree(&hello_IfHandle);

	if (status) exit(status);

	exit(0);
}

/******************************************************/
/*         MIDL allocate and free                     */
/******************************************************/

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);
}

在同一个solution中new一个project,添加hellos.c

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "hello.h"
#include "rpc.h"
#include <windows.h>

void main()
{
	RPC_STATUS status;
	unsigned char * pszProtocolSequence = "ncacn_ip_tcp";
	unsigned char * pszSecurity         =  NULL; 
	unsigned char * pszEndpoint         = "12345";
	unsigned int    cMinCalls = 1;
	unsigned int    fDontWait = FALSE;

	status = RpcServerUseProtseqEp(pszProtocolSequence,
		2,
		pszEndpoint,
		pszSecurity); 

	if (status) exit(status);

	status = RpcServerRegisterIf(hello_v1_0_s_ifspec,  
		NULL,   
		NULL); 

	if (status) exit(status);

	status = RpcServerListen(cMinCalls,
		RPC_C_LISTEN_MAX_CALLS_DEFAULT,
		fDontWait);

	if (status) exit(status);
}

/******************************************************/
/*         MIDL allocate and free                     */
/******************************************************/

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);
}


另建一hellop.c

#include <stdio.h>
#include <windows.h>

void Shutdown(void)
{
	RPC_STATUS status;

	status = RpcMgmtStopServerListening(NULL);

	if (status) 
	{
		exit(status);
	}

	status = RpcServerUnregisterIf(NULL, NULL, FALSE);

	if (status) 
	{
		exit(status);
	}
} 

void HelloProc(char * pszString)
{
	printf("%s\n", pszString);
}


在vs2008的client和server工程里分别修改properties->linker->input->addtional dependencies,输入RpcRT4.lib

在rpc运行的过程中,可能会受到windows的安全限制,修改注册表HKEY_LOCAL_MACHINE \ SOFTWARE\Policies\ Microsoft\Windows NT\RPC ,设置Restrict
RemoteClients
值为0,没有响应的项就添加

若将client/server中的一端拿到没有支持vs2008的c/c++运行库的vm中,设置properties->c/c++->code generation->runtime library为multi-threaded debug

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值