Public Group

#### Archived

This topic is now archived and is closed to further replies.

# CGI in C++

This topic is 5813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am starting on my website and I want to try to encorporate CGI scripts. I have been told that you can use C++ for CGI scripts but I can't see how to go about doing so.
1. Is there a header for CGI and what is it?
2. Would I use "QUERY_STRING" to get the string from the url?
3. How would I return the data to the HTML page?
4. What format should the script be in?
For the last question I think you'd need to compile it but I thought to myself "would a .exe or a .dll actually work". Also I need guidance for making a CGI script in VC++6.0 although if it were easier I could just write raw code (without a project) and use VC++ to compile. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

## And the Dark Lord shall DESTROY Middle Earth

###### And Ilthigore will save you
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
P.S I KNOW perl is better for CGI scripting but I'd rather not learn ANOTHER programming language right now. [edited by - ilthigore on August 16, 2002 3:20:31 AM] [edited by - ilthigore on August 16, 2002 3:21:13 AM]

##### Share on other sites
look into ISAPI Extension DLLs if the web server is Windows or IIS Compatible, you can just code everything in C++

##### Share on other sites
quote:

1.)Is there a header for CGI and what is it?

There are libraries that will help you do some of the more common CGI tasks with C++, but core C++ has no direct support for working with CGI per say.

quote:

2.)Would I use "QUERY_STRING" to get the string from the url?

Depends on how you send your data to the script (Post vs. Get). One of them passes the querystring as an argument to your application (so you can retrieve it through the argv argument to main) and one requires you to use GetEnvironmentVariable (or a similar function). Sorry I can't remember which is which (it's been a while since I've had to do a CGI script in C++).

quote:

3.)How would I return the data to the HTML page?

Using something like cout. (The default output stream sends whatever information you send it on to the client -- as I recall...someone correct me if I'm wrong).

quote:

3.)What format should the script be in?

Whatever format the web server uses. If you plan to run it on a Linux machine, then you'll need to compile it on a Linux box. (Usually the admins at your ISP will compile a script for you if you send them the project and a makefile...you'll have to ask them about specifics like compiler and such).

quote:

For the last question I think you'd need to compile it but I thought to myself "would a .exe or a .dll actually work".

DLL's are usually written as an extension to the web server rather than a simple CGI script (also they would only work in a Windows environment). Most scripts are an executable binary file compiled for the platform the server runs on.

quote:

Also I need guidance for making a CGI script in VC++6.0 although if it were easier I could just write raw code (without a project) and use VC++ to compile.

Stick with the core C++ functions (unless you're sure you'll be running your application on a Windows box...you may be able to get away with API specific calls then). Just make sure you make a windows console application, not a standard windows application. As I said before most of the time the ISP will have to compile your script for you (unless you have a shell account). Use whatever IDE you're most comfortable with, just be sure you know whatever headers, etc you use are also available to the compiler that will ultimately be used on your source code.

HTH

[edited by - SysOp_1101 on August 16, 2002 3:47:44 AM]

##### Share on other sites
Ilthigore:

Most CGI programming these days is done in Perl. There are probably a few books out there that have C/C++ in them, but I would say that 90% are Perl-only. I have a copy of CGI Programming on the World Wide Web , by O'Reilly and Associates, which does have some C/C++ examples. You might be able to find it used. If you get desperate, we can probably work out a deal, since I don't really use it and I have the 2nd edition which is Perl-only.

John.

[edited by - JohnAD on August 16, 2002 2:29:35 PM]

##### Share on other sites
quote:
Original post by Ilthigore
P.S I KNOW perl is better for CGI scripting but I''d rather not learn ANOTHER programming language right now.

I know this isn''t what you want to hear, but learning to write CGI in C/C++ isn''t really the best way to spend your time. While perl may be a little better, both of them are extremely outdated ways to do web programming (kinda like writing a game in pure ASM). They require overly complex code to do simple things and can make your site virtually unmaintainable.

I would reccommend either learing how to run JSP under Tomcat or PHP under Apache. Both are far easier, safer, and more robust than C++ CGI scripts.

##### Share on other sites
Hmm, sounds like "CGI Internet Programming with C++ and C"
by Mark Felton is the book you want to read.

Kami no Itte ga ore ni zettai naru!

##### Share on other sites
quote:
Original post by Ilthigore
1. Is there a header for CGI and what is it?

Not one I''m aware of, I have always lived without it.
quote:
2. Would I use "QUERY_STRING" to get the string from the url?

If REQUEST_METHOD is GET you should use QUERY_STRING, if it is POST you should read from stdin or using cin.

  char* GetQuery(){   char* cgi_query = NULL;   char* env_var = getenv("REQUEST_METHOD");   if (!env_var)   {      throw "400 REQUEST_METHOD not set.";   }   if (!strcmp(env_var, "GET"))   {      char* query_string = getenv("QUERY_STRING");      if (!query_string)      {         throw "400 REQUEST_METHOD is GET, but QUERY_STRING is not set.";      }      cgi_query = new char[strlen(query_string) + 1];      strcpy(cgi_query, query_string);   }   else   {      int total = 0;      int content_length = atoi(getenv("CONTENT_LENGTH"));      cgi_query = new char[content_length + 1];      while (total < content_length)      {         int bytes_read = read(0, cgi_query + total, content_length);         if (bytes_read <= 0)         {            throw "400 read() returned 0.";         }         total += bytes_read;      }      cgi_query[content_length] = ''\0'';   }   return cgi_query;}

quote:
3. How would I return the data to the HTML page?

Output it to stdout or using cout. Remember to print "Content-Type: text/html\n\n" to stdout/cout before anything else.
quote:
4. What format should the script be in?

You should compile it on the target machine.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002065
×