Advertisement
dllbridge

months_2_00

Jul 2nd, 2023
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.91 KB | None | 0 0
  1.  
  2. //  1) The beginning of the file "main.cpp"
  3. //     - - - - - - - - - - - - - - - - - -
  4.  
  5. #include  <stdio.h>
  6. #include  "resource/Cdate.h"
  7.  
  8.  
  9.  
  10.  
  11. ////////////////////////////////////////////////////
  12. int main()                                        //
  13. {
  14.  
  15.     for(int i = 1; i <= 12; i++)
  16.     {
  17.        printf("%10s has %d days\n", Month[i].pszMon,
  18.                                     Month[i].nDays);
  19.     }
  20.  
  21.  
  22.  
  23. return 0;  
  24. }
  25.  
  26.  
  27.  
  28. //  2) The beginning of the file "Cdate.h"
  29. //    - - - - - - - - - - - - - - - - - -
  30.  
  31. #include   <windows.h>
  32.  
  33.  
  34.  
  35. /////////////////////////////////////////////////
  36. class Cdate
  37. {
  38.    
  39.     static  int  nCounter;                     // Будет подсчитывать и хранить кол-во созданных объектов - месяцев
  40.    
  41.     public:
  42.     int          nDays;                        //                                        Кол-во дней в этом месяце
  43.    
  44.     const char *pszMon;                        //                                                  Название месяца
  45.    
  46.    
  47.     ////////методы///////
  48.     Cdate();                                   //      Инициализирует 12 объектов класса параметрами 12-ти месяцев.
  49.    ~Cdate();                                   //      Деструктор пригодится для паузы перед завершением программы.
  50.  
  51. };
  52.  
  53.  
  54.  
  55. extern Cdate   Month[14];                      //                           Массив месяцев (объектов класса Cdate)  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //  3) The beginning of the file "Cdate.cpp"
  64. //     - - - - - - - - - - - - - - - - - -
  65.  
  66. #include   <stdlib.h>
  67. #include    <stdio.h>
  68. #include   <string.h>
  69. #include  <windows.h>
  70. #include    "Cdate.h"
  71.  
  72.  
  73. int         connDll();
  74.  
  75. extern int (*_retDate)(const char *psz, int n);
  76.  
  77. extern  HINSTANCE   hDll;
  78.  
  79. //////////////////////////////////////////////////////////
  80.  
  81.  
  82. int  Cdate::nCounter = -1;
  83.  
  84. Cdate    Month[14];                                                                                   //  12 месяцев
  85.  
  86.  
  87. //            Инициализирует 12 объектов класса параметрами 12-ти месяцев.
  88. /////////////////////////////////////////////////////////////////////////
  89. Cdate::Cdate()                                                         //
  90. {
  91.    
  92.        int i = ++ nCounter;
  93.              
  94.        if(i <= 12)
  95.        {
  96.           if(i == 0)
  97.          
  98.              connDll();                    //  Подключаем dll-библиотеку
  99.              
  100.    
  101.           Month[i].pszMon =  (const char*)_retDate("month", i);
  102.           Month[i].nDays  =               _retDate( "days", i);                  
  103.        }
  104. }
  105.  
  106.  
  107.  
  108. //            Деструктор пригодится для паузы перед завершением программы.
  109. /////////////////////////////////////////////////////////////////////////
  110. Cdate::~Cdate()                                                        //
  111. {
  112.     if(nDays == 28)    
  113.     {
  114.        printf(" - - - - - - - - - - -\n"); 
  115.        printf("%s destructor works.  \n", pszMon); 
  116.        system("pause");
  117.        
  118.        FreeLibrary(hDll);
  119.     }
  120.    
  121. }
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. //  4) The beginning of the file "connect.cpp"
  130. //    - - - - - - - - - - - - - - - - - - - -
  131.  
  132.  
  133. #include   <windows.h>
  134. #include     <stdio.h>
  135.  
  136.  
  137. HINSTANCE hDll;
  138.  
  139.  
  140.  
  141. int (*_retDate)(const char *psz, int n) = 0;
  142.  
  143.  
  144. ////////////////////////////////////////////////////
  145. int connDll()                                        //
  146. {
  147.  
  148.  
  149.     hDll = LoadLibrary("resource/dll/my.dll");
  150.    
  151.     if(hDll == 0)
  152.     {
  153.         printf("Error load of dll-library!\n");
  154.  
  155.         return 0;
  156.     }
  157.    
  158.    _retDate = (int(*)(const char *, int) )GetProcAddress(hDll, "_retDate");
  159.    
  160.    
  161.     if(_retDate == 0)
  162.     {
  163.         printf("Error load func a.\n");
  164.         FreeLibrary(hDll);
  165.         return 0;
  166.     }
  167.  
  168. return 0;
  169. }  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. //  5) The beginning of the file "dllmain.cpp"
  176. //    - - - - - - - - - - - - - - - - - - - -
  177.  
  178. // Replace "dll.h" with the name of your header
  179.  
  180. #include "dll.h"
  181. #include <windows.h>
  182.  
  183.  
  184. int  nDays[14] = {0, 31, 28, 31, 30, 31, 30,                           //     Кол-во дней в месяцах для невисокосного
  185.                      31, 31, 30, 31, 30, 31, 0};                       //                                      года.
  186.  
  187. const char *pszMonths[14] = {        "Zero",                           // The element with index 0 will not be used,
  188.                                   "January",
  189.                                  "February",
  190.                                     "March",
  191.                                     "April",
  192.                                       "May",
  193.                                      "June",
  194.                                      "July",
  195.                                    "August",
  196.                                 "September",
  197.                                   "October",
  198.                                  "November",
  199.                                  "December"
  200.                             };
  201.  
  202.  
  203.  
  204.  
  205. DllClass::DllClass()
  206. {
  207.  
  208. }
  209.  
  210.  
  211. DllClass::~DllClass ()
  212. {
  213.  
  214. }
  215.  
  216.  
  217. BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
  218.                        DWORD reason        /* Reason this function is being called. */ ,
  219.                        LPVOID reserved     /* Not used. */ )
  220. {
  221.     switch (reason)
  222.     {
  223.        
  224.       case DLL_PROCESS_ATTACH:
  225.                                 break;
  226.  
  227.       case DLL_PROCESS_DETACH:
  228.                                 break;
  229.  
  230.       case DLL_THREAD_ATTACH:
  231.                                 break;
  232.  
  233.       case DLL_THREAD_DETACH:
  234.                                 break;
  235.     }
  236.  
  237.     return TRUE;                                                          // Returns TRUE on success, FALSE on failure
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. ///////////////////////////////////////
  246. extern "C" __declspec(dllexport)
  247. ////////////////////////////////////////////////////////////
  248. int _retDate(const char *psz, int n)
  249. {
  250.    
  251.     if( strcmp("month", psz) == 0)  return (int)pszMonths[n];  
  252.     if( strcmp( "days", psz) == 0)  return          nDays[n];      
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement