Link to home
Start Free TrialLog in
Avatar of manx
manx

asked on

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
Avatar of chensu
chensu
Flag of Canada image

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.
Avatar of manx
manx

ASKER

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.
Avatar of manx

ASKER

Nope changing the WriteFile to a cout does not affect the outcome. The gateway error still occurs.

Regards

ManX
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");
Avatar of manx

ASKER

Well,

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

Regards

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

WriteCGI("Content-Type: text/html\n\n<html></html>");
Avatar of manx

ASKER

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
Avatar of manx

ASKER

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
Avatar of manx

ASKER

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
Avatar of manx

ASKER

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
Avatar of manx

ASKER

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
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.
Avatar of manx

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ianB
ianB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial