Problem finding DLL

R

Rich

I am trying to use unmanaged DLLs within managed code. My approach is
to create a managed DLL with wrapper functions for my unmanaged
functions. The unmanaged functions come from an unmanaged DLL.

I can successfully build my managed DLL and use it from managed code.
However, at runtime the code cannot find the unmanaged DLL. I've tried
putting the unmanaged DLL in the same directory as the code is running
from, in C:\Windows\System32, and a few other places - no luck, I
always get an error indicating that the unmanaged DLL cannot be found.

Here is what I do in my managed DLL to access functions in the
unmanaged DLL:

using namespace System::Runtime::InteropServices;

[DllImport("ADT_API_L1", EntryPoint="ADT_L1_Initialize")]
extern "C" unsigned int ADT_L1_Initialize(unsigned int dev_num,
char *filename);

The unmanaged DLL is "ADT_API_L1.dll". In the code for the managed DLL
I also define a namespace and classes with member functions that call
unmanaged functions like "ADT_L1_Initialize" - I left this out to keep
this as uncluttered as possible.
From my managed CLR application I include the managed DLL as a
reference and I can see my namespace, classes, etc. The managed
application compiles without error, but when I run the program it
chokes and dies, giving an error that it cannot load the DLL
(System.DllNotFoundException).

I'm sure there is something simple and stupid that I am missing. Can
anybody here set me straight?
 
B

Bruno van Dooren [MVP VC++]

I can successfully build my managed DLL and use it from managed code.
However, at runtime the code cannot find the unmanaged DLL. I've tried
putting the unmanaged DLL in the same directory as the code is running
from, in C:\Windows\System32, and a few other places - no luck, I
always get an error indicating that the unmanaged DLL cannot be found.

The DLLs get loaded at runtime.
To make sure that you native dll is found
put it alongside the assembly in which it is imported or install it on your
disk somewhere, and add the containing folder in your path variable.

Now, there are 2 things to keep in mind:
- putting stuff in the system32 directory is frowned upon. you generally
don't want to do this to avoid DLL hell.
- be sure that your native DLL can also resolve its dll dependencies. it
will fail to load if one of its dependencies cannot be found (like e.g. the
VC runtime dlls)

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
R

Rich

Bruno,

I figured out my problem (I was doing something stupid), but thanks for
your response.
I do have another question related to DLLs, but I put it in a new post.

Thanks again,
Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top