1

I have a DLL that I have developed. I use this DLL in a website using DllImport.

When I run the website via Visual Studio all is okay, but when I run it with the IIS it is stuck with no errors.

Here's my code:

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.Runtime.InteropServices;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        [DllImport("D:\\WebApplication1\\WebApplication1\\bin\\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
            {
                string iniFile = File.ReadAllText(@"D:\WebApplication1\WebApplication1\bin\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);
            }
        }
    }
}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24

1 Answers1

0

A website's files/folders are relative to the home directory for that particular website. Due to the way DllImport works, one can add the desired directory to the PATH environment variable and then specify only the filename.

Try the following:

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 WebApplication1
{
    public partial class WebForm1 : 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)
        {
            IntPtr pVariant = IntPtr.Zero;

            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"));

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

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

Resources:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24