Share via


CHttpServerContext::WriteClient

BOOL WriteClient( LPVOID lpvBuffer**, LPDWORD** lpdwBytes**, DWORD** dwReserved = 0 );

Return Value

Nonzero if successful, otherwise 0. If the call fails, the Windows function may be called to determine the cause of the error.

Parameters

lpvBuffer

Pointer to the buffer where the data is to be written.

lpdwBytes

Pointer to a DWORD that holds the number of characters to write from the buffer referenced by Buffer.

dwReserved

Reserved for future use.

Remarks

Call this member function to send information to the HTTP client immediately. For example, use WriteClient to send an error message.

Example

This example demonstrates how to use WriteClient to immediately send binary data from the ISAPI extension. The example opens a binary file, sets the appropriate Context-Type header, and sends it to the browser. m_bSendHeaders is set to FALSE so that MFC won't add any types of headers.

This ISAPI extension can be used to stream binary data directly to the browser on the HTML page.

<HTML>.
You will see image here:.
<IMG SRC="/scripts/myisapi.dll?header=image/gif&file=c:\temp\myfile.gif" >
</HTML>

// Parse map: indicates that the default function takes two name/value
// pairs: a content-type header that will be generated by the ISAPI
// extension and a path to the local binary file.
BEGIN_PARSE_MAP(CSendBin5Extension, CHttpServer)
   // TODO: insert your ON_PARSE_COMMAND() and
   // ON_PARSE_COMMAND_PARAMS() here to hook up your commands.
   // For example:

   ON_PARSE_COMMAND(Default, CSendBin5Extension, ITS_PSTR ITS_PSTR)
   ON_PARSE_COMMAND_PARAMS("file header")

   DEFAULT_PARSE_COMMAND(Default, CSendBin5Extension)
END_PARSE_MAP(CSendBin5Extension)

void CSendBin5Extension::Default(CHttpServerContext* pCtxt,
   LPTSTR szFile, LPTSTR szHeader)
{
   CHAR szHeaders [256];
   // Use headers would be supplied via name/value pair to the default.
   // They would be in this form: image/gif.
   wsprintf (szHeaders, "Content-Type: %s\r\n\r\n", szHeader);
   DWORD dwRead ;
   DWORD dwSize = lstrlen (szHeaders);
   VOID * pBuff;

   // Turn off sending header by MFC.
   pCtxt->m_bSendHeaders = FALSE;

   // Send our own headers.
   if (!pCtxt->ServerSupportFunction (HSE_REQ_SEND_RESPONSE_HEADER,
      NULL, &dwSize, (LPDWORD ) szHeaders))
   {
      // Report an error.
      ISAPITRACE1 ("ServerSupportFunction failed: %d\n", GetLastError());
      return;
   }

   // Open local file. File name would be supplied as a parameter to
   // the default in this form: c:\temp\myfile.gif
   HANDLE hFile = CreateFile (szFile, GENERIC_READ, FILE_SHARE_READ,
      (LPSECURITY_ATTRIBUTES) NULL,
      OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, (HANDLE) NULL);
   if (hFile == INVALID_HANDLE_VALUE)
   {
      ISAPITRACE1 ("CreateFile error: %d\n", GetLastError());
      return;
   }

   CHAR szBuffer [2048];
   do
   {
      // read chunk of the file
      if (!ReadFile (hFile, szBuffer, 2048, &dwRead, NULL))
      {
         ISAPITRACE1 ("ReadFile error: %d\n", GetLastError());
         return;
      }
      if (!dwRead)
         // EOF reached, bail out
         break;

      // Send binary chunk to the browser
      if (!pCtxt->WriteClient( szBuffer, &dwRead, 0))
      {
         ISAPITRACE1 ("WriteClient error: %d\n", GetLastError());
         return;
      }
      ISAPITRACE ("Buffer sent\n");
   }
   while (1);
   CloseHandle (hFile);
   return;
}

CHttpServerContext OverviewClass MembersHierarchy Chart

See Also   CHttpServerContext::ReadClient