[100分敬上]RpcTryExcept执行报错,RpcExceptionCode返回RPC_S_ACCESS_DENIED
我是从网上下载了一个非常简单的rpc例子程序,服务端启动没有问题,客户端执行rpc函数的时候,返回了RPC_S_ACCESS_DENIED,意思是执行权限不够。怎么会这样呢? 我自己连自己机器上的程序权限不够?
大虾帮忙看一下吧,问题出在哪里,非常感谢啊!!!!!
-----------------------------------------
用VC2010express建立两个工程。
首先,idl文件my.idl:
- XML code
[ 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);}编译以后,相应的文件放到服务器端和客户端程序里面去。
服务器端实现:
- C/C++ code
#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("glm 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
客户端代码:
- C/C++ code
// 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]
Runtime reported exception:0,except=5
请按任意键继续. . .
[解决办法]
两篇参考文章.
http://technet.microsoft.com/en-us/library/aa995844(EXCHG.80).aspx
http://blog.csdn.net/Edward007/archive/2008/01/08/2030766.aspx
[解决办法]
代码没有任何问题:
采用RPC通信的服务程序,在Win2000和Win2003下都运行正常,但在XP下运行失败,错误提示是“拒绝访问”,怎么解决呢?
解决方法如下:
单击“开始”,单击“运行”,键入“gpedit.msc”,然后单击“确定”,打开组策略。
打开组策略后,选择“计算机配置->管理模板->系统->远过程调用->用于未验证的RPC客户端的限制”。
双击“用于未验证的RPC客户端的限制”,在弹出的右键菜单里选择“属性”。
在弹出的“属性”对话框里选择“设置”属性页,选择“已启用”、“要应用的RPC运行时未验证的客户端限制”选择“无”。
单击“确定”完成设置。
经测试确定只有XP+SP2会有RPC通信“拒绝访问”的问题,其他Win2K及Win2K以上系统没有此问题。
然后运行代码,在服务器端就能打印出
glm server:Hello RPC World!