Designer rejects user control

I have a C ++ management library project compiled using / CLR. Inside this project there is a user control that calls a call to the native DLL. This user control appears in the designer toolbar, as it should be, but I can not drag it into the form. Without a link to a DLL, a user control can be used perfectly, but with a link I just get the message “Could not load the toolbar item” when I try to use it.

A native call is functional and will not harm User Control. A user control can be well considered in the designer on its own with the included DLL call. Also, if the control is manually added to the form and executed as a program, it will also be displayed in order.

This makes me suspect that the only problem is that Visual Studio Designer needs to know where this local DLL is located. But I'm not sure how to say it, or where to place the DLL so that it can find it. As far as I know, in the project settings there is no way to refer to the native DLL. Therefore, it makes sense to me that the designer is simply complaining because he cannot fix it.

Is there any way to make this work?

+8
dll visual-studio user-controls c ++ - cli designer
source share
6 answers

Unfortunately, you are faced with a “design error” in VS (or, in other words, a “feature”).

Your suspicion that the problem is with the Visual Studio designer, who needs to know where the local DLL is located, is partially right. This is not a matter of not knowing the location, but rather the fact that the designer cannot reflect mixed-mode assemblies (those that contain both managed and native code) to create an instance of the control. This causes the toolbar to display the error you specified.

The workaround is to compile the C ++ source files using /clr:pure to create a purely managed EXE.


Another possibility (also a “design error” in VS) is that the control you are trying to add was compiled as a 64-bit component. Since Visual Studio is a 32-bit process, it can only run 32-bit modules. Although it allows you to add a link to a 64-bit assembly, it cannot actually JIT compile this 64-bit assembly and execute it in the process.

The workaround here is to compile your custom control assembly using the “AnyCPU” parameter, which will lead to its execution as a 32-bit process in a 32-bit environment, and a 64-bit process in a 64-bit Environment. In fact, this is the best of both worlds, assuming you wrote your code correctly.


Finally, if none of them work, there is always the opportunity to bypass the constructor. You can still write the code needed to instantiate your user control and set its properties in the form initializer. All you lose is the ability to use the control inside the constructor inside Visual Studio. Everything will work as expected at runtime.

+8
source share

Link your library to / DELAYLOAD: "your_native.dll". For me, this solved the same problem.

+3
source share

I found this to sound like a similar problem?

https://connect.microsoft.com/VisualStudio/feedback/details/388107/winforms-designer-fails-to-load-managed-dll-having-unmanaged-dependencies#details

So, MS seems to be officially announcing the upgrade from VS2008 to VS2010 as a workaround.

Have you ever found another workaround?

This is the problem that I am experiencing right now in VS2008 with a C # .net managed project using a C ++ managed project using an unmanaged C ++ project.

Of course, it seems to me that the temporary assemblies of the designer that Visual Studio uses are a problem - it loads the detected dependency wrapper assembly into a temporary folder, but not related to the unmanaged dependencies of the wrapper assembly. I see this in C: \ Users \ Username \ AppData \ Local \ Microsoft \ VisualStudio \ 9.0 \ ProjectAssemblies. When I try to load the constructor, a new folder is created there with a managed DLL but no unmanaged dependencies. Unfortunately, I can’t understand what the person from the question from the above Microsoft says, this is a workaround using $ (TargetDir).

+1
source share

What you need to try:

VS2010: copy the dll to the Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies .

VS2008, VS2010: copy the dll to the AppData\Local\Microsoft\VisualStudio\<version>\ProjectAssemblies\ .

0
source share

It seems to me that managing with a link to its own DLL fails because Visual Studio cannot load it. Try setting the PATH environment variable either globally or an executable version of Visual Studio when it starts. If this does not help, try debugging the content management time behavior and you should find the problem.

0
source share

I had a problem like this, which could be the same as yours (who knows how many things can cause it?), Basically that I couldn’t remove the controls for the designer, but the existing controls worked just fine as production, so is testing.

For the record here was my solution:

1) Change the settings of your solution to x86. This allowed me to establish control over the designer, move him, etc. 2) Upon completion, you can return to AnyCPU or x64. I really only use the constructor to make it easier to set multiple values, and it seems that a problem with 32/64 bits is causing problems.

0
source share

Source: https://habr.com/ru/post/651332/


All Articles