Example of usage
#include <windows.h> #include <iostream> #include <string>
typedef int (*era_process_request)(const char* request, char** response); typedef void (*era_free)(char* s); typedef int (*era_init_lib)(); typedef void (*era_deinit_lib)();
int main(int argc, char** argv) { HMODULE hMod = LoadLibrary(L"ServerApi.dll"); //1 if (!hMod) { std::cout<< "Cannot load library error. Last error is:" << GetLastError() << std::endl; return 1; }
era_init_lib init_lib = (era_init_lib)GetProcAddress(hMod,"era_init_lib"); //2 if (!init_lib ) { std::cout<<"Cannot init library" << std::endl; FreeLibrary(hMod); //9 return 1; } era_deinit_lib deinit_lib = (era_deinit_lib)GetProcAddress(hMod,"era_deinit_lib"); //7 if (!deinit_lib ) { std::cout<<"Cannot deinit libraries" << std::endl; FreeLibrary(hMod); //9 return 1; }
int res = init_lib(); //3 if(res) { std::cout<<"Init lib result:" << res << std::endl; FreeLibrary(hMod); //9 return 1; }
era_process_request send_request = (era_process_request)GetProcAddress(hMod,"era_process_request");//4 if (!send_request) { std::cout<<"Cannot load era_process_request" << std::endl; deinit_lib(); //8 FreeLibrary(hMod); //9 return 1; }
era_free free_response = (era_free)GetProcAddress(hMod,"era_free");//5 if (!free_response) { std::cout<<"Cannot load era_free" << std::endl; deinit_lib(); //8 FreeLibrary(hMod); //9 return 1; } while (true) { std::string request;// = "{\"Era.ServerApi.StartRequest\":{}}"; std::cout<<">"; std::getline(std::cin, request);
if (request == "quit") {; break; }
std::cout<<"Executing json ..." << std::endl; char* szRes = NULL; int res = send_request(request.c_str(),&szRes); //6 if (szRes) std::cout<<szRes<<std::endl; free_response(szRes); //6 }
std::cout<<"Exiting ... " << std::endl;
deinit_lib(); //8
FreeLibrary(hMod); //9
std::cout<<"Library freed ... " << std::endl;
return 0; } |