远程过程调用的详细信息: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