Creating an RPC Interface
Overview
This tutorial walks you through the steps required to define, implement, and register a Remote Procedure Call (RPC) interface on Windows using the Microsoft Interface Definition Language (MIDL) and the RPC runtime.
Prerequisites
- Windows 10 (or later) development environment
- Visual Studio 2022 (or later) with C++ workload
- Basic knowledge of C/C++
Step 1 – Define the Interface
Create a file named MyInterface.idl and add the following MIDL definition:
// MyInterface.idl
import "rpc.idl";
[
uuid(6D2E5E4C-7B8A-4F5A-9B2B-7C0C8F7F1A2B),
version(1.0)
]
interface MyInterface
{
void HelloWorld([in, string] const wchar_t* name);
};
Step 2 – Generate Stubs
Run the MIDL compiler to generate the client and server stubs:
midl /win64 MyInterface.idl
This creates MyInterface_h.h, MyInterface_c.c, and MyInterface_s.c.
Step 3 – Implement the Server
Implement the RPC function in server.cpp:
#include "MyInterface_h.h"
#include
void HelloWorld(const wchar_t* name)
{
std::wcout << L"Hello, " << name << L"!" << std::endl;
}
Compile and link with rpcrt4.lib and the generated server stub (MyInterface_s.c).
Step 4 – Register the Interface
Register the interface using rpcss:
rpcss -r MyInterface.idl
Step 5 – Create the Client
Write a client that calls the RPC function:
#include "MyInterface_h.h"
#include
int wmain(int argc, wchar_t* argv[])
{
RPC_STATUS status = RpcBindingFromStringBinding(L"ncacn_ip_tcp:127.0.0.1[12345]", &MyInterface_IfHandle);
if (status) { std::wcerr << L"Binding failed: " << status << std::endl; return 1; }
HelloWorld(L"World");
RpcBindingFree(&MyInterface_IfHandle);
return 0;
}
Compile with rpcrt4.lib and the client stub (MyInterface_c.c).
Run the Example
Start the server executable first, then run the client. You should see:
Hello, World!