none
Use Managed C++ class library with C# RRS feed

  • Question

  • I have a managed C++ class library (.lib) that works with a C++ program.

    I simply add the reference to the .lib and add the #include for the header file and all works.

    I want to access this class library from a C# program.

    When I attempt to add the reference to the .lib, it says it cannot be added and that I should check to see that it is a valid assembly.

    any suggestions.

      Jim

    Thursday, November 30, 2017 10:35 PM

Answers

  • Hi jekain31459,,

    Thank you for posting here.

    If you want to call c++ dll from c# code, you could do the steps below.

    C++ code dll

    #include "stdafx.h"
    
    #include <stdio.h> 
    
    extern "C"
    {
    	__declspec(dllexport) void DisplayHelloFromDLL()
    	{
    		printf("Hello from DLL !\n");
    	}
    }

    extern "C” (with brackets) is also very important, it shows that all code within brackets is available from “outside”. 

    __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.

    C# code to invoke C++ dll

            [DllImport(@"C:\Users\v-wezan\Desktop\interop c# call c++\MyDll\Debug\MyDll.dll")]
            public static extern void DisplayHelloFromDLL();
    
         
            static void Main(string[] args)
            {
                Console.WriteLine("This is C# program");
                DisplayHelloFromDLL();
    
                Console.ReadKey();
            }

    For more details about Dllmport, you could refer to the MSDN article.

    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.110).aspx

    Here is the output.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 1, 2017 12:41 AM
    Moderator

All replies

  • It is not a managed C++ class library. A .lib file is not managed. I don't know what "reference to the .lib" means but if whatever you did works with the .lib file then you did not add a reference. You can do something as in Managed C++ Wrapper For Unmanaged Code but we need more information to know what to suggest. You might be able to use the C++ code from C# using Platform Invoke, also called P/invoke, that uses the DllImport statement.


    Sam Hobbs
    SimpleSamples.Info

    Friday, December 1, 2017 12:23 AM
  • Hi jekain31459,,

    Thank you for posting here.

    If you want to call c++ dll from c# code, you could do the steps below.

    C++ code dll

    #include "stdafx.h"
    
    #include <stdio.h> 
    
    extern "C"
    {
    	__declspec(dllexport) void DisplayHelloFromDLL()
    	{
    		printf("Hello from DLL !\n");
    	}
    }

    extern "C” (with brackets) is also very important, it shows that all code within brackets is available from “outside”. 

    __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.

    C# code to invoke C++ dll

            [DllImport(@"C:\Users\v-wezan\Desktop\interop c# call c++\MyDll\Debug\MyDll.dll")]
            public static extern void DisplayHelloFromDLL();
    
         
            static void Main(string[] args)
            {
                Console.WriteLine("This is C# program");
                DisplayHelloFromDLL();
    
                Console.ReadKey();
            }

    For more details about Dllmport, you could refer to the MSDN article.

    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.110).aspx

    Here is the output.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 1, 2017 12:41 AM
    Moderator
  • Actually, I'll say it this way - the C# compiler cannot process a static-linked library generated by C++ compiler.

    The intermediate code generated is in fact managed code, just with all placeholders that meant to be complemented with a header file, but C# compiler lacks functionality to process it, including but not limited to preprocessor directives. There's no way you can inline IL code generated by managed C++ to C# binaries.


    Friday, December 1, 2017 2:22 AM
    Answerer
  • Actually, I'll say it this way - the C# compiler cannot process a static-linked library generated by C++ compiler.

    The intermediate code generated is in fact managed code, just with all placeholders that meant to be complemented with a header file, but C# compiler lacks functionality to process it, including but not limited to preprocessor directives. There's no way you can inline IL code generated by managed C++ to C# binaries.


    I don't know why you are replying to me but the reply is very vague.

    You say "static-linked library generated by C++ compiler" but we don't know if it is using a static link or a dynamic link. Note that a static link library is created as in Creating a Static Library.

    You say "intermediate code generated"; generated by C++ or C#? If C# then yes, C# is incapable of producing unmanaged code, just as the standard C++ compiler is incapable of producing managed code. C++ managed code is produced by the C++/CLI compiler, which is not the standard compiler. There is a newer managed C++ language but I assume it is not relevant here.

    You say "complemented with a header file, but C# compiler lacks functionality to process it". Certainly C# does not process C++ code.

    You say "IL code generated by managed C++" but there is no indication that there is any IL code generated by the C++ compiler for the program in this thread.



    Sam Hobbs
    SimpleSamples.Info

    Friday, December 1, 2017 3:16 AM
  • Do you know that you can create static library (*.lib)  for managed C++?

    1) Start with normal Clr Console Application project for C++.

    2) Add a Class library to the solution. The default is DLL Managed C++ Project.

    3) Make the following changes to Project Properties... 

    4) Compile and here you are (at the Debug folder of solution level):

    What cannot be done is that, in order to use a .lib file, the compiler must be able to copy the code from the .lib file for necessary function and classes, replace any placeholders with header information cross reference with the .obj file of .exe file you're compiling, and then embed that code into the .exe file and fix up the address referenced.

    If successful, the only thing you need to deploy will be just the EXE (and possibly the .NET runtime). The LibTest.lib here will not be needed.

    This is something what C# compiler is not built to handle.

    Friday, December 1, 2017 10:01 AM
    Answerer
  • Hi jekain314159,

    Is there any update? Do you resolve the issue?

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 8, 2017 6:06 AM
    Moderator
  • I'll mark this reply as answer because I'm sure it must work.

    There is a slight variation of possible answer that, in the wrapper DLL instead of exporting function in classic C way, you can make it a normal managed DLL so that you can use the classes defined in the .lib file directly.

    However in the essence that you need to use wrapper DLL for it to work, I'll mark this as answer.

    If this answer does not work for you, feel free to unmark the answer, and then reply on why it does not work for you (say, there is some limitation in your project prevents it from working) to reopen the discussion.



    Friday, December 8, 2017 8:34 AM
    Answerer