Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sending binary data from CGI in Windows

Posted on 1999-01-06
8
Medium Priority
?
317 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
  • 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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 600 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

916 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