Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File DoRPC_Client.cpp
- #include <iostream>
- #include <DoRPC.h>
- #include <stdexcept>
- #include <string_view>
- #include <fmt/format.h>
- #pragma comment(lib, "Rpcrt4.lib")
- constexpr unsigned char * ToRpcStr(char *str)
- {
- return reinterpret_cast<unsigned char*>(str);
- }
- int main(int argc, char **argv)
- {
- using namespace std;
- using fmt::format;
- if (argc != 4)
- {
- cout << format("usage: {} serv-ip username passord", argv[0]) << endl;
- return 1;
- }
- try {
- auto login = argv[2];
- auto password = argv[3];
- auto ip_addr = argv[1];
- // create string binding handle
- unsigned char* szStringBinding = nullptr;
- auto status = RpcStringBindingCompose(
- NULL,
- ToRpcStr("ncacn_ip_tcp"),
- ToRpcStr(ip_addr),
- ToRpcStr("9191"),
- NULL,
- &szStringBinding);
- if (status)
- {
- throw runtime_error(format("creating string binding handle {}", status));
- }
- // create binding handle
- status = RpcBindingFromStringBinding(
- szStringBinding,
- &hDoRPCBinding);
- if (status)
- {
- throw runtime_error(format("creating binding handle {}", status));
- }
- RpcStringFree(&szStringBinding);
- // Setting auth info
- SEC_WINNT_AUTH_IDENTITY auth_info{};
- auth_info.User = ToRpcStr(login);
- auth_info.UserLength = strlen(login);
- auth_info.Password = ToRpcStr(password);
- auth_info.PasswordLength = strlen(password);
- auth_info.Domain = nullptr;
- auth_info.DomainLength = 0;
- auth_info.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
- status = RpcBindingSetAuthInfo(
- hDoRPCBinding,
- nullptr,
- RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
- RPC_C_AUTHN_GSS_KERBEROS,
- 0,
- 0);
- if (status)
- {
- throw runtime_error(format("Binding auth info {}", status));
- }
- auto call_it = []() {
- RpcTryExcept
- {
- const unsigned char szMsg[] = "Client: I Can RPC Now!";
- Show(szMsg);
- }
- RpcExcept(1)
- {
- std::cout << "Runtime exception occurred: " << RpcExceptionCode() << std::endl;
- }
- RpcEndExcept
- };
- call_it();
- status = RpcBindingFree(&hDoRPCBinding);
- if (status)
- {
- throw runtime_error(format("freeing binding handle {}", status));
- }
- }
- catch (exception &e)
- {
- cout << "Error: " << e.what() << endl;
- }
- 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 reinterpret_cast<void*>(new char[size]());
- }
- // Memory deallocation function for RPC.
- void __RPC_USER midl_user_free(void* p)
- {
- delete[] p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement