I’m having an issue when calling 32 bit Delphi DLL files from a C# web site. The code generally runs fine, but occasionally I get an error,
*Unable to load DLL ”: The specified module could not be found. (Exception from HRESULT: 0x8007007E).
This issue persists until I recycle the application pool for the site, and then it works fine again.
On the same server, there is also a web service that is calling the same set of DLL files. This web service doesn’t seem to have the same issue that the web site has.
Both applications are using .NET Framework 3.5, separate application pools on IIS.
Here is the code I’m using to wrap the DLL files:
public sealed class Mapper
{
static Mapper instance = null;
[DllImport("kernel32.dll")]
private static extern bool SetDllDirectory(string lpPathName);
private Mapper()
{
SetDllDirectory(ConfigManager.Path);
}
public static Mapper Instance
{
get
{
if (instance == null)
{
instance = new Mapper();
}
return instance;
}
}
public int Foo(string bar, ref double val)
{
return Loader.Foo(bar, ref val);
}
}
public static class Loader
{
[DllImport("some.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, EntryPoint = "foo")]
public static extern int Foo(string bar, ref double val);
}
Then I call it like this:
double val = 0.0;
Mapper.Instance.Foo("bar", ref val);
Why would it “randomly” Unable to load DLL ”: The specified module could not be found. (Exception from HRESULT: 0x8007007E).?
The other problem is that I haven’t been able to replicate the issue in the development environment. I thought that due to two applications calling the same DLL files, that there could be some locks occurring. To replicate this, I created an application that spawned multiple threads and repeatedly called the 32-bit DLL files, and then used the web site to call these same DLL files. I still couldn’t replicate the issue.
Some possible fixes that I can think of:
- Wrap the 32 bit DLL files in the web service (because the web service doesn’t seem to suffer from the same problem). But this may be worthless if it turns out that the web service also fails.
- Set up state server for the session state and periodically recycle the application pool for the site.This isn’t fixing the problem, only avoiding it.
- Wrap the DLL files in an EXE file, and call that EXE file. Then I shouldn’t get the same issue. But this also seems like a hacky solution.
- Implement the mapper class differently? But how else should I be doing the call? The other drawback is that other applications are using this mapper, so I’d need to change there code too.
What should I do?
To fix this issue (it’s stopped happening now), I left the mapper code as is, but in the calling application I manually set the path (even though the mapper DLL file does it most of the time).
So in the main application, I make a call to append the path of the directory where the DLL files are located.
What’s strange is that the mapper class works most of the time, but sometimes stopped. Adding the path with the above code seems to have solved the issue.