Varun Douzer

Varun Douzer

  • NA
  • 38
  • 2.5k

C++ DLL import in c#

Nov 12 2019 12:16 AM
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
  1. #include  
  2. #include  
  3. #include  
  4. //#include "ess_c.h"  
  5. /* Define functions prototype */  
  6. typedef unsigned long long(__stdcall *init)(int);  
  7. typedef int(__stdcall *serial)(unsigned long long, unsigned short *, unsigned short *);  
  8. typedef int(__stdcall *close)(unsigned long long);  
  9. typedef int(__stdcall *read2SWITCH)(unsigned long long handle, unsigned char two_switch, unsigned char * presence,  
  10. unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * status, unsigned char * origin);  
  11. typedef int(__stdcall *readROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char * presence,  
  12. unsigned char * type, unsigned char * model, unsigned char * soft_vers, unsigned char * err_code,  
  13. unsigned char * processing, unsigned char * position);  
  14. typedef int(__stdcall *setROTSWITCH)(unsigned long long handle, unsigned char rot_switch, unsigned char position, unsigned char direction);  
  15. int main(int argc, char *argv[])  
  16. {  
  17. /* Load DLL into memory */  
  18. HINSTANCE ProcDLL_ESS;  
  19. #ifdef _WIN64  
  20. ProcDLL_ESS = LoadLibrary(TEXT("ess_c_64.dll"));  
  21. #else  
  22. ProcDLL_ESS = LoadLibrary(TEXT("ess_c_32.dll"));  
  23. #endif  
  24. /* Declare pointers on dll functions */  
  25. init dll_init;  
  26. serial dll_serial;  
  27. close dll_close;  
  28. read2SWITCH dll_read2SWITCH;  
  29. readROTSWITCH dll_readROTSWITCH;  
  30. setROTSWITCH dll_setROTSWITCH;  
  31. /* Link dll pointers with functions prototype */  
  32. dll_init = (init)GetProcAddress(ProcDLL_ESS, "ess_initialization");  
  33. dll_serial = (serial)GetProcAddress(ProcDLL_ESS, "ess_get_serial");  
  34. dll_close = (close)GetProcAddress(ProcDLL_ESS, "ess_close");  
  35. dll_read2SWITCH = (read2SWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_two_switch");  
  36. dll_readROTSWITCH = (readROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_get_data_rot_switch");  
  37. dll_setROTSWITCH = (setROTSWITCH)GetProcAddress(ProcDLL_ESS, "ess_set_rot_switch");  
  38. /* Define variables used for ESS */  
  39. unsigned long long essHandle = 0;  
  40. unsigned short Serial = 0;  
  41. unsigned short Version = 0;  
  42. unsigned char position = 0;  
  43. unsigned char two_switch = 0; // 2-SWITCH position (0 is the 1st on the SWITCHBOARD)  
  44. unsigned char rot_switch = 0; // ROT-SWITCH position (0 is the 1st, position 'A')  
  45. /* SWITCH variables definition used by the shared library */  
  46. unsigned char Presence;  
  47. unsigned char Type;  
  48. unsigned char Model;  
  49. unsigned char Software_Version;  
  50. unsigned char Status;  
  51. unsigned char Origin;  
  52. unsigned char Err_code;  
  53. unsigned char Processing;  
  54. unsigned char Position;  
  55. unsigned int localCount = 0;  
  56. if (ProcDLL_ESS != NULL) { // If dll loaded  
  57. std::cout << "ESS dll loaded" << std::endl;  
  58. /* Initialize device */  
  59. if (dll_init != NULL) { // Check if function was properly linked to the dll file  
  60. /* Initialize the first SWITCHBOARD in Windows enumeration list */  
  61. essHandle = dll_init(0);  
  62. std::cout << "ESS session initialized" << std::endl;  
  63. }  
  64. /* Read device serial number */  
  65. if (dll_serial != NULL) {  
  66. /*Get the serial number of the SWITCHBOARD*/  
  67. dll_serial(essHandle, &Serial, &Version);  
  68. std::cout << "SWITCH BOARD SN: " << Serial << std::endl;  
  69. }  
  70. /* Get status of the 2-SWITCH (N°1 on the board) */  
  71. if (dll_read2SWITCH != NULL){  
  72. dll_read2SWITCH(essHandle, two_switch, &Presence, &Type, &Model, &Software_Version, &Status, &Origin);  
  73. if ((Presence == 1) && (Type == 1)){ // If 2-SWITCH detected  
  74. std::cout << std::endl << "2-SWITCH detected." << std::endl;  
  75. std::cout << "Model (1 if 2-SWITCH): " << int(Model) << std::endl; // Display model  
  76. std::cout << "Software version: " << int(Software_Version) << std::endl; // Display software version  
  77. if (Status == 1){ // Display 2-SWITCH status  
  78. std::cout << "Switch is ON." << std::endl;  
  79. }  
  80. else{ std::cout << "Switch is OFF." << std::endl; }  
  81. }  
  82. else{ std::cout << "No 2-SWITCH found on 1st position." << std::endl; }  
  83. }  
  84. /* Get status of the ROT-SWITCH (N°A on the board) */  
  85. if (dll_readROTSWITCH != NULL){  
  86. dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);  
  87. if ((Presence == 1) && (Type == 0)){ // If SWITCH detected  
  88. std::cout << std::endl << "ROT-SWITCH detected." << std::endl;  
  89. std::cout << "Model (1: M-SWITCH; 3: L-SWITCH): " << int(Model) << std::endl; // Display model  
  90. std::cout << "Software version: " << int(Software_Version) << std::endl; // Display software version  
  91. std::cout << "Switch position: " << int(Position) << std::endl; // Display ROT-SWITCH position  
  92. /* Change ROT-SWITCH position */  
  93. if (Position == 0){ // SWITCH changes from position 1 to 0 or from 0 to 1  
  94. dll_setROTSWITCH(essHandle, rot_switch, 1, 0);  
  95. }  
  96. else{  
  97. dll_setROTSWITCH(essHandle, rot_switch, 0, 0);  
  98. }  
  99. std::cout << "ROT-SWITCH position changed!" << std::endl;  
  100. /* Wait for processing flag to be active */  
  101. do {  
  102. dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);  
  103. Sleep(100);  
  104. while (Processing != 1);  
  105. Sleep(100);  
  106. /* Wait for processing flag to be down */  
  107. do{  
  108. dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);  
  109. Sleep(100);  
  110. while (Processing == 1);  
  111. Sleep(100);  
  112. // Read new position after change  
  113. dll_readROTSWITCH(essHandle, rot_switch, &Presence, &Type, &Model, &Software_Version, &Err_code, &Processing, &Position);  
  114. std::cout << "New Switch position: " << int(Position) << std::endl; // Display ROT-SWITCH position  
  115. }  
  116. else{ std::cout << "No ROT-SWITCH found on position 'A'." << std::endl; }  
  117. }  
  118. /* Close ESS session */  
  119. if (dll_close != NULL) {  
  120. dll_close(essHandle);  
  121. std::cout << std::endl << "ESS session closed" << std::endl;  
  122. }  
  123. }  
  124. /* Release the DLL */  
  125. FreeLibrary(ProcDLL_ESS);  
  126. std::cout << "ESS dll unloaded" << std::endl;  
  127. /* Exit application */  
  128. system("PAUSE");  
  129. return EXIT_SUCCESS;  
  130. }  

Attachment: Cpp_ESS.rar

Answers (1)