服务器应用程序
以下示例来自平台软件开发工具包的 RPC\Hello 目录中的“Hello World”应用程序 (SDK) 。 分布式应用程序的服务器端通知系统其服务可用。 然后,它会等待客户端请求。 MIDL 编译器必须与以下示例一起使用。
根据应用程序的大小和编码首选项,可以选择在一个或多个单独的文件中实现远程过程。 在本教程程序中,源文件 Hellos.c 包含main服务器例程。 文件 Hellop.c 包含远程过程。
将远程过程组织到单独的文件中的好处是,过程可以与独立程序链接,以在代码转换为分布式应用程序之前对其进行调试。 在独立程序中运行这些过程后,可以编译包含远程过程的源文件并将其链接到服务器应用程序。 与客户端应用程序源文件一样,服务器应用程序源文件必须包含 Hello.h 头文件。
服务器调用 RPC 运行时函数 RpcServerUseProtseqEp 和 RpcServerRegisterIf ,使绑定信息可供客户端使用。 此示例程序将接口句柄名称传递给 RpcServerRegisterIf。 其他参数设置为 NULL。 然后,服务器调用 RpcServerListen 函数,以指示它正在等待客户端请求。
服务器应用程序还必须包括服务器存根调用的两个内存管理功能: midl_user_allocate 和 midl_user_free。 当远程过程将参数传递给服务器时,这些函数在服务器上分配和释放内存。 在此示例程序中, midl_user_allocate 和 midl_user_free 只是 C 库函数 malloc 和 free 的包装器。 (请注意,在 MIDL 编译器生成的正向声明中,“MIDL”为大写。头文件 Rpcndr.h 分别将midl_user_free和midl_user_allocate定义为MIDL_user_free和MIDL_user_allocate。)
/* file: hellos.c */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "hello.h"
#include <windows.h>
void main()
{
RPC_STATUS status;
unsigned char * pszProtocolSequence = "ncacn_np";
unsigned char * pszSecurity = NULL;
unsigned char * pszEndpoint = "\\pipe\\hello";
unsigned int cMinCalls = 1;
unsigned int fDontWait = FALSE;
status = RpcServerUseProtseqEp(pszProtocolSequence,
RPC_C_LISTEN_MAX_CALLS_DEFAULT,
pszEndpoint,
pszSecurity);
if (status) exit(status);
status = RpcServerRegisterIf(hello_ServerIfHandle,
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);
}