0

This question is closely related to an old one: Windows Form Designer: Could not load file or assembly, but the desired answer there is not given.

My problem is the following. I have a custom control that I want to use in a WinForms project in .NET. Among other things, the control requires some functionality from opencv. So, I have an unmanaged C++ class that handles all the calls to opencv. Let's call it Unmanaged.dll Then, I have a C# wrapper to that that calls the unmanaged C++ using interop, let's call it CSharpWrapper.dll. CSharpWrapper is working and all the dependencies (Unmanaged.dll and opencv_worldXXX.dll) are being copied into the output directory using the <Content Include="path/to/dll"><CopyToOuputDircetory>PreserverNewest</CopyToOuputDirectory></Content> pattern.

The ui code that is consuming CSharpWrapper inherits from System.Windows.Forms.Control. Let's call it MyCanvas. When I build my application, it works perfectly (again, copying all the dependencies into the application output directory), but, when I drag and drop a MyCanvas component from my Forms Toolbox into my form Designer using Visual Studio, Visual Studio reports a Dll not found exception on the line that builds the objects pointed to in Unmanaged.dll.

What the answer I link above taught me is where to find the dll cache that Visual Studio populates with dependencies that are needed by Forms, Controls, Canvases etc. used by Designer. Sure enough, when I look in C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\17.0_8106eeaf\ProjectAssemblies there is a hash folder with CSharpWrapper.dll and CSharpWrapper.pdb in it. But none of the unmanaged dependencies are there, hence the runtime exception when using Designer. By manually copying the dependencies into that directory, Designer is satisfied and everything works.

But that is just a hack. And, worse is that, if I don't do the hack job, every time I edit my Form with MyCanvas on it, it will regenerate the .Designer.cs file and nuke all the references to MyCanvas. That is bad.

So, is there a way to get Visual Studio to put my unmanaged dlls into the ProjectAssemblies cache directory automatically?

Edit:

I've tried testing for DesignMode in the constructor for MyCanvas so that it should skip summoning the missing Dll, but for some reason it still throws. By attaching my debugger to my project from another instance of Visual Stuio and launching the control in Designer, sure enough DesingMode is false. Here is the code (.NET Framework 4.8):

class MyCanvas:Control
{
    private CSharpWrapper wrapper = null;
    public MyCanvas() : base()
    {
        if(!DesignMode) // <- false when using Designer for some reason
            wrapper = new CSharpWrapper(); // <- summons Unmanaged.dll here
    }
}
dmedine
  • 1,430
  • 8
  • 25
  • There is no System.Windows.Forms.Canvas class, makes it hard to arrive at an answer. You *really* don't want to solve this problem, DLL Hell is a serious headache. That DLL gets loaded into VS, not your program. Making issues like "what if another control does that" and "when does it get unloaded" quite hard to resolve. Avoid getting it loaded at all by using the DesignMode property in your code. – Hans Passant Aug 02 '23 at 12:46
  • @HansPassant, I am away from my work work station today, so I need to go back and look at the base class namespace, but I thought it was accurate. Sorry about that. I will update. I tried `if(!DesingMode)LoadCSharpWrapperEtc()` in the constructor for `MyCanvas` but it still throws the exception---not sure what that is about. Maybe the base class ambiguity. – dmedine Aug 03 '23 at 04:23
  • I spend a large amount of my days in DLL Hell. I'm pretty used to it and pretty good at clawing my way back into the light at this point. The Designer kerfuffle is a new one for me, though. – dmedine Aug 03 '23 at 04:25
  • @HansPassant it inherits from Control. – dmedine Aug 03 '23 at 23:59
  • Is it possible to create a separate MyCanvas control to avoid problems with unmanaged DLLs at design time? – wenbingeng-MSFT Aug 04 '23 at 10:05
  • @wenbingeng-MSFT Perhaps, I am annoyed that `DesignMode` is coming up as false when I'm using Designer, though. – dmedine Aug 07 '23 at 00:23

0 Answers0