none
C# runtime reference path RRS feed

  • Question

  •  

    I have an ActiveX control written in unmanaged C++ MFC that I'm loading in Internet Explorer.  This ActiveX control links to a managed C++ DLL, which links to a managed C# DLL.  The application can't find the managed C# DLL and throws this exception:

     

    An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.

    Additional information: Could not load file or assembly '<blah>' or one of its dependencies. The system cannot find the file specified.

     

    If I put my DLL in the same directory as IExplore.exe, then it works fine.

     

    Is there any way to set the path that it looks for C# references in?  I tried adding it to HKLM\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders, but that didn't help.  I also tried putting it in the GAC, but then I would have to give my assembly a strong name which has other un-desired side effects.

     

    Thanks,

    Gregg

    Thursday, February 14, 2008 8:49 PM

Answers

  • Hi Gregg,

     

    1. You can create IExplore.exe.config file and define assemblyBinding element in it.

    2. You can handle AssemblyResolve event of AppDomain in your managed c++ dll.

     

    Please, let me know if this helps.

     

    Friday, February 15, 2008 10:38 AM

All replies

  • Hi Gregg,

     

    1. You can create IExplore.exe.config file and define assemblyBinding element in it.

    2. You can handle AssemblyResolve event of AppDomain in your managed c++ dll.

     

    Please, let me know if this helps.

     

    Friday, February 15, 2008 10:38 AM
  •  

    Hi Vitaliy,

     

    Thanks a lot.  Handling the AssemblyResolve event solved my problems, although I had to put the code that added the event handler into its own function because it was trying to load my managed C# DLL before the event handler was set.

     

    This will actually help me solve another problem that I've been having, so thanks for pointing this one out to me.

    Monday, March 3, 2008 3:10 PM
  • Could you explain how you handled the AssemblyResolve event?  I think I have the same problem, although I'm a bit new at this.

    I have a simple EXE (getEmailAddress) and a simple DLL (ActiveDirectoryInterface).  If they're not in the same dir, I get this error.  Since I can't control what others will do with this EXE once I release it, I'd like it to handle this error gracefully.

    I tried the example shown in the MSDN "AppDomain.AssemblyResolve Event" link, but I didn't work at all.  VS2008 doesn't understand the "Assembly" in:
    Code Snippet

           static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)


    Any help would be GREATLY appreciated!


    Thanks,
    Jim
    Monday, March 3, 2008 4:54 PM
  • Ah, looks like that should be "System.Reflection.Assembly".  Code compiles, but I'm still not handling the error.  Any suggestions for handling exceptions like this that seem to be "outside" of Main()?  Using try/catch in Main was the first thing I did.  Since the Assembly/Reference (DLL) can't be found, I think this error occurs before Main() really gets executed.
    Monday, March 3, 2008 5:06 PM
  • The code below is from managed C++, so you'll have to change it a little to work in C#.

     

    The problem I found is that references are loaded as soon as a function is called that requires them, not when the library is loaded.  Hence, if main() uses any classes or functions from your DLL (ActiveDirectoryInterface) then you can't set the AssemblyResolve event handler before main() is run, and it won't work.

     

    One work-around I tried for this was to move all of the code from main() into mainImpl() and call mainImpl() from main(), however the compiler optimized the two functions together and I was back to square one.  Hence, I don't know what advice to give you to work around this issue.

     

    Here is how I set the AssemblyResolve event handler (in managed C++)

    AppDomain::CurrentDomain->AssemblyResolve += new ResolveEventHandler(ResolveOnPath);

     

    Then, my ResolveOnPath function looks sort of like this:

     

    using System::Reflection;

    Assembly* ResolveOnPath(Object *sender, ResolveEventArgs *args)

    {

    return Assembly::LoadFile("C:\\temp\\" + args->Name->Split(',')[0] + ".dll");

    }

    Monday, March 3, 2008 6:02 PM
  • Thanks for the code, and the tips.  I'm definitely stuck in the can't-handle-an-exception-before-Main scenario.  If anyone else stumbles across this and has a suggestion (at any time in the future), I'd love to hear it!
    Monday, March 3, 2008 9:19 PM
  •  Gregg Mattinson wrote:

     

    One work-around I tried for this was to move all of the code from main() into mainImpl() and call mainImpl() from main(), however the compiler optimized the two functions together and I was back to square one.  Hence, I don't know what advice to give you to work around this issue.

     

     

    In order to preserve such compiler implementation I suggest you to create static class MainImpl with static function and call this function from the Main method.

    Tuesday, March 4, 2008 7:52 AM