您的位置:首页 > 编程语言 > C语言/C++

C#调用C语言写的dll,并发布web服务

2014-08-12 12:48 507 查看
创建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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: