locked
cant add reference to c# class library from c++ project RRS feed

  • Question

  • getting an error when adding a reference to a c# class library dll from a c++ /cli project:
      "could not add a reference to file because it is neither a .NET assembly nor a registered ActiveX control"

    Here are the steps I perform to get the error:

    create c++ win32 project
    set property - /clr
    add 2nd project to solution - c# class library
    build solution
    c++ project properties, common properties, add new reference
    browse to the bin\debug folder of the c# class library project and select the .dll
    get error: "could not add a reference to file because it is neither a .NET assembly  nor a registered ActiveX control"

    running visual studio 2012 update 3.

    thanks,

    Tuesday, July 23, 2013 11:42 AM

Answers

All replies

  • Does it still happen if you build a C++/CLI project instead of C++/Win32/CLR?

    I built another solution. This time the c++ project is a CLR Console Application. This works.  I compare all the linker and compiler project settings  between the 2 projects.  I don't see any differences, other than Console vs win32.

    What am I doing wrong?

    thanks,

    Wednesday, July 24, 2013 3:24 AM
  • getting an error when adding a reference to a c# class library dll from a c++ /cli project:
      "could not add a reference to file because it is neither a .NET assembly nor a registered ActiveX control"

    Here are the steps I perform to get the error:

    create c++ win32 project
    set property - /clr
    add 2nd project to solution - c# class library
    build solution
    c++ project properties, common properties, add new reference
    browse to the bin\debug folder of the c# class library project and select the .dll
    get error: "could not add a reference to file because it is neither a .NET assembly  nor a registered ActiveX control"

    running visual studio 2012 update 3.

    thanks,

    Hi Steve,
    Refer to your description I reproduce a sample to test, the specific operations I did as below.
    1. create a win32 project, then set project properties Common Language RunTime Support (/clr). It works fine.

    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
     
     System::Console::Write("123");
     System::Console::Read();
     return 0;
    }

    2.create a CLR console application, then add a new resource file test.cpp. It works fine.

    int main()
    {
     
     System::Console::Write("123");
     System::Console::Read();
     return 0;
    }
    So, could you please give me more detailed information about how you  did settings step by step after you created win32?  Hope you give me feedback, I am very appreciated to fix this problem together with you.


    May Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 24, 2013 7:44 AM
  • So, could you please give me more detailed information about how you  did settings step by step after you created win32?  Hope you give me feedback, I am very appreciated to fix this problem together with you.


    May Wang
    MSDN Community Support | Feedback to us

    the 2nd step is to create a c# class library project:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Train4Lib
    {
      public class Class1
      {
        public static int GetInt()
        {
          return 25;
        }
      }
    }
    
    
     

    then in the c++ win32 project I set the additional #using directory to the bin/debug folder of the c# project. That enables this code to compile in the win32 project:

    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #using <Train4Lib.dll>
    
    using namespace System ;
    using namespace System::Runtime::InteropServices;
    using namespace Train4Lib ;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int rv = Class1::GetInt( ) ;
    
    	System::Console::Write("123") ;
    	System::Console::Read( ) ;
    
    	return 0;
    }
    
    

    the problem is I cannot add a reference to the c# library dll from the win32 project. Properties->common properties->add reference. That is, visual studio 2012 marks the reference as "copy local false" and does not accept a change to copy local true. If I add the reference by browsing to it I get the error: "could not add reference to file because it is not a .net assembly"

    thanks,

    Wednesday, July 24, 2013 11:47 AM
  • Hi Steve,

    I follow your step to test again, it is no problem here. I create a Train4Lib project to produce a Train4Lib.dll , then I create a win32 console project with /clr. I copy Train4Lib.dll file under win32 console project directory. Then it works fine.


    May Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 24, 2013 3:55 PM
  • Hi Steve,

    I follow your step to test again, it is no problem here. I create a Train4Lib project to produce a Train4Lib.dll , then I create a win32 console project with /clr. I copy Train4Lib.dll file under win32 console project directory. Then it works fine.

    yes, May, I can copy the DLL myself also.  That is not my question.  Why can I not add the c# library as a reference to the c++ project?  Everytime I build the solution I want the c# library dll to be copied to the folder of the c++ exe file.  Is this a feature that is supposed to work?

    Wednesday, July 24, 2013 6:06 PM
  • Hello Steve Richter,

    You are right  when I try to add reference, it doesn't work. It seems that it is related to a VS known issue here

    http://connect.microsoft.com/VisualStudio/feedback/details/526132/cli-c-project-cannot-be-referenced-from-c-project-allowing-only-assembly-dll 

    Could you please submit a new feedback on this site:

    https://connect.microsoft.com/VisualStudio/Feedback

    After you have posted your new feedback, could you please send your link here so that other members can also follow up this problem.

    Thanks for your understanding.

    Best Regards!

    May


    May Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Steve Richter Thursday, July 25, 2013 11:27 AM
    Thursday, July 25, 2013 6:37 AM
  • Hello Steve Richter,

    You are right  when I try to add reference, it doesn't work. It seems that it is related to a VS known issue here

    http://connect.microsoft.com/VisualStudio/feedback/details/526132/cli-c-project-cannot-be-referenced-from-c-project-allowing-only-assembly-dll 

    Could you please submit a new feedback on this site:

    https://connect.microsoft.com/VisualStudio/Feedback

    After you have posted your new feedback, could you please send your link here so that other members can also follow up this problem.

    Thanks for your understanding.

    Best Regards!

    May


    May Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    https://connect.microsoft.com/VisualStudio/feedback/details/794976/in-a-c-project-cannot-add-reference-to-a-c-project

    Thursday, July 25, 2013 11:27 AM