ASP.NET Web Api DllImport (LoadLibrary) fails on IIS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iacierno
    New Member
    • Nov 2015
    • 5

    ASP.NET Web Api DllImport (LoadLibrary) fails on IIS

    Hi,
    I'm developping a Web API that needs to call an unmanaged DLL written in C++.

    In debugging (IIS Express), I'm able to call the function within the DLL and LoadLibrary is loaded correctly with a Handle.
    Problem comes when I deploy my Web Api to my local machine (IIS 8): It recognizes the path of the DLL, permissions are also set properly, but LoadLibrary fails bacause I get a IntPtr.Zero.

    I used Marshal.GetLast Win32Error() to detect the error, but I get error Code 193, which seems to have nothing to do with LoadLibrary.


    Here's my code:

    Code:
     if (!Directory.Exists(PATH) && !File.Exists(PATH_DLL))
                        throw new Exception(string.Format("Dll not found: {0}.", PATH_DLL));
    
                    //LoadLibrary
                    hModule = DllInterop.LoadLibrary(PATH_DLL);
                    IsLoaded = (hModule != IntPtr.Zero);
                    if (!IsLoaded)
                    {
                        int errno = Marshal.GetLastWin32Error();
                        throw new Exception(string.Format("Failed to load library. Fehler:{0}.", errno));
                    }

    I would be very grateful for your help. Thanks in advance!
    Last edited by Rabbit; Nov 26 '15, 06:43 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Use Debugger.Break( )

    It has certainly to do with IIS_USERS Permissions set to the DLL_PATH.

    else Fusion Log Viewers can also be used.

    Hope this helps.

    Comment

    • iacierno
      New Member
      • Nov 2015
      • 5

      #3
      Thanks for your reply!
      I don't think it's because of permissions regarding DLL_PATH.
      I tried if I'm able to access any other function in "kernel32.d ll" or even "user32.dll ".

      Code:
      [DllImport("kernel32.dll")]
      public static extern bool Beep(int frequency, int duration);
      
      [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
      public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
      But none of them is working on IIS. Debugging with Visual Studio works...
      I even tried to change the identity of the application pool to "Local Filesystem", but without success.

      Do you have any other suggestion?

      Comment

      • iacierno
        New Member
        • Nov 2015
        • 5

        #4
        Thanks for your suggestion. I found the problem: in my c++ DLL I was calling MessageBoxes, which seems to represent a problem on production.

        MessageBox from user32.dll and the Beep from kernel32.dll might not be loaded beacuse of permission set to the IIS running on session 0.

        Comment

        Working...