A question about DllNotFoundException

  • Thread starter Thread starter Michel
  • Start date Start date
M

Michel

Hi there,

I am using a dll (using DllImport) and often (but not always) I get a
DllNotFoundException: The dll could not be found.
This dll has some startup logic (which takes a second or two to execute).

I notice that I have a lot more success when the calling thread is using the
STA apartment mode (instead of MTA). To my knowledge (it is not my dll) the
dll is not using any COM components. There is only one thread accessing the
dll at a time.
I've also noticed I have more success if I put the dll in the system32
directory instead of the executable directory.
Last, I notice that I have more success if I call the dll from a windows
service (using background threads) instead of a windows application (with or
without background threads).

The frustrating part is that I cannot get it to work OR fail 100% of the
time. I can only vary the degree of success.

Some thoughts/questions I have:
Could the CallingConvention (or any other property of DllImportAttribute) be
a cause of this problem?
What does a thread's apartment mode have to do with a standard dll
(exporting functions). I must use MTA (for reasons outside the scope of this
dll).
What are causes of the DllNotFoundException, could it also be e.g. a
timeout?

Any thoughts or suggestions appreciated,
Thanks,
Michel Smits
 
Hi Michel:

Just a thought:

Does the DLL you are trying to import have a dependency on a second
DLL that might not be found? Try looking at the DLL with DEPENDS.EXE
from sysinternals.com just to see if anything might be amiss.

HTH,
 
Hi Scott,

Thanks for the suggestion but no, it doesn't.
The bad thing is it *mostly* goes ok.

Thanks again,
Michel
 
Back
Top