I have a c++ application code which I have attached below . I want to call the dll functions from a c# application . I am stuck with function pointers and explicit calling of function pointers using getProcAdress(). Please suggest a way
- #include
- #include
- #include
-
-
- typedef unsigned long long(__stdcall *init)(int);
- typedef int(__stdcall *serial)(unsigned long long, unsigned short *, unsigned short *);
- typedef int(__stdcall *close)(unsigned long long);
- typedef int(__stdcall *read2SWITCH)(unsigned long long handle, unsigned char two_switch, unsigned char * presence,
- unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * status, unsigned char * origin);
- typedef int(__stdcall *readROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char * presence,
- unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * err_code,
- unsigned char * processing, unsigned char * position);
- typedef int(__stdcall *setROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char position, unsigned char direction);
- int main(int argc, char *argv[])
- {
-
- HINSTANCE ProcDLL_ESS;
- #ifdef _WIN64
- ProcDLL_ESS = LoadLibrary(TEXT("ess_c_64.dll"));
- #else
- ProcDLL_ESS = LoadLibrary(TEXT("ess_c_32.dll"));
- #endif
-
- init dll_init;
- serial dll_serial;
- close dll_close;
- read2SWITCH dll_read2SWITCH;
- readROTSWITCH dll_readROTSWITCH;
- setROTSWITCH dll_setROTSWITCH;
-
- dll_init = (init)GetProcAddress(ProcDLL_ESS, "ess_initialization");
- dll_serial = (serial)GetProcAddress(ProcDLL_ESS, "ess_get_serial");
- dll_close = (close)GetProcAddress(ProcDLL_ESS, "ess_close");
- dll_read2SWITCH = (read2SWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_two_switch");
- dll_readROTSWITCH = (readROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_rot_switch");
- dll_setROTSWITCH = (setROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_set_rot_switch");
-
- unsigned long long essHandle = 0;
- unsigned short Serial = 0;
- unsigned short Version = 0;
- unsigned char position = 0;
- unsigned char two_switch = 0;
- unsigned char rot_switch = 0;
-
- unsigned char Presence;
- unsigned char Type;
- unsigned char Model;
- unsigned char Software_Version;
- unsigned char Status;
- unsigned char Origin;
- unsigned char Err_code;
- unsigned char Processing;
- unsigned char Position;
- unsigned int localCount = 0;
- if (ProcDLL_ESS != NULL) {
- std::cout << "ESS dll loaded" << std::endl;
-
- if (dll_init != NULL) {
-
- essHandle = dll_init(0);
- std::cout << "ESS session initialized" << std::endl;
- }
-
- if (dll_serial != NULL) {
-
- dll_serial(essHandle, &Serial, &Version);
- std::cout << "SWITCH BOARD SN: " << Serial << std::endl;
- }
-
- if (dll_read2SWITCH != NULL){
- dll_read2SWITCH(essHandle, two_switch, &Presence, &Type, &Model, &Software_Version, &Status, &Origin);
- if ((Presence == 1) && (Type == 1)){
- std::cout << std::endl << "2-SWITCH detected." << std::endl;
- std::cout << "Model (1 if 2-SWITCH): " << int(Model) << std::endl;
- std::cout << "Software version: " << int(Software_Version) << std::endl;
- if (Status == 1){
- std::cout << "Switch is ON." << std::endl;
- }
- else{ std::cout << "Switch is OFF." << std::endl; }
- }
- else{ std::cout << "No 2-SWITCH found on 1st position." << std::endl; }
- }
-
- if (dll_readROTSWITCH != NULL){
- dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
- if ((Presence == 1) && (Type == 0)){
- std::cout << std::endl << "ROT-SWITCH detected." << std::endl;
- std::cout << "Model (1: M-SWITCH; 3: L-SWITCH): " << int(Model) << std::endl;
- std::cout << "Software version: " << int(Software_Version) << std::endl;
- std::cout << "Switch position: " << int(Position) << std::endl;
-
- if (Position == 0){
- dll_setROTSWITCH(essHandle, rot_switch, 1, 0);
- }
- else{
- dll_setROTSWITCH(essHandle, rot_switch, 0, 0);
- }
- std::cout << "ROT-SWITCH position changed!" << std::endl;
-
- do {
- dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
- Sleep(100);
- } while (Processing != 1);
- Sleep(100);
-
- do{
- dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
- Sleep(100);
- } while (Processing == 1);
- Sleep(100);
-
- dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);
- std::cout << "New Switch position: " << int(Position) << std::endl;
- }
- else{ std::cout << "No ROT-SWITCH found on position 'A'." << std::endl; }
- }
-
- if (dll_close != NULL) {
- dll_close(essHandle);
- std::cout << std::endl << "ESS session closed" << std::endl;
- }
- }
-
- FreeLibrary(ProcDLL_ESS);
- std::cout << "ESS dll unloaded" << std::endl;
-
- system("PAUSE");
- return EXIT_SUCCESS;
- }