1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include "ContextExample_h.h"
// Write a formatted error message to std::cerr.
DWORD HandleError(const char* szFunction, DWORD dwError)
{
void* pBuffer = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
LPSTR(&pBuffer),
0,
NULL);
std::cerr << szFunction << " failed. "
<< (pBuffer ? LPCSTR(pBuffer) : "Unknown error. ")
<< "(" << dwError << ")" << std::endl;
LocalFree(pBuffer);
return dwError;
}
int main()
{
RPC_STATUS status;
unsigned char* szStringBinding = NULL;
std::clog << "Calling RpcStringBindingCompose" << std::endl;
// Creates a string binding handle.
// This function is nothing more than a printf.
// Connection is not done here.
status = RpcStringBindingCompose(
NULL, // UUID to bind to.
reinterpret_cast<unsigned char*>("ncacn_ip_tcp"), // Use TCP/IP protocol.
reinterpret_cast<unsigned char*>("localhost"), // TCP/IP network address to use.
reinterpret_cast<unsigned char*>("4747"), // TCP/IP port to use.
NULL, // Protocol dependent network options to use.
&szStringBinding); // String binding output.
if (status)
return HandleError("RpcStringBindingCompose", status);
handle_t hBinding = NULL;
std::clog << "Calling RpcBindingFromStringBinding" << std::endl;
// Validates the format of the string binding handle and converts
// it to a binding handle.
// Connection is not done here either.
status = RpcBindingFromStringBinding(
szStringBinding, // The string binding to validate.
&hBinding); // Put the result in the explicit binding handle.
if (status)
return HandleError("RpcBindingFromStringBinding", status);
std::clog << "Calling RpcStringFree" << std::endl;
// Free the memory allocated by a string.
status = RpcStringFree(
&szStringBinding); // String to be freed.
if (status)
return HandleError("RpcStringFree", status);
std::clog << "Calling RpcEpResolveBinding" << std::endl;
// Resolves a partially-bound server binding handle into a
// fully-bound server binding handle.
status = RpcEpResolveBinding(hBinding, ContextExample_v1_0_c_ifspec);
if (status)
return HandleError("RpcEpResolveBinding", status);
RpcTryExcept
{
std::clog << "Calling Open" << std::endl;
// Open the context handle.
CONTEXT_HANDLE hContext = Open(hBinding, "Hello Context World!");
std::cout << "Press enter to call Output" << std::endl;
std::cin.get();
std::clog << "Calling Output" << std::endl;
// Calls the RPC function. The hBinding binding handle
// is used explicitly.
Output(hContext);
std::cout << "Press enter to call Close" << std::endl;
std::cin.get();
std::clog << "Calling Close" << std::endl;
// Close the context handle.
Close(&hContext);
}
}
| |