-2

I created an activeX exe (registered with regasm.exe) that loads a DLL with

Dim a As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(FullPath)

This DLL works with an Interface "PSInterface". As normal exe it works, but when i call that activex object i am getting a ReflectionTypeLoadException in a.GetTypes. When i look into that error i get the following:

Could not load file or assembly 'PSInterface, Version=1.0.0.0, Culture = neutral, PublicKeyToken=null' or one of its dependencies. The System cannot find the file specified.

The PSInterface.dll is in the same folder as the exe and dll.

What can i do?

Fabian
  • 9
  • 5
  • You can try loading PSInterface.dll using Depends.exe and make sure it isn't missing a dependency. You can try using .NET assembly resolution logging (Fusion logging I believe) to see why .NET is failing to load it. Unfortunately, the error messages for failure to load a library are extremely poor as just about any potential failure gets grouped into the one message which only indicates the top-level file it's trying to load (but crucially not the specific file/assembly that failed which isn't necessarily the same). – Craig Sep 10 '21 at 18:37
  • The Fusion Log Viewer helped me to find the path where the program searches after the PSInterface.dll. I using NI Teststand 2013 to call the COM object and it searches the PSInterface (and other references) in ```"C:\Program Files (x86)\National Instruments\TestStand 2013\Bin"``` but i dont want my references there. Can i define that in Visual Studio? – Fabian Sep 13 '21 at 11:46
  • I'm not sure whether there are any options to customize. You can provide an event handler for CurrentDomain.AssemblyResolve to manually handle assembly resolution. – Craig Sep 13 '21 at 13:19
  • The problem is that i have many assemblies to load. They are all in the same folder. So i found Appdomain.CurrentDomain.BaseDirectory, which gives me the path to the folder ```"C:\Program Files (x86)\National Instruments\TestStand 2013\Bin"``` where Teststand tries to laod the references. The easiest way should be to set the "Basedirectory" to my path but its ReadOnly. Any Idees? – Fabian Sep 14 '21 at 08:22

1 Answers1

0

To load the Assemblies from the same folder, I set AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf LoadFromSameFolder and the Function

Private Shared Function LoadFromSameFolder(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
        Dim folderPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
        Dim assemblyPath As String = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".dll")
        If Not IO.File.Exists(assemblyPath) Then
            assemblyPath = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".exe")
            If Not IO.File.Exists(assemblyPath) Then
                Return Nothing
            End If
        End If
        Dim assembly As Assembly = Assembly.LoadFrom(assemblyPath)
        Return assembly
    End Function

Thanks @Craig for your help.

More Here: How to add folder to assembly search path at runtime in .NET?

Fabian
  • 9
  • 5