Search code examples
asp.netasp.net-web-apivisual-studio-2017iis-expressunmanaged

Dll is not loading in ASP.net Web Api 2.0


I really need someone help. I have a ASP.Net Web Api 2.0 project and I am stuck on loading a Dll into my project. Here are the details:

The loading Dll is written in C++, then there is a .Net wrapper WrapperA, then there is another project ProjectA in which we call .Net WrapperA, then there is Web Api calling ProjectA.

The error message I am getting is:

Managed Debugging Assistant 'PInvokeStackImbalance' Message=Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'CRM.Crypto!CryptOnAPI_Lib.CryptOnAPI::LoadLibrary' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'

The LoadLibrary() function is defined in WrapperA as

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr LoadLibrary(string dllToLoad, IntPtr hFile, uint dwFlag);

In web.config file, I have also mentioned this configuration as well

<hostingEnvironment shadowCopyBinAssemblies="false"/>

I have tried to put all dlls in System32, inetsvr, IIS express folders, but nothing is working. Please note, I have a test application which is a Win Form application to test the dll, and dll is loading perfectly in that application.

I have placed all dlls in bin folder of the web API project. If I remove the dlls from bin folder, I get the same error message as mentioned above.

Please help :(


Solution

  • Found the solution of my problem. Still I am not able to debug with win32 dll but when deployed to IIS server, it is working fine. Solution is, place the Dlls in one of the following folders:

    C:\Windows\System32\inetsrv C:\Windows\SysWOW64\inetsrv

    The win32 dll will load in the Asp.Net Api.

    Cheers