Solved

Sending binary data from CGI in Windows

Posted on 1999-01-06
8
302 Views
Last Modified: 2013-12-25
Okay, in a CGI application running in IIS 4.0 I am doing the following:

- Getting the standard output handle.
- SetConsoleMode(hOut, 0) to prevent "cooking" the binary data
- Sending the header "Content-type: image/jpeg\n" and "Content-length: 1841\n\n"
- Using WriteFile to send the file data

The JPEG file I am sending is 1841 bytes long.  When I send exactly 1841 bytes using WriteFile I get an error that not all data has been sent.  When I append an extra nul to the 1841 bytes, I get an "invalid image file" error.

Does anyone have any simple code for sending a binary file from a CGI application in IIS 4.0?
0
Comment
Question by:dnavarro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:jconde
ID: 1830499
what compiler are you using to compile your cgi?

The problem is that you're writing data to stdout, but stdout is defined as text (ascii) not binary.  What you need to do is configure stdout to binary.
0
 
LVL 1

Author Comment

by:dnavarro
ID: 1830500
I'm doing it in a BASIC dialect, using all Win32 API calls.

And as mentioned above, I called SetConsoleMode() to change from text mode to binary mode.  Is there something else I should call also?
0
 
LVL 7

Expert Comment

by:jconde
ID: 1830501
I can send you the code for something that does pretty much the same with gif format but it's written in C.

Just to make sure everything's working concerning the console mode, write a little application that does the same thing as "type" does for dos.  (Read a file and output it's contents to the standard output)

i.e.  type image.jpg > test.jpg
      your_program image.jpg > test.jpg

I really don't think the extra character is the problem.....if that was the problem, you should be able to see at least some part of the image.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:dnavarro
ID: 1830502
I have some C code which uses RTL routines for writing to the console.  What I really need is some C code that uses only Win32 API calls directly that can be compiled with VC++.  For that, I'd up the points to 200 if it worked.
0
 
LVL 1

Author Comment

by:dnavarro
ID: 1830503
Adjusted points to 200
0
 
LVL 1

Author Comment

by:dnavarro
ID: 1830504
Okay, anyone interested in 300 points?  Still looking for a small "C" program which uses the Windows API to open a binary file (JPEG, GIF, or ZIP) and output it through STDOUT to a web server.  No RTL calls, strictly Win32 API.
0
 

Accepted Solution

by:
CharanKumar earned 300 total points
ID: 1830505
void main()
{
char buffer[255]
DWORD bytes_written = 0;
strcpy(buffer,"Content-type: image/jpeg\n\n");
WriteFile(hStdOut, buffer, strlen(buffer), &bytes_written, NULL);
sprintf(image,"youimage.jpg");
HANDLE file_handle = CreateFile(image, GENERIC_READ,
            0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD file_size = GetFileSize(file_handle, NULL);
HANDLE mapped_file = CreateFileMapping(file_handle, NULL, PAGE_READONLY,
            0, file_size, NULL);
void* out_buffer = MapViewOfFile(mapped_file, FILE_MAP_READ, 0, 0, 0);        
WriteFile(hStdOut, (LPCVOID)out_buffer, file_size, &bytes_written, NULL);
        UnmapViewOfFile(out_buffer);
        CloseHandle(mapped_file);
        CloseHandle(file_handle);
}

This is a working code. You have to map the jpeg file and out the pointer returned by MapViewOfFile.
0
 
LVL 1

Author Comment

by:dnavarro
ID: 1830506
This works, although it's not precisely what I was looking for.  But I can take it from here.  Thanks.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question