home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / string / wrprofst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.0 KB  |  56 lines

  1. /*
  2.  * This program demonstrates the use of WriteProfileString(). In this
  3.  * example, the string "fubar=yes" keyword and value is written to an
  4.  * application name called "foo".  If this construct currently exists
  5.  * in the win.ini file, it is reported to the user. Note! This program
  6.  * makes permanent changes to the WIN.INI file, but should not affect
  7.  * any existing applications.
  8.  */
  9.  
  10. #include <windows.h>
  11.  
  12. #define CHARLEN  4
  13.  
  14. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  15. HANDLE hInstance, hPrevInstance;
  16. LPSTR  lpszCmdLine;
  17. int    nCmdShow;
  18. {
  19.    char  szFooStr[5];
  20.    int   iCopied;
  21.    int   iResult;
  22.    BOOL  bResult;
  23.  
  24.    iCopied = GetProfileString((LPSTR)"foo", (LPSTR)"fubar", (LPSTR)"ERR",
  25.                               (LPSTR)szFooStr, CHARLEN);
  26.  
  27.    if (iCopied == NULL)
  28.       MessageBox(GetFocus(), (LPSTR)"Error in Checking WIN.INI",
  29.                  (LPSTR)"GetProfileString()", MB_OK);
  30.    else if ((szFooStr[0] == 'y') &&
  31.             (szFooStr[1] == 'e') &&
  32.             (szFooStr[2] == 's'))
  33.       MessageBox(GetFocus(), (LPSTR)"Profile String Currently Exists!",
  34.                  (LPSTR)"GetProfileString()", MB_OK);
  35.  
  36.    else {
  37.       MessageBox(GetFocus(),
  38.                  (LPSTR)"Inserting 'fubar=yes' into [foo] section of WIN.INI.",
  39.                  (LPSTR)"WriteProfileString()", MB_OK);
  40.       iResult = MessageBox(GetFocus(),
  41.                            (LPSTR)"Continuing Will Change WIN.INI Contents!",
  42.                            (LPSTR)"WriteProfileString()",
  43.                            MB_OKCANCEL | MB_ICONEXCLAMATION);
  44.       if (iResult != IDCANCEL) {
  45.          bResult = WriteProfileString((LPSTR)"foo", (LPSTR)"fubar",
  46.                                       (LPSTR)"yes");
  47.          if (bResult == NULL)
  48.             MessageBox(GetFocus(), (LPSTR)"Error In Writing To WIN.INI!",
  49.                        (LPSTR)"WriteProfileString() Error!",
  50.                        MB_OK | MB_ICONEXCLAMATION);
  51.          }
  52.       }
  53.    
  54.    return(0);
  55. }
  56.