home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / primcuts.zip / DosLoadString / DosLoadString.c next >
Text File  |  2000-06-30  |  3KB  |  96 lines

  1. /*---------------------------------------------------------------------------*\
  2.  *    Title: DosLoadString                                                   *
  3.  * Filename: DosLoadString.c                                                 *
  4.  *     Date: 1999-01-20                                                      *
  5.  *   Author: Jan M. Danielsson (jan.m.danielsson@telia.com)                  *
  6.  *                                                                           *
  7.  *  Purpose: Load a resource string using the CP API.                        *
  8.  *                                                                           *
  9.  * ToDo:                                                                     *
  10.  *   - In an application, the stringbundles should be cached.                *
  11.  *     DosFreeResource() should not be called for every time a single string *
  12.  *     is loaded.                                                            *
  13.  *   - Errorchecking in DosLoadString(). DosFreeResource() returnvalue is    *
  14.  *     is not handled.                                                       *
  15.  *                                                                           *
  16.  * Known bugs:                                                               *
  17.  *   - None, yet                                                             *
  18. \*---------------------------------------------------------------------------*/
  19. #define INCL_DOSRESOURCES                /* DosGetResource()        */
  20. #define INCL_DOSMEMMGR                   /* DosAllocMem()           */
  21. #define INCL_DOSERRORS                   /* Error-value definitions */
  22.  
  23. #include <os2.h>
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #include "res.h"
  29.  
  30.  
  31. /*
  32.  * Function prototypes
  33.  */
  34. APIRET APIENTRY DosLoadString(HMODULE hMod, ULONG ulID, PSZ pszBuf, ULONG cbBuffer);
  35.  
  36.  
  37. int main(void)
  38. {
  39.    APIRET   rc = 0;
  40.    char     pszString[64] = "";
  41.  
  42.    if((rc = DosLoadString((HMODULE)0, IDS_APPNAME, pszString, sizeof(pszString))) != NO_ERROR)
  43.    {
  44.       printf("Error: DosLoadString()/DosGetResource() returned: %d\n", rc);
  45.       return 1;
  46.    }
  47.  
  48.    printf("String ID %d is '%s'.\n", IDS_APPNAME, pszString);
  49.  
  50.    return 0;
  51. }
  52.  
  53.  
  54. APIRET APIENTRY DosLoadString(HMODULE hMod, ULONG ulID, PSZ pszBuf, ULONG cbBuffer)
  55. {
  56.    APIRET   rc = 0;
  57.    PSZ      pszTmp = NULL;
  58.    PSZ      p = NULL;                    /* Temporary pointer used when parsing string */
  59.    long     lCount = 0;
  60.    long     i = 0;
  61.    long     lTmp = 0;
  62.  
  63.    /*
  64.     * When retriving strings with DosGetResource(), it actually loads a
  65.     * bundle of strings (up to 16 strings in each). Strings are
  66.     * stored in bundles of 16 in the resource file.
  67.     */
  68.    if((rc = DosGetResource(hMod, RT_STRING, (ulID/16)+1, (PPVOID)&pszTmp)) != 0)
  69.    {
  70.       return rc;
  71.    }
  72.  
  73.    p = pszTmp;
  74.    p += 2;        /* Ignore the stringtable codepage in this demonstration */
  75.  
  76.    /*
  77.     * Set the pointer to the location of the string we want to retrive
  78.     * in the bunble we got with DosGetResource().
  79.     */
  80.    lCount = ulID % 16;
  81.    while(i < lCount)
  82.    {
  83.       lTmp = (BYTE)p[0];
  84.       p += (lTmp+1);
  85.       i++;
  86.    }
  87.    p++;
  88.  
  89.    /* Copy the resourcestring outputbuffer */
  90.    strncpy(pszBuf, (const char *)p, cbBuffer);
  91.  
  92.    DosFreeResource((PVOID)pszTmp);
  93.  
  94.    return NO_ERROR;
  95. }
  96.