Tuesday, November 7, 2006

Using a .NET 1.x Control from a MC++ 2.0 Windows Forms Application

OK, this one took me forever to find, even though the solution was simple, so I'm blogging it.


Apparently, there's still at least one person out there using RichTextBox2/RichTextEditor. And he wanted to use it from managed C++, of all places. And it wasn't working, of course. So he emailed me, and I was happy to help. Of course, it took some research, since VC++ isn't exactly my first choice for writing Windows Forms applications. (Like, I didn't even know VC++ has a forms designer now!)


The problem occurs any time you try to add a control to a form when the control was compiled against .NET 1.x. When you do that, you'll see errors like this:


Error C3699: '^' : cannot use this indirection on type 'System::Windows::Forms::Cursor'
warning C4945: 'DescriptionAttribute' : cannot import symbol from 
'c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll':
as 'System::ComponentModel::DescriptionAttribute' has already been imported
from another assembly 'System'
c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll

This is due to the fact that when you add the control to the form, VC++ helpfully adds references for the control's dependent assemblies to the form. Unfortunately, those dependencies include things like the 1.x version of System.dll, and you've already got references in your project to the 2.0 version of System.dll. Hence the conflict.


The solution is to go into Project->Properties->Common Properties->References, where you must remove references to the duplicate 1.x libraries. Easy. And hopefully easier to find now.

No comments:

Post a Comment