Solved

Sending binary data from CGI in Windows

Posted on 1999-01-06
8
262 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:dnavarro
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:dnavarro
Comment Utility
Adjusted points to 200
0
 
LVL 1

Author Comment

by:dnavarro
Comment Utility
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
Comment Utility
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
Comment Utility
This works, although it's not precisely what I was looking for.  But I can take it from here.  Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
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…
The viewer will learn how to dynamically set the form action using jQuery.

771 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

12 Experts available now in Live!

Get 1:1 Help Now