home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / DLLMIX.PAK / CTOBC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.2 KB  |  154 lines

  1. //----------------------------------------------------------------------------
  2. // Mixing BC++ built 32-bit DLL's with non-BC applications.
  3. // Copyright (c) 1996 by Borland International, All Rights Reserved
  4. //
  5. // This application tests calling into a 32-bit DLL built with Borland C++. The
  6. // code has been conditionalized to be built with either Borland or Microsoft
  7. // tools and was verified with Borland C++ 5.0 and Microsoft Visual C++ 4.1.
  8. //----------------------------------------------------------------------------
  9. #include <windows.h>
  10. #include <stdio.h>      // stdio.h
  11. #include "CToBC.rh"
  12. #include "bcdll.h"      // functions from bcdll.dll
  13.  
  14. BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wP, LPARAM lP);
  15.  
  16. const char testDll[] = "BCDLL.DLL";
  17.  
  18. #if defined(__BORLANDC__)
  19.    const char toolSet[] = "Borland C++";
  20. #elif defined(_MSC_VER)
  21.    const char toolSet[] = "Visual C++";
  22. #else
  23.    const char toolSet[] = "Unknown tool set";
  24. #endif
  25.  
  26. bool            (__stdcall *bcMessage)(const char * msg);
  27. long             (__cdecl   *bcMultiply)(short s, int i);
  28. long double    (__stdcall *bcAverage)(double d, float f);
  29. void             (__cdecl   *bcExceptionTest)(void);
  30. void             (__stdcall *bcUnhandledException)(void);
  31.  
  32. ///////////////////////////////////////////////////////////////////////////////
  33. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int)
  34.     {
  35.    //
  36.    // Load the Dynamic Link LIbrary bcdll.dll
  37.    //
  38.    HINSTANCE hLib = LoadLibrary(testDll);
  39.    if((unsigned)hLib<=HINSTANCE_ERROR)
  40.        {
  41.       char bfr[40];
  42.       wsprintf(bfr, "Failure loading library: %s", testDll);
  43.       MessageBox(NULL, bfr, toolSet, MB_OK|MB_APPLMODAL);
  44.       return 1;
  45.       }
  46.  
  47.    //
  48.    // Get all the entry points for the functions we wish to test. Typedef's can
  49.    // be used to simplify casting the retrun value from GetProcAddress.
  50.    //
  51.    bcMessage = (bool(__stdcall*)(const char*)) GetProcAddress(hLib, "Message");
  52.    bcMultiply = (long(__cdecl*)(short, int)) GetProcAddress(hLib, "_Multiply");
  53.    bcAverage = (long double(__stdcall*)(double, float)) GetProcAddress(hLib, "Average");
  54.    bcExceptionTest = (void(__cdecl*)()) GetProcAddress(hLib, "_ExceptionTest");
  55.    bcUnhandledException = (void(__stdcall*)()) GetProcAddress(hLib, "UnhandledException");
  56.  
  57.    //
  58.    // GetProcAddress returns null on failure
  59.    //
  60.    if( bcMessage == NULL || bcMultiply == NULL || bcAverage == NULL
  61.            || bcExceptionTest == NULL || bcUnhandledException == NULL)
  62.         {
  63.       MessageBox(NULL, "Failure locating procedures.", toolSet,
  64.             MB_OK|MB_APPLMODAL);
  65.       return 1;
  66.       }
  67.  
  68.    //
  69.    // Create a modal dialog as the application main window.
  70.    //
  71.     DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);
  72.  
  73.    if((unsigned)hLib>HINSTANCE_ERROR)
  74.        FreeLibrary(hLib);
  75.    return 0;
  76.    }
  77.  
  78. //
  79. // Data used by DlgProc
  80. //
  81. static short s;
  82. static int i;
  83. static long lAns;
  84. static float f;
  85. static double d;
  86. static long double ldAns;
  87. static char bfr [100];
  88.  
  89. //
  90. // Main window procedure
  91. //
  92. BOOL APIENTRY DlgProc(HWND hDlg, UINT msg, WPARAM wP, LPARAM /* lP */)
  93.     {
  94.    switch (msg)
  95.        {
  96.       case WM_INITDIALOG:
  97.          sprintf(bfr, "Calling from %s to a Borland C++ DLL", toolSet);
  98.          SetWindowText(hDlg, bfr);
  99.           return true;
  100.  
  101.       case WM_COMMAND:
  102.           switch(wP)
  103.              {
  104.               case IDC_BTN_MESSAGE:   // Verify pointer and buffer handling
  105.                sprintf(bfr, "A message from %s", toolSet);
  106.                bcMessage(bfr);
  107.                return true;
  108.  
  109.               case IDC_BTN_MULTIPLY:  // Verify calling convention match
  110.                s = 55;
  111.                 i = -1024;
  112.                  lAns = bcMultiply(s, i);
  113.                sprintf(bfr, "%d * %d = %ld", s, i, lAns);
  114.                MessageBox(hDlg, bfr, toolSet, MB_OK|MB_APPLMODAL);
  115.                 return true;
  116.  
  117.               case IDC_BTN_AVERAGE:   // Verify handling of real numbers
  118.                 d = 6232.001;
  119.                f = 128.327;
  120.                 ldAns = bcAverage(d, f);
  121.                sprintf(bfr, "The average of %5f and %5f is %5Lf", d, f, ldAns);
  122.                   MessageBox(hDlg, bfr, toolSet, MB_OK|MB_APPLMODAL);
  123.                 return true;
  124.  
  125.               case IDC_BTN_EXCEPTION: // Verify EH isolated in DLL
  126.                 bcExceptionTest();
  127.                 return true;
  128.  
  129.               case IDC_BTN_UNHANDLED: // Verify handling exceptions from the DLL
  130.                 try{  // The DLL will throw a C++ exception for type char *
  131.                     bcUnhandledException();
  132.                   }
  133.                catch(char *msg)  // A BC-compiled app will recognize the type
  134.                   {
  135.                   sprintf(bfr, "Application caught exception thrown from DLL: %s", msg);
  136.                   MessageBox(hDlg, bfr, toolSet, MB_OK|MB_APPLMODAL);
  137.                   }
  138.                catch(...)  // A non-BC app will catch an un-typed exception
  139.                    {
  140.                   MessageBox(hDlg, "Application caught exception thrown from DLL",
  141.                           toolSet, MB_OK|MB_APPLMODAL);
  142.                   }
  143.                 return true;
  144.  
  145.               case IDOK:
  146.               case IDCANCEL:
  147.                 EndDialog(hDlg, 0);
  148.                 return true;
  149.             }
  150.          break;
  151.       }
  152.    return false;
  153.     }
  154.