Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling 32-bit unmanaged DLL files from C# randomly failing
    text
    copied!<p>I'm having an issue when calling 32 bit <a href="http://en.wikipedia.org/wiki/Embarcadero_Delphi" rel="nofollow noreferrer">Delphi</a> DLL files from a C# web site. The code generally runs fine, but occasionally I get an error,</p> <blockquote> <p>*Unable to load DLL '': The specified module could not be found. (Exception from HRESULT: 0x8007007E).</p> </blockquote> <p>This issue persists until I recycle the application pool for the site, and then it works fine again.</p> <p>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.</p> <p>Both applications are using <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="nofollow noreferrer">.NET</a> Framework 3.5, separate application pools on <a href="http://en.wikipedia.org/wiki/Internet_Information_Services" rel="nofollow noreferrer">IIS</a>.</p> <p>Here is the code I'm using to wrap the DLL files:</p> <pre><code>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); } </code></pre> <p>Then I call it like this:</p> <pre><code>double val = 0.0; Mapper.Instance.Foo("bar", ref val); </code></pre> <p>Why would it "randomly" <em>Unable to load DLL '': The specified module could not be found. (Exception from HRESULT: 0x8007007E).</em>?</p> <p>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.</p> <p>Some possible fixes that I can think of:</p> <ol> <li>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.</li> <li>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.</li> <li>Wrap the DLL files in an EXE file, and call that EXE file. Then I <em>shouldn't</em> get the same issue. But this also seems like a hacky solution.</li> <li>Implement the <em>mapper</em> 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.</li> </ol> <p>What should I do?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload