﻿<%@ Page Language="C#" debug="true" %>
<%@ Import namespace="System" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Threading" %>
<%@ Import namespace="System.Runtime.InteropServices" %>
<script language="C#" runat="server">

public class DllInvoke 
{
	//操作系统函数
	[DllImport("kernel32.dll")]
	private extern static IntPtr LoadLibrary(String path);
	[DllImport("kernel32.dll")]
	private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);
	[DllImport("kernel32.dll")]
	private extern static bool FreeLibrary(IntPtr lib);

	//委托声明
	private delegate int DllGetActiveServices( );
	private delegate IntPtr DllOpenReportService(IntPtr CreatePara);
	private delegate void DllCloseService(IntPtr h);
	private delegate IntPtr Dllfunc(IntPtr h, IntPtr funcname, IntPtr para);

	public DllInvoke(String DLLPath) {
		//加载dll
		m_hService = IntPtr.Zero;
		m_hLib = LoadLibrary(DLLPath);
		if(m_hLib == IntPtr.Zero) return;

		//定位入口函数地址
		m_getactiveservices = (DllGetActiveServices)GetInvoke("GetActiveServices", typeof(DllGetActiveServices));
		m_openservice = (DllOpenReportService)GetInvoke("OpenReportService", typeof(DllOpenReportService));
		m_closeservice = (DllCloseService)GetInvoke("CloseService", typeof(DllCloseService));
		m_func = (Dllfunc)GetInvoke("func", typeof(Dllfunc));
		if(m_getactiveservices==null || m_openservice==null || m_closeservice==null || m_func==null) {
			//定位失败
			FreeLibrary(m_hLib);  
			m_hLib = IntPtr.Zero;
		}
	}
	~DllInvoke() {
		CloseService( );
	}

	//当前活动的服务数
	public int GetActiveServices() {
		return m_getactiveservices();
	}
	//打开服务
	public bool OpenReportService(string CreatePara) {
		if(m_hService != IntPtr.Zero) return true;	//已经打开着
		if(m_hLib == IntPtr.Zero) return false;
		IntPtr h1 = Marshal.StringToHGlobalUni(CreatePara);
		m_hService = m_openservice(h1);
		Marshal.FreeHGlobal(h1);
		return (m_hService == IntPtr.Zero) ? false : true;
	}
	//关闭服务
	public void CloseService() {
		if(m_hService != IntPtr.Zero) {
			m_closeservice(m_hService);
			m_hService = IntPtr.Zero;
		}
	}
	//调用函数
	public string func(string funcname, string para) {
		if(m_hService == IntPtr.Zero) return "";
		//参数 ==> Unicode串指针地址
		IntPtr h1 = Marshal.StringToHGlobalUni(funcname);
		IntPtr h2 = Marshal.StringToHGlobalUni(para);
		//调用
		IntPtr nRet = m_func(m_hService, h1, h2);
		//释放参数内存
		Marshal.FreeHGlobal(h1);
		Marshal.FreeHGlobal(h2);
		return Marshal.PtrToStringUni(nRet);
	}

	//private====================
	private Delegate GetInvoke(String APIName,Type t) {
		IntPtr api = GetProcAddress(m_hLib, APIName);
		return (Delegate)Marshal.GetDelegateForFunctionPointer(api,t);
	}
	//DLL句柄
	public IntPtr m_hLib;
	//入口函数地址
	private DllGetActiveServices m_getactiveservices;
	private DllOpenReportService m_openservice;
	private DllCloseService m_closeservice;
	private Dllfunc m_func;
	//服务句柄
	private IntPtr m_hService;
};



	public void Page_Load(Object sender, EventArgs e) {
		String pageFunc = Request.Params["func"];
		if(pageFunc == null) return;
		Response.ContentType = "text/plain";

		//报表服务中 http 访问的的相对URL
		String BaseDir = "http://localhost/supcan/";
		//最终生成文件的手机访问的URL目录
		String TempURL = "http://localhost/supcan/dotnet/temp/";

		//winface.dll所在目录 (硕正组件的绝对目录)
		String WinFacePathname = Server.MapPath("SupcanDLL\\winface.dll");

		//加载winface.dll
		DllInvoke dll = new DllInvoke(WinFacePathname);
		if(dll.m_hLib == IntPtr.Zero) {
			Response.Write("dll加载失败");
			Response.End();
			return;
		}

		//防止潮水攻击
		while(dll.GetActiveServices( ) > 25) {
			Thread.Sleep(400);
/*			Response.Write("服务器忙，请稍后再试1");
			Response.End();
			return;*/
		};

		//Temp 目录的绝对路径
		String TempDir = Server.MapPath("Temp");

		//创建报表服务
		if(dll.OpenReportService("LogSize=1000;LogLevel=0;TempDir=" +TempDir+ ";BaseDir=" + BaseDir) == false) {
			Response.Write("服务创建失败");
			Response.End();
			return;
		}

		//指定中心数据源的URL(相对于服务创建参数中的BaseDir)
		dll.func("SetSource", "reportdata/datacenter.xml");

		//确定报表文件名
		String ReportFilename;
		if(pageFunc == "c1")
			ReportFilename = "report/report1.xml";
		else if(pageFunc == "c2")
			ReportFilename = "report/center2-3.xml";
		else if(pageFunc == "c3")
			ReportFilename = "report/center3.xml";
		else if(pageFunc == "c4")
			ReportFilename = "report/xmlsheet7.xml";
		else if(pageFunc == "c5")
			ReportFilename = "report/xmlsheet3.xml";
		else if(pageFunc == "c6")
			ReportFilename = "report/htmltable1.xml";
		else if(pageFunc == "c7")
			ReportFilename = "report/center7.xml";
		else if(pageFunc == "c8")
			ReportFilename = "report/barcode.xml";
		else if(pageFunc == "c9")
			ReportFilename = "report/xmlsheet6.xml";
		else {
			Response.End();
			return;
		}

		//报表服务：打开文件、计算
		dll.func("build", ReportFilename);
		if(pageFunc == "c6")
			dll.func("setSource", "ds1 \r\n http://news.163.com/rank/");
		dll.func("calc", "");

		//报表转换成html
		String ext = "htm";
		String TempFilename = dll.func("CacheDirUtility", "isCreateTempFile=true;ext=" + ext);
		if(TempFilename=="") {
			dll.CloseService();
			Response.Write("服务器忙，请稍后再试3.");
			Response.End();
			return;
		}

		dll.func("callfunc", "105\r\n type=" +ext+ "; filename=" + TempFilename);
		//关闭服务
		dll.CloseService();

		//返回html文件的URL==========
		//取得不含路径的文件名
		int n = TempFilename.LastIndexOf('\\');
		TempFilename = TempFilename.Substring(n+1);
		//拼接成URL
		TempURL += TempFilename;

		Response.Redirect(TempURL);
		Response.End();
	}
</script>