提问者:小点点

Dllimport在visual studio上工作,但不在iis服务器上工作


你好,我有一个我开发的dll。我在使用dll导入的网站中使用此dll。当我通过visual studio运行网站时,一切都正常。但当我用iis运行它时,它没有错误。请参阅所附代码。谢谢在这里输入图像描述


共1个答案

匿名用户

网站的文件/文件夹相对于该特定网站的主目录。由于DllImport的工作方式,可以将所需目录添加到PATH环境变量中,然后只指定文件名。

尝试以下方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Hosting;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace WebAppRunDLLTest
{
    public partial class _default : System.Web.UI.Page
    {
        [DllImport("dapreporter.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr fndapreporter(string inifile, int nReport, int nYear, int nMonth, int nDay, int nType, int nCode, int precision);

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //get path to bin directory
                //string binDir = Server.MapPath("~/bin/");
                string binDir = HostingEnvironment.MapPath("~/bin/");

                //add bin directory to PATH so DLLImport can find the DLL
                Environment.SetEnvironmentVariable("PATH", String.Format("{0};{1}", Environment.GetEnvironmentVariable("PATH"), binDir));

                string iniFile = File.ReadAllText(Path.Combine(binDir, "WinDAP.ini"));

                IntPtr pVariant = fndapreporter(iniFile, 0, 2021, 9, 12, 0, 197, 0);

                object sHtml = Marshal.GetObjectForNativeVariant(pVariant);

                Marshal.FreeCoTaskMem(pVariant);
                pVariant = IntPtr.Zero;
                Response.Write(sHtml.ToString());
            }
            catch (Exception ex)
            {
                Response.Write("ERROR: " + ex.Message);
            }
        }
    }
}

资源:

  • 使用非静态路径从DLL调用函数