Export (0) Print
Expand All

The Server Application

The example below is from the 'Hello World' application in the RPC\Hello directory of the Platform Software Development Kit (SDK). The server side of the distributed application informs the system that its services are available. It then waits for client requests. The MIDL compiler must be used with the example below.

Depending on the size of your application and your coding preferences, you can choose to implement remote procedures in one or more separate files. In this tutorial program, the source file Hellos.c contains the main server routine. The file Hellop.c contains the remote procedure.

The benefit of organizing the remote procedures in separate files is that the procedures can be linked with a standalone program to debug the code before it is converted to a distributed application. After the procedures work in the standalone program, you can compile and link the source files containing the remote procedures with the server application. As with the client-application source file, the server-application source file must include the Hello.h header file.

The server calls the RPC run-time functions RpcServerUseProtseqEp and RpcServerRegisterIf to make binding information available to the client. This example program passes the interface handle name to RpcServerRegisterIf. The other parameters are set to NULL. The server then calls the RpcServerListen function to indicate that it is waiting for client requests.

The server application must also include the two memory management functions that the server stub calls: midl_user_allocate and midl_user_free. These functions allocate and free memory on the server when a remote procedure passes parameters to the server. In this example program, midl_user_allocate and midl_user_free are simply wrappers for the C-library functions malloc and free. (Note that, in the MIDL compiler- generated forward declarations, "MIDL" is uppercase. The header file Rpcndr.h defines midl_user_free and midl_user_allocate to be MIDL_user_free and MIDL_user_allocate, respectively.)


/* 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);
}


 

 

Community Additions

ADD
Show:
© 2014 Microsoft