Advertisement
Guest User

main.cpp

a guest
May 14th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "TestInterface.h"
  4.  
  5. // Simplified version of the logging function.
  6. void logOneLine (const char *clr, const char *msg) {
  7.     static FILE *log    = 0;
  8.     log         = fopen("TestDLL.log", log ? "at" : "wt");
  9.     fprintf     (log, "%s: %s\n", clr, msg);
  10.     fclose      (log);
  11. }
  12. // Macro logging to ease reading the code.
  13. #define Log     logOneLine(__func__, buff)
  14.  
  15. int main (void) {
  16.     char    buff[255];
  17.     memset              (buff, 0, 255*sizeof(char));
  18.     Log;
  19.  
  20.     HMODULE hDll        = LoadLibrary(L"TestLib.dll");
  21.     while (1) {
  22.         if (hDll) {
  23.             // You might want to replace mangled names below with the right ones after compiling on your system.
  24.             Instanciator        instanciator            = (Instanciator)    GetProcAddress(hDll,    "_Z11Instanciatebi");
  25.             Destructor          destructor              = (Destructor)      GetProcAddress(hDll,    "_Z7DestroyP13TestInterface");
  26.             if (!instanciator || !destructor) {
  27.                 sprintf         (buff, "Unable to retrieve factory function pointers.");
  28.                 Log;
  29.                 break;
  30.             }
  31.  
  32.             int numinstances    = 0;
  33.             printf              ("\nEnter number of iterations:");
  34.             if (scanf("%d", &numinstances)!=1)
  35.                 break;
  36.  
  37.             sprintf             (buff, "Number of iterations: %d.", numinstances);
  38.             Log;
  39.             if (numinstances<1 || numinstances > 40)
  40.                 break;
  41.  
  42.             TestInterface       **ptrs                  = new TestInterface*[numinstances];
  43.             if (!ptrs)
  44.                 break;
  45.             memset              (ptrs, 0, sizeof(TestInterface*)*numinstances);
  46.  
  47.             for (int i=0; i<numinstances; ++i) {
  48.                 sprintf         (buff, "Iteration #%d...", i);
  49.                 Log;
  50.                 bool    bb;
  51.                 int     ii;
  52.                 ptrs[i]        = instanciator((i % 2)==0, i);
  53.                 if (!ptrs[i])
  54.                     break;
  55.                 if (ptrs[i]->get(bb, ii)) {
  56.                     sprintf(buff, "Got bb=%s, ii=%d.", bb ? "true" : "false", ii);
  57.                     Log;
  58.                 }
  59.             }
  60.             for (int i=0; i<numinstances; ++i) {
  61.                 destructor      (ptrs[i]);
  62.                 ptrs[i]         = 0;
  63.             }
  64.             delete[]            ptrs;
  65.         }
  66.         break;
  67.     }
  68.     if (hDll) {
  69.         FreeLibrary         (hDll);
  70.         hDll                = 0;
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement