With the help of the code generated with MIDL compiler we can easily write a server application. You may write the code as below and read the comments to follow what’s happening around.
// File DoRPC_Server.cpp
#include <stdio.h>
#include "..\RPC1_IDL\DoRPC.h"
int main()
{
RPC_STATUS status;
// Uses the protocol combined with the endpoint for receiving
// remote procedure calls.
status = RpcServerUseProtseqEp(
(unsigned char*)("ncacn_ip_tcp"),// Use TCP/IP protocol RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog q length for TCP/IP.
(unsigned char*)("9191"), // TCP/IP port to use.
NULL); // No security.
if(status)
{
exit(status);
}
// Registers the DoRPC interface.
status = RpcServerRegisterIf(
DoRPC_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)
exit(status);
// Start to listen for remote procedure calls for all registered interfaces.
// 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)
{
exit(status);
}
return 0;
}
// Memory allocation function for RPC.
// The runtime uses these two functions for allocating/deallocating
// enough memory to pass the string to the server.
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);
}
// Now we implement our server function.
void Show(const unsigned char* szMsg)
{
printf("%s\n",szMsg);
}