home *** CD-ROM | disk | FTP | other *** search
/ Tutto per Internet / Internet.iso / soft95 / Html / Server / Samples / IS2WCGI / Test / IS2WCGI.C next >
Encoding:
C/C++ Source or Header  |  1995-12-04  |  3.3 KB  |  131 lines

  1. // is2wcgi.c - a quick test app
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4.  
  5. LPSTR glpProfile;
  6.  
  7. void GetCgiProfileKey (LPSTR lpszSection, LPSTR lpszKey, LPSTR lpszBuf, DWORD dwSize);
  8. void WriteString (HANDLE hFile, LPSTR lpsz);
  9. void HtmlWriteText (HANDLE hFile, LPSTR lpsz);
  10.  
  11. int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  12.     {
  13.     char szOutputFileName[MAX_PATH];
  14.     HANDLE hOutput;
  15.     HANDLE hProfile;
  16.     BYTE byBuf[1025];
  17.     DWORD dwLen;
  18.  
  19.     // Profile name is passed on command line
  20.     glpProfile = lpCmdLine;
  21.  
  22.     // Get output file name
  23.     GetCgiProfileKey ("System", "Output File", szOutputFileName, MAX_PATH);
  24.  
  25.     hOutput = CreateFile (szOutputFileName,
  26.                           GENERIC_WRITE,
  27.                           0,
  28.                           NULL,
  29.                           OPEN_EXISTING,
  30.                           FILE_ATTRIBUTE_NORMAL,
  31.                           NULL);
  32.  
  33.     if (hOutput == INVALID_HANDLE_VALUE)
  34.         {
  35.         MessageBox (NULL, "IS2WCGI Test App - Invalid output file", NULL, MB_OK);
  36.         return (0);
  37.         }
  38.  
  39.     WriteString (hOutput, "Content Type: text/html\r\n\r\n");
  40.     WriteString (hOutput, "<HTML><HEAD>\r\n");
  41.     WriteString (hOutput, "<TITLE>Output of WCGITEST</TITLE>\r\n");
  42.     WriteString (hOutput, "</HEAD><BODY>\r\n");
  43.     WriteString (hOutput, "<H1>Profile Sent to Windows CGI App</H1>\r\n");
  44.     WriteString (hOutput, "<PRE>\r\n");
  45.  
  46.     hProfile = CreateFile (glpProfile,
  47.                           GENERIC_READ,
  48.                           0,
  49.                           NULL,
  50.                           OPEN_EXISTING,
  51.                           FILE_ATTRIBUTE_NORMAL,
  52.                           NULL);
  53.  
  54.     if (hProfile == INVALID_HANDLE_VALUE)
  55.         HtmlWriteText (hOutput, "Unable to open profile\r\n");
  56.     else
  57.         {
  58.         do    {
  59.             if (!ReadFile (hProfile, byBuf, 1024, &dwLen, NULL))
  60.                 {
  61.                 HtmlWriteText (hOutput, "Error reading from profile\r\n");
  62.                 break ;
  63.                 }
  64.  
  65.             byBuf[dwLen] = 0;
  66.             if (dwLen)
  67.                 HtmlWriteText (hOutput, byBuf);
  68.             } while (dwLen == 1024);
  69.         }
  70.  
  71.  
  72.     WriteString (hOutput, "</PRE></BODY></HTML>\r\n");
  73.     CloseHandle (hOutput);
  74.     CloseHandle (hProfile);
  75.  
  76.     return (0);
  77.     }
  78.  
  79. void GetCgiProfileKey (LPSTR lpszSection, LPSTR lpszKey, LPSTR lpszBuf, DWORD dwSize)
  80.     {
  81.     GetPrivateProfileString    (lpszSection, lpszKey, "", lpszBuf, dwSize, glpProfile);
  82.     }
  83.  
  84. void WriteString (HANDLE hFile, LPSTR lpsz)
  85.     {
  86.     DWORD dwRead;
  87.     if (!WriteFile (hFile ,lpsz, lstrlen (lpsz), &dwRead, NULL))
  88.         MessageBox (NULL, "IS2WCGI Test App - Cannot write to output file", NULL, MB_OK);
  89.     }
  90.  
  91. void HtmlWriteText (HANDLE hFile, LPSTR lpsz)
  92.     {
  93.     char szBuf[2048];
  94.     int nLen;
  95.     int i;
  96.  
  97.     //
  98.     // Build up enough data to make call to WriteString
  99.     // worthwhile; convert special chars too.
  100.     //
  101.     nLen = 0;
  102.     szBuf[0] = 0;
  103.     for (i = 0 ; lpsz[i] ; i++)
  104.         {
  105.         if (lpsz[i] == '<')
  106.             lstrcpy (&szBuf[nLen], "<");
  107.         else if (lpsz[i] == '>')
  108.             lstrcpy (&szBuf[nLen], ">");
  109.         else if (lpsz[i] == '&')
  110.             lstrcpy (&szBuf[nLen], "&");
  111.         else if (lpsz[i] == '\"')
  112.             lstrcpy (&szBuf[nLen], """);
  113.         else
  114.             {
  115.             szBuf[nLen] = lpsz[i];
  116.             szBuf[nLen + 1] = 0;
  117.             }
  118.  
  119.         nLen += lstrlen (&szBuf[nLen]);
  120.  
  121.         if (nLen >= 1024)
  122.             {
  123.             WriteString (hFile, szBuf);
  124.             nLen = 0;
  125.             }
  126.         }
  127.  
  128.     if (nLen)
  129.         WriteString (hFile, szBuf);
  130.     }
  131.