locked
How to get a Dictionary from C++ DLL to C#? RRS feed

  • Question

  • What i want:

    DictionaryToC# function_in_cplusplus_library(DictionaryFromC#){

    }

    How can i do the conversion?

    Tuesday, September 25, 2012 4:02 PM

Answers

  • To exchange dictionaries between C# and C++, you'll want to use the Windows::Foundation::Collections::IMap<K,V> interface.  In .NET, this interface is projected to the System.Collections.Generic.IDictionary<K,V> interface which means that any type that implements IDictionary can be passed to C++ as an IMap.   Similarly, C++ can return an IMap to .NET as an IDictionary.

    For example, if you wrote some C++ code in a Windows Runtime Component project in Visual Studio that looked like this:

    #include <collection.h>
    
    using namespace std;
    using namespace Platform;
    using namespace Platform::Collections;
    using namespace Windows::Foundation::Collections;
    
    IMap<String^, int>^ MyCppClass::DoubleDictionaryElements(IMap<String^, int>^ csharpDictionary)
    {
        map<String^, int> stlMap;
    
        for_each(begin(csharpDictionary), end(csharpDictionary), [&stlMap] (IKeyValuePair<String^, int>^ csharpDictionaryEntry)
        {
            stlMap[csharpDictionaryEntry->Key] = csharpDictionaryEntry->Value * 2;
        });
    
        return ref new Map<String^, int>(stlMap);
    }

    It would look to C# as a method with the following signature:

    IDictionary<string, int> MyCppClass.DoubleDictionaryElements(IDictionary<string, int> csharpDictionary)
    

    Which means you could use it similar to:

    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    dictionary["initial"] = 21;
    dictionary["other"] = 5;
    
    MyCppClass test = new MyCppClass();
    IDictionary<string, int> doubled = test.DoubleDictionaryElements(dictionary);

    Wednesday, September 26, 2012 5:16 PM

All replies

  • To exchange dictionaries between C# and C++, you'll want to use the Windows::Foundation::Collections::IMap<K,V> interface.  In .NET, this interface is projected to the System.Collections.Generic.IDictionary<K,V> interface which means that any type that implements IDictionary can be passed to C++ as an IMap.   Similarly, C++ can return an IMap to .NET as an IDictionary.

    For example, if you wrote some C++ code in a Windows Runtime Component project in Visual Studio that looked like this:

    #include <collection.h>
    
    using namespace std;
    using namespace Platform;
    using namespace Platform::Collections;
    using namespace Windows::Foundation::Collections;
    
    IMap<String^, int>^ MyCppClass::DoubleDictionaryElements(IMap<String^, int>^ csharpDictionary)
    {
        map<String^, int> stlMap;
    
        for_each(begin(csharpDictionary), end(csharpDictionary), [&stlMap] (IKeyValuePair<String^, int>^ csharpDictionaryEntry)
        {
            stlMap[csharpDictionaryEntry->Key] = csharpDictionaryEntry->Value * 2;
        });
    
        return ref new Map<String^, int>(stlMap);
    }

    It would look to C# as a method with the following signature:

    IDictionary<string, int> MyCppClass.DoubleDictionaryElements(IDictionary<string, int> csharpDictionary)
    

    Which means you could use it similar to:

    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    dictionary["initial"] = 21;
    dictionary["other"] = 5;
    
    MyCppClass test = new MyCppClass();
    IDictionary<string, int> doubled = test.DoubleDictionaryElements(dictionary);

    Wednesday, September 26, 2012 5:16 PM
  • So valuable! Thank you!

    Wednesday, September 26, 2012 5:39 PM