home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / call32.zip / dll.c next >
C/C++ Source or Header  |  1992-08-15  |  3KB  |  75 lines

  1. /**************************************************************
  2. *                                                                                  *
  3. *    Sample 32-bit code callable from a 16 bit application.     *
  4. *    There is a bug in the IBM C Set/2 compiler (PMR 6x060)     *
  5. *    that does not properly handle pointer thunks from 16-bit   *
  6. *    code.  This 32-bit sample shows the work around.           *
  7. *                                                             *
  8. *    Author: David Moskotiwz, Productivity Solutions            *
  9. *                                                               *
  10. *    Thanks to Hae Shung Ju and Dave Mooney of IBM to find      *
  11. *    the work-around.                                           *
  12. *                                                               *
  13. **************************************************************/
  14.  
  15. #define INCL_DOS
  16. #define INCL_NOPMAPI
  17. #include <os2.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. /*
  22. **    According to the documentation, all 16-bit pointers must be declared with
  23. **    the _Seg16 keyword.  Note the prototype below. We don't have to do
  24. **    anything with the non-pointer values (the 2nd parameter).
  25. **
  26. **    However, because the third parameter is a function, we have to
  27. ** use the _Seg16 to make sure the compiler generates the proper
  28. ** thunk.  THe example in the documentation (without the _Seg16, fails).
  29. */
  30.  
  31. int _Far16 _Pascal DoFiles(char * _Seg16, ULONG, void (* _Seg16 _Far16 _Pascal fn)(FILEFINDBUF3 * _Seg16 ));
  32.  
  33. int _Far16 _Pascal DoFiles(char * _Seg16 pattern, ULONG Attribute, void (* _Seg16  _Far16 _Pascal fn)(FILEFINDBUF3 * _Seg16 ))
  34. {
  35.    FILEFINDBUF3 flist;
  36.    ULONG       count = 1;
  37.    HDIR        hdir = (HDIR) HDIR_CREATE;
  38.    int         NumberDone = 0;
  39.     APIRET        rc;
  40.  
  41.     /*
  42.     **    This is a bit of magic.  The compiler "knows" that _pattern_
  43.     **    is a 16-bit pointer.  However, it does not generate the pointer
  44.     **    thunk unless we explicitly assign the contents to a 32-bit pointer.
  45.     */
  46.  
  47.     char     *pat = pattern;
  48.  
  49.  
  50.     printf("\nwe're here... in the 32 bit DLL\n");
  51. /*
  52. ** The following line fails for a couple of reasons, first, the printf
  53. ** family doesn't recognize the need to thunk the paramter, second, the
  54. ** compiler doesn't do it.
  55. **
  56. **    printf(" character string passed: %s \n", pattern);
  57. */
  58.     printf(" character string passed: %s \n", pat);
  59.  
  60.    rc = DosFindFirst(pat, &hdir, Attribute
  61.                     , &flist, sizeof(flist), &count, FIL_STANDARD);
  62.    if (!rc)
  63.    {
  64.       do
  65.       {
  66.         (*fn)(&flist);
  67.          NumberDone++;                // keep track of how many we did
  68.       }
  69.       while(!(DosFindNext(hdir, &flist, sizeof(flist),&count)) );
  70.    }
  71.  
  72.    DosFindClose(hdir);                // close the directory
  73.    return(NumberDone);                // let 'em know how many we did
  74. }
  75.