How to call managed C++ methods from Un-managed C++

前端 未结 2 1380
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 18:23

PLEASE SEE UPDATE BELOW

(RESOLVED) Also I have extended this into a second question here Implement a C# DLL COM File In Unmanaged C++ Program

I have resear

相关标签:
2条回答
  • 2021-01-18 18:49

    First, your methods receive and return a String^ which is a managed object. Unmanaged code does not know this type, and cannot create such object. So, you will need to wrap the function call such that the function marshal the managed type to something that the unmanaged code can understand.

    After that, you can add the DllExport attribute to the managed method, as discussed here.

    0 讨论(0)
  • 2021-01-18 18:52

    You could use the cool C++ Marshaling library provided by Microsoft, something like this:

    #include "cSharpRiJHarn"
    #include "stdafx.h"
    #include <string>
    #include <stdio.h>
    #include "msclr\marshal_cppstd.h" // marshaling library
    
    using namespace cSharpRiJHarn;
    using namespace System;
    using namespace msclr::interop; // marshaling library
    
    std::wstring Encrypt(std::wstring s)
    {
        return marshal_as<std::wstring>(RijndaelLink::encrypt(marshal_as<String^>(s)));
    }
    
    std::wstring Decrypt(std::wstring s)
    {
        return marshal_as<std::wstring>(RijndaelLink::decrypt(marshal_as<String^>(s)));
    }
    
    0 讨论(0)
提交回复
热议问题