[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

CGI error

I am tryng to write CGI routines for the first time in VC++ .

There seems to be a problem with the write function. Is the following code correct ?

extern "C" _declspec(dllexport) void WriteCGI(CString stext)
{
      
      HANDLE hconsole;
      DWORD NumberOfBytesWritten;

      //      Get handle to std output device
      hconsole = GetStdHandle(STD_OUTPUT_HANDLE);

      //      Write to standard device
      WriteFile (hconsole,&stext,stext.GetLength(),&NumberOfBytesWritten,NULL);
}

The function produces the proper output when run locally on a MS-Dos console.
When running on the IIS, the function returns no response. The error is http 502 invalid gateway


Is it trying to write to a wrong port ?

Thanks and Regards

ManX
0
manx
Asked:
manx
1 Solution
 
chensuCommented:
You need to output the following header first.

"Content-Type: text/html\n\n"

You also need to output the HTML tags, such as <HTML></HMTL>, <BODY></BODY>, ...

And, you don't have to use WriteFile. Using cout is much easier.
0
 
manxAuthor Commented:
The content headers are all outputed first; i.e

WriteCGI("Content-type: text/vnd.wap.wml\n\r");
WriteCGI("<?xml version= \"1.0\" ?>\n\r");
WriteCGI("<!DOCTYPE wml PUBLIC>\n\r");
WriteCGI("<wml>\n\r");
WriteCGI("<card id=""cardname"">\n\r");
WriteCGI("      <p>\n\r");
WriteCGI("WRITECGI successful\n\r");
WriteCGI("      </p>\n\r ");
WriteCGI("</card>\n\r");
WriteCGI("</wml>\n\r");

It still does not work.

The cout/WriteFile should not make a difference. But will try it out any how.
0
 
manxAuthor Commented:
Nope changing the WriteFile to a cout does not affect the outcome. The gateway error still occurs.

Regards

ManX
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.

 
chensuCommented:
There must be two new lines after the Content-Type header.

WriteCGI("Content-type: text/vnd.wap.wml\r\n\r\n");

or

WriteCGI("Content-type: text/vnd.wap.wml\n\n");
0
 
manxAuthor Commented:
Well,

The problem still persists after adding the 2 \r\n chars at the end of the content type string.

Regards

ManX
0
 
chensuCommented:
Try the following simplest line to see if there is still an error message.

WriteCGI("Content-Type: text/html\n\n<html></html>");
0
 
manxAuthor Commented:
After changing the sample app to WriteCGI("Content-Type: text/html\n\n<html></html>");

It still does not work. When running the exe file off the IIs from the browser, the error message recieved is
"CGI Error
The specified CGI application misbehaved by not returning a complete set of HTML headers. The headers returned include :"

Thus it does not seem to be returning any thing at all. Would it be because it is writing to the wrong port or the file transfer mode has to be set to binary etc....

Regards

ManX
0
 
manxAuthor Commented:
After changing the sample app to WriteCGI("Content-Type: text/html\n\n<html></html>");

It still does not work. When running the exe file off the IIs from the browser, the error message recieved is
"CGI Error
The specified CGI application misbehaved by not returning a complete set of HTML headers. The headers returned include :"

Thus it does not seem to be returning any thing at all. Would it be because it is writing to the wrong port or the file transfer mode has to be set to binary etc....

Regards

ManX
0
 
manxAuthor Commented:
After changing the sample app to WriteCGI("Content-Type: text/html\n\n<html></html>");

It still does not work. When running the exe file off the IIs from the browser, the error message recieved is
"CGI Error
The specified CGI application misbehaved by not returning a complete set of HTML headers. The headers returned include :"

Thus it does not seem to be returning any thing at all. Would it be because it is writing to the wrong port or the file transfer mode has to be set to binary etc....

Regards

ManX
0
 
manxAuthor Commented:
Just another comment,

All the above code when run on the local machine during development. Seems to run just fine; i.e the sample console app outputs all the required strings to the MS DOS window.

Regards

ManX
0
 
manxAuthor Commented:
Just another comment,

All the above code when run on the local machine during development. Seems to run just fine; i.e the sample console app outputs all the required strings to the MS DOS window.

Regards

ManX
0
 
szetoaCommented:
I don't think your problem is with the code, but with the permission on your web server.  First you have to make sure you can execute a .exe program (use web manager to check if the directory housing your .exe has 'execute' access).  Then you have to make sure any library your program needs is also accessable (check permissions on those directories).  I sugguest you write a simple c program with a couple getenv() just to display back the following information:

PATH USERNAME

Hope this helps.
0
 
manxAuthor Commented:
Managed to solve the problem. Turns out the prob existed as the transfre mode was set to binary not ascii text.

Thanks one and all for all the comments/advice given

Regards and Happy Y2K

ManX
0
 
ianBCommented:
Hi,

I am answering this question so it can be saved as a PAQ. The user wishes to award the two experts 50 points each for the help provided

"However I would like to allocate 50pts to both chensu and szetoa. Both with good grades"

Each expert will find a question for them to answer in this topic area.

ian
Community Support @ Experts EXchange
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now