473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

FileNotFoundException question

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
Nov 17 '05 #1
3 2404
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/de...assemblies.asp

Hope that helps!
--
Magius
"Nick" wrote:
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

Nov 17 '05 #2
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

"Magius" wrote:
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/de...assemblies.asp

Hope that helps!
--
Magius
"Nick" wrote:
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

Nov 17 '05 #3
Hi Nick,

I am glad Magius' help can help you. In addition to this, below is the
official document of how .Net Runtime locates the assembly:
"How the Runtime Locates Assemblies"
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconhowruntimelocatesassemblies.asp

Hope it helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ulf | last post by:
Hello, After I got a FileNotFoundException in my real life CSharp code, I nailed it down to the following line: System.EnterpriseServices.ResourcePool rp = new...
1
by: Brian | last post by:
I've looked through the previous posts on this one and have verified permissions and location of my Dlls, but I am still getting an exception when I try to Load an assembly. The directory where...
6
by: Prodip Saha | last post by:
I am trying to load a shared component like DataAccess from GAC and not been able to successfully get it working for my asp.net application. The dll is loading from the GAC with correct version and...
5
by: MattC | last post by:
Hi, I am getting the following error: System.IO.FileNotFoundException: C:\Dreamtxt\WebSites\mysite\www\feedback.aspx at System.Web.UI.TemplateParser.GetParserCacheItem() at...
0
by: jason | last post by:
Hello. Newbie question for sure. I've got this simple web service (.asmx) function that works fine when called remotly from asp.net client web page. The dll compiled output from the wsdl...
3
by: James | last post by:
Hi guys Do you ever get the exception in Managed C++? How can I know which file or dll is missed?
5
by: SGRing | last post by:
I have a c# application that I'm migrating from 2003 to 2005, and I moved it to another machine. It built ok. But, when trying to run I get System.IO.FileNotFoundException was unhandled...
0
by: Peter Vestergaard | last post by:
Hi, I am running VS .Net 2005. I have an application in which one of the classes are having a member that is an instance of a class defined in a managed C++ dll. As soon as I try to create an...
2
by: bbindae | last post by:
I am trying to check whether XmlTextReader reads the xml file successfully or not. MSDN says that XmlTextReader raise the FileNotFoundException when it cannot find the file to read. Here is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.