PrfWriteProfileData - Example Code

This function writes, and subsequently reads, the profile data associated with application SAMPLE.EXE.

/* Write and read binary data to a profile.  Some return code */
/* checking omitted for brevity.                              */

#define  INCL_WINSHELLDATA
#define  INCL_WINERRORS
#define  INCL_DOSERRORS
#include <os2.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE  7

INT main(VOID) {

    HAB     hab          = NULLHANDLE;         /* Window handle   */
    HINI    hini         = NULLHANDLE;         /* INI file handle */
    PSZ     pszFileName  = "PROFILE.INI";      /* Name of profile */
    BOOL    rc           = FALSE;              /* Return code     */
    PSZ     pszAppName   = "SAMPLE.EXE";       /* Application name */
    PSZ     pszKeyName   = "BINARY_DATA";      /* Data key name    */
    APIRET  ret          = NO_ERROR;           /* API return code  */
    PBYTE   pData        = NULL;               /* Pointer to data buffer */
    ULONG   ulDataSize   = 0;                  /* Size of data to read   */
    INT     idx          = 0;                  /* Loop index             */

    hab = WinInitialize( 0 );
    hini = PrfOpenProfile( hab, pszFileName );   /* Open INI file */

      /* Allocate Memory for binary data to be written and read */

    ret = DosAllocMem( (PPVOID)&pData, sizeof(BYTE)*80,
                       (ULONG)PAG_COMMIT | PAG_READ | PAG_WRITE );

        /* Initialize binary data and then write it to profile */

    for(idx = 0; idx < BUFSIZE; idx++) {
        pData[idx] = idx+65;
    }

    rc = PrfWriteProfileData( hini, pszAppName, pszKeyName,
                             (PVOID)pData, (ULONG)BUFSIZE );

    if(rc == FALSE) {
        printf("PrfWriteProfileData error code: %X\n", WinGetLastError(hab));
        return 1;
    }

       /* Put junk in buffer so we can see if read is successful */

    for(idx = 0; idx < BUFSIZE; idx++) {
        pData[idx] = idx;
    }

        /* Retrieve size of data and then get data */

    rc = PrfQueryProfileSize( hini, pszAppName, pszKeyName, &ulDataSize );
    rc = PrfQueryProfileData( hini, pszAppName, pszKeyName,
                             (PVOID)pData, &ulDataSize);

    if(rc==FALSE) {
        printf("PrfQueryProfileData error code: %X\n", WinGetLastError(hab));
        return 1;
    }

    printf("Profile Data Read:");
    for(idx=0; idx < ulDataSize; idx++)
        printf("%c", pData[idx]);
    printf("\n");

    PrfCloseProfile(hini);     /* Close profile  */
    DosFreeMem(pData);         /* Free memory    */
    return NO_ERROR;           /* Phone home <g> */

}


[Back: PrfWriteProfileData - Related Functions]
[Next: PrfWriteProfileData - Topics]