停止服务器应用程序

服务器应用程序可以通过调用 RpcMgmtStopServerListeningRpcServerUnregisterIf 或直接退出主机进程来停止侦听客户端。 这两种方法都是可以接受的。 如果服务器遵循第一种方法,则应实现以下步骤:

服务器函数 RpcServerListen 在发生异常或调用 RpcMgmtStopServerListening 之前不会返回到调用程序。 默认情况下,仅允许另一个服务器线程使用 RpcMgmtStopServerListening 来停止 RPC 服务器。 尝试停止服务器的客户端将收到错误RPC_S_ACCESS_DENIED。 但是,可以将 RPC 配置为允许部分或所有客户端停止服务器。 有关详细信息,请参阅 RpcMgmtStopServerListening

还可以让客户端应用程序对服务器上的关闭例程进行远程过程调用。 关闭例程调用 RpcMgmtStopServerListeningRpcServerUnregisterIf。 本教程示例程序应用程序通过将新的远程函数 Shutdown 添加到文件 Hellop.c 来使用此方法。

Shutdown 函数中, RpcMgmtStopServerListening 的单个 null 参数指示本地应用程序应停止侦听远程过程调用。 RpcServerUnregisterIf 的两个 null 参数是通配符,表示应取消注册所有接口。 FALSE 参数指示应立即从注册表中删除接口,而不是等待挂起的调用完成。

/* add this function to hellop.c */
void Shutdown(void)
{
    RPC_STATUS status;
 
    status = RpcMgmtStopServerListening(NULL);
 
    if (status) 
    {
       exit(status);
    }
 
    status = RpcServerUnregisterIf(NULL, NULL, FALSE);
 
    if (status) 
    {
       exit(status);
    }
} //end Shutdown