码迷,mamicode.com
首页 > 其他好文 > 详细

apache通过cgi调用exe程序

时间:2014-09-24 16:06:37      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   strong   

windows下,使用c写了一个简单的cgi程序,生成exe类型的可执行文件,代码如下:

1 #include<stdio.h>
2 int main()
3 {
4     printf("Content-Type:text/html\n\n");   
5     printf("Hello,world!\n");
6     return 0;
7 }

怎样使apache加载生成的exe文件?只需要两步即可实现:

1、配置cgi程序所在目录

2、使apache能该识别exe文件

对应到apache的配置文件分别为:

A:

<IfModule alias_module>代码块中增加:

ScriptAlias /cgi-bin/ "E:/c/"

红色字体为cgi程序(此处指生成的exe文件)所在的目录

注意:需要修改两处,在该代码块下方不远处也有一个地方需要修改,如下:

1 <Directory "E:/c">
2     AllowOverride None
3     Options None
4     Order allow,deny
5     Allow from all
6 </Directory>

 

B:

<IfModule mime_module>代码块中修改或者增加如下两行:

AddType text/html .exe
AddHandler cgi-script .exe .cgi

假如生成的exe文件名为:test.exe

此时将test.exe拷贝到E:\C目录下,并重启apache。

浏览器中运行http://localhost/cgi-bin/test.exe,此时若看到Hello,world!则说明,配置成功。

 

扩展:

在c语言中接受get、post数据,范例如下:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 int main()
 5 {
 6     char *data;
 7     long m,n;
 8     
 9     printf("Content-Type:text/html\n\n");
10     printf("<title>c语言生成html</title>");   
11  
12     data = getenv("QUERY_STRING");
13     if(NULL == data)
14         printf("<p>Error.</p>");
15     else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!= 2)
16         printf("<p>Please input number.</p>");
17     else
18         printf("<p>m:%ld;n:%ld.</p>",m,n);
19     return 0;
20 }

 

apache通过cgi调用exe程序

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址:http://www.cnblogs.com/qbyyqhcz/p/3990658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!