1
votes

This is with Visual Studio 2010, SP1. I have a C++ class in a separate C++ project that I want to access and use in C# from another project (both living under the same solution). I have referenced my C++ project from C#. I build my C++ project using the /clr option. I have built a Managed version of the C++ class that calls the corresponding native C++ class.

When I right-click on my C++ project reference from my "References" of my C# project, and click "View in Object Browser", my C++ project exists as part of the list of objects, and I can open it and see my class, namespace, etc. that I am trying to use.

However, from C# code, when I try to do "using " it's not part of the autocomplete list of namespaces, and in fact it gives a compilation error if I type the C++ namespace. "Error 1 The type or namespace name 'MyCppClass' could not be found (are you missing a using directive or an assembly reference?)"

And the answer to that question is no, I'm not missing a using directive or assembly reference, as explained above. Additionally, my C++ classes don't exist in the default namespace, they simply don't exist in my C# project's universe.

So... what am I doing wrong?

1
Are you able to add any other foreign dlls? Just normal c# ones? I've had similar issues in the past if your project is set to the .net 4 client profile then try change it to just normal .net 4 and see if that helps. - David Esteves
I don't know about other foreign dlls, would need to try, but I'm able to reference other C# projects. I was configured as .net 4 client, and switched it to the normal one, however that didn't solve my problem referencing C++ objects. - Simon Ouellette
I have the same problem... Has anyone ever solved this problem? - hjweide

1 Answers

0
votes

This may be too late, but I just had this same issue as you and came back to see if you got an answer.

Eventually I figured out that you need to actually add your COM class as a reference to your .net project along with the .net wrapper class. I created a c++ COM dll and used the tlbimp to generate a .net wrapper. I then added the wrapper to my project, and went to add references under the COM tab it had my COM assembly there, so I added that too and my project compiled and worked fine.