Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sending binary data from CGI in Windows

Posted on 1999-01-06
8
Medium Priority
?
312 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
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

 
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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

670 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