FileNotFoundException question

G

Guest

Hello,

Let me explain my situation. There is an application in C++ (that I didn't
write) that needs to call a C# DLL. One option, I believe, would be to
compile the app with the clr switch. Rather than do that first the other
developer wondered if I could create an intermediate managed code DLL that
would call my C# code.

So, I created a C++ DLL that exports a function and compiled it with the clr
switch. It calls my C# code. To test I created a C++ console application that
uses LoadLibrary to load the C++ managed code DLL. I originally had the C++
and the C# DLLs in the same directory and the executable in a different
directory. With that set up I always get a FileNotFoundException error. It
says it can't find the C# assembly. When I move the executable to the same
directory as the two DLLs it works fine.

Does anyone have any ideas as to why it would behave that way? Thanks for
any help, I really appreciate it.

Thanks,
Nick
 
G

Guest

Hi Nick,

Your problem has to do with the way the CLR probes for assemblies. Assuming
that you don't have a <codeBase> element in your application's configuration
file, the CLR probes for the assembly (your C# dll) in the following order:

1. The Global Assembly Cache.

2. The application base path, which is the root location where the
application is being executed. This is your executable's path. Keep this in
mind, more on this in a second.

3. The application base path appened with any private paths specified.

This is overly simplified, the rules are actually a lot more complex and
have to do with configuration files, static and dynamic loading, culture
info, etc. but I think you can see the problem already. You perform a
LoadLibrary to load the C++ dll, which references the C# dll. Therefore, the
CLR has to load the C# dll. In your case, again assuming no extern config
files / culture info / etc. it will search the GAC, not find it, then search
the application base directory, and not find it. It will then terminate the
probe and throw a FileNotFoundException. In the case where the dll's are in
the same directory as the exe, it can find it because the dll is located in
the application base directory.

For more information, you can see the following link, which explains things
in more detail:

http://msdn.microsoft.com/library/d...ide/html/cpconhowruntimelocatesassemblies.asp

Hope that helps!
 
G

Guest

Hi Magius,

Thanks for the help. I haven't got it working yet, but I just started
looking at it again. At least now I have a clue as to what I'm doing. Thanks
again.

Regards,
Nick
 
J

Jeffrey Tan[MSFT]

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