Solved

Sending binary data from CGI in Windows

Posted on 1999-01-06
8
279 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to get a list of users From AD 13 136
How can i reset all images size to default in a word file 6 68
Example unit tests with AngularJS 3 45
How can i open a .fxp file 3 29
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article will show, step by step, how to integrate R code into a R Sweave document
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now