C# Azure Function and legacy DLL

-1

I am trying to use Azure Function to do some calculations using a legacy .DLL

In my Azure function I have code like this and it works fine locally:

[DllImport("calculation.dll", ExactSpelling = false, CallingConvention = CallingConvention.StdCall, EntryPoint = "CreateItem")]
    private static extern int CreateItem([MarshalAs(UnmanagedType.LPStr)] string applicationName);

Running it in azure is get errors like this:

[Error] Executed 'Calc' (Failed) Unable to load DLL 'C:\home\site\wwwroot\bin\runtimes\win7-x86\native\calculation.dll' or one of its dependencies: A dynamic link library (DLL) initialization routine failed. (0x8007045A)

Opening the DLL in Dependency walker I can see that it depends on:

 c:\windows\system32\KERNEL32.DLL 
 c:\windows\system32\USER32.DLL
 c:\windows\system32\OLE32.DLL 
 c:\windows\system32\OLEAUT32.DLL

I tried copying the 4 dlls from my local computer and into the Azure Function without any luck.

Is this even the right direction to go trying the solve this? Or is there something fundamental I am missing?

c#
azure-functions
dllimport
legacy
asked on Stack Overflow Feb 25, 2021 by Mobz

1 Answer

2

The whole point of Azure managed instances (and indeed, any cloud code-running service) is that everything your website/function/whatever needs to run is self-contained within the code package you upload to Azure. Code that tries to call out to arbitrary third-party DLLs, that themselves call out to other DLs, can never satisfy this requirement. (Not to mention that there's no guarantee whether the instance you're running on is even Windows to begin with).

The only way you could make this work is to provision a Windows VM on Azure, install your DLL there, and create a web API that invokes the DLL and returns the result; then make your Azure Function call the web API. But this is a massive amount of extra effort to work around the fact that P/Invoke really isn't a thing on Azure.

If you can't replace this DLL with managed code, you may want to reconsider trying to host this in the cloud.

answered on Stack Overflow Feb 25, 2021 by Ian Kemp

User contributions licensed under CC BY-SA 3.0