none
Unable to load DLL access is denied (exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    Question

  • Hello,

    This question was originally asked on windows server forum but was suggested to be directed to this form.  original question here

    I'm consuming a 32-bit native DLL from a c# web service. Everything works fine locally in VS with IIS express. However when I deployed the site to a shared hosting provider (Server 2012 R2, IIS 7) the calls to the DLL (using platform invoke) generated an exception:

    Unable to load DLL 'dll_name.dll': Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    I asked the hosting provider to make sure my process is running in 32-bit mode and they assured me it is.

    Tech support also tried the following:

    Ran "tasklist /m dll_name.dll" It didn't return any processes that would lock the dll.
    Checked the permissions on the dll. The Network Service and app pool identity both had read/write/execute permissions. Just to make sure it wasn't permissions, they added IIS_IUSRS with r/w/x. Still didn't work.
    When this exception occurs, the exception.Message reads:

    Unable to load DLL 'dll_name.dll': Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    However, the exception.HRESULT value is 0x80131524 which it seems to be DLL not found. I'm not sure why there's a difference.

    If I delete the DLL from the /bin folder, I get the following message:

    Unable to load DLL 'dll_name.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

    I also tried the following:

    1- Replaced dll_name.dll with another native DLL from my bin folder, I got the same access denied error

    2- Replaced dll_name.dll with one of my windows DLLs (user32.dll), got the same access denied error

    3- Replaced dll_name.dll (in DllImport) with server user32.dll, got the following error: Unable to find an entry point named 'MyImportedFunction' in DLL 'user32.dll'.

    4- Replaced dll_name.dll with one of the managed DLLs from my bin folder, got the following error: Unable to find an entry point named 'MyImportedFunction' in DLL 'GR_Core.dll'

    The code that calls the DLL is as follows:

        public class DllCalls
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool SetDllDirectory(string lpPathName);
    
            [DllImport("Detection.Dll")]
            public static extern int DetectObject(IntPtr array, int Rows, int Cols, string BaseFolder);
        }
    
        public static class DetectionApis
        {
            public static int[] DetectRectangle(int[] LumaImage, int LumaStride, Rectangle Rect)
            {
                int DetPts = 0;
    
                string DstDll = System.Web.HttpContext.Current.Server.MapPath("~/bin");
                DllCalls.SetDllDirectory(DstDll);
    
                /*=================================================
                 * Extract region of interest rectangle
                 *================================================*/
                byte[] RoiRect = new byte[Rect.Width * Rect.Height];
                ImagePixels.ExtractRectToBytes(LumaImage, LumaStride, Rect, ref RoiRect);
    
                /*=================================================
                 * Pass array ByRef  
                 *================================================*/
                int ArraySize = RoiRect.Length;
                byte OneByte = 0;
                IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(OneByte) * ArraySize);
                Marshal.Copy(RoiRect, 0, buffer, RoiRect.Length);
    
                try
                {
                    string BaseFolder = System.Web.HttpContext.Current.Server.MapPath("~/cascades") + "\\";
                    DetPts = DllCalls.DetectObject(buffer, Rect.Height, Rect.Width, BaseFolder);
                }
                catch (Exception e)
                {
                    LogFile.WriteLogStrg("DetectObject() caused an exception: " + e.Message + "HRESULT = " + e.HResult.ToString("X8"));
                    return (null);
                }
    
                int[] DetPoints = null;
                if (DetPts == 8)
                {
                    DetPoints = new int[DetPts * 2];
                    Marshal.Copy(buffer, DetPoints, 0, DetPts * 2);
                }
                Marshal.FreeCoTaskMem(buffer);
    
                return (DetPoints);
            }
        }

     The imported DLL function is as follows:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    __declspec(dllexport) int __stdcall DetectObject (int *CharLumaBuf, int Rows, int Cols, char *BaseFolder)
    {
        return(-100);
    }
    
    #ifdef __cplusplus
    }
    #endif

    Any ideas/suggestions?

    Thanks

    Monday, January 13, 2014 10:38 AM

Answers