[100分敬上]RpcTryExcept执行报错,RpcExceptionCode返回RPC_S_ACCESS_DENIED
我是从网上下载了一个非常简单的rpc例子程序,服务端启动没有问题,客户端执行rpc函数的时候,返回了RPC_S_ACCESS_DENIED,意思是执行权限不够。怎么会这样呢? 我自己连自己机器上的程序权限不够?
大虾帮忙看一下吧,问题出在哪里,非常感谢啊!!!!!
-----------------------------------------
用VC2010express建立两个工程。
首先,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("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;}
// 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);
}