1

I need to call c# method in native c++. I followed this guide, download it, it's worked, but when I try it on new version of .net I cannot reproduce. The main idea of this code is call c# method in such way: C# .dll -> C++/CLI(managed c++) -> c++

C#

public static class ManagedClass
    {
        public static bool Check()
        {
            return true;
        }
    }

Managed c++

namespace ClassLibrary1 {

    public ref class DoWork
    {
    public: bool CheckCSharp()
    {
        return CSharp::ManagedClass::Check();
    }
    };
}
__declspec(dllexport) bool Check()
{
    ClassLibrary1::DoWork work;
    return work.CheckCSharp();
}

c++

#include "ClassLibrary1.h"
_declspec(dllexport) bool Check();

int main()
{
    std::cout << Check();
    system("pause");
    return 0;
}

It's show error

Error LNK2019 unresolved external symbol "bool __cdecl Check(void)" (?Check@@YA_NXZ) referenced in function _main

1 unresolved externals

3
  • 1
    In your unmanaged project setting, you will need to reference the ManagedDll.lib file. The linker error says that you didn't take care of that. Commented Mar 20, 2019 at 8:37
  • @HansPassant But when I include header, this mean it's in the same directory, then it should be along with it, or not? I created an empty ClassLibrary1.h in native c++ Commented Mar 20, 2019 at 8:47
  • 1
    A .h file can only take care of the declaration, it doesn't tell the linker what to do. This is a detail that doesn't exist in C#, it doesn't use a linker and adding a reference to an assembly is enough to keep it happy, but needs to be taken care of explicitly in C++. Commented Mar 20, 2019 at 9:19

1 Answer 1

1

enter image description here must write in linker path to .lib

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.