创建ashx文件:
<%@ WebHandler Language="C#" Class="wgtochina" %>
using System;
using System.Web;
using System.Runtime.InteropServices;
public class wgtochina : IHttpHandler
{
[DllImport("casmapi.dll", EntryPoint = "wgtochina", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int do_wgtochina(int iFirst, int iProjType, int iZoneNum, double dCentMedian, double x, double y, ref double outX, ref double outY);
public void ProcessRequest(HttpContext context)
{
int a = 99;
double outX = 0;
double outY = 0;
try
{
int iFirst = System.Int32.Parse(context.Request.Params["iFirst"]);
int iProjType = System.Int32.Parse(context.Request.Params["iProjType"]);
int iZoneNum = System.Int32.Parse(context.Request.Params["iZoneNum"]);
double dCentMedian = System.Double.Parse(context.Request.Params["dCentMedian"]);
double x = System.Double.Parse(context.Request.Params["x"]);
double y = System.Double.Parse(context.Request.Params["y"]);
a = do_wgtochina(iFirst, iProjType, iZoneNum, dCentMedian, x, y, ref outX, ref outY);
}
catch (Exception ex)
{
context.Response.ContentType = "text/plain";
context.Response.Write(ex.ToString());
return;
}
string result = "";
switch (a)
{
case -1:
result = "无授权";
break;
case -2:
result = "授权文件异常";
break;
case -3:
result = "授权文件过期";
break;
case -4:
result = "加密狗异常";
break;
case 0:
result = "输入参数异常";
break;
case 1:
result = "正常";
break;
default:
result = "未知错误";
break;
}
string json = "{\"status\":\"" + result + "\",\"outX\":" + outX + ",\"outY\":" + outY + "}";
context.Response.ContentType = "text/plain";
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
}
32位dll放在C:\Windows\SysWOW64目录下,确保这个dll依赖的其他dll也在这个目录下
启用IIS7的32位兼容性
打开IIS管理器,点应用程序池
再点右边的"设置应用程序池默认设置"
再点启用32位应用程序,将false改成true
确定后就生效了
还有就是IIS用户要有对C:\temp目录的读写权限
还有就是要将.net和IIS关联起来在cmd下执行aspnet_regiis.exe –i
本文介绍了如何在C#中调用C语言编写的dll,并在IIS7上发布Web服务。关键步骤包括:确保32位dll放置在C:WindowsSysWOW64目录及依赖项齐全,启用IIS7的32位应用程序支持,授予IIS用户对C: emp目录的读写权限,以及通过aspnet_regiis.exe命令关联.NET和IIS。
787

被折叠的 条评论
为什么被折叠?



