簡體   English   中英

元帥 C# 字典到 C++(非托管)

[英]Marshal C# dictionary to C++ (unmanaged)

我目前正在開發一個 .NET Framework 4.7.2 應用程序。 我需要使用來自非托管 C++ 庫的邏輯。 我不能使用 C++/CLI (managed C++)

我試圖弄清楚如何將 C# 字典編組到非托管 C++:

Dictionary<string, float> myData

您知道嗎,非托管 C++ 中Dictionary<string, float>的正確等效項是什么?

謝謝!

如果您的字典很小,請在具有鍵值結構的連續緩沖區中進行序列化。

如果您的字典很大並且 C++ 只需要查詢幾個值,或者更改過於頻繁,請使用 COM 互操作。 由於 .NET 運行時中廣泛的 COM 支持,非常容易做到。 下面是一個例子,使用 guidgen 為界面生成 GUID。

// C# side: wrap your Dictionary<string, float> into a class implementing this interface.
// lock() in the implementation if C++ retains the interface and calls it from other threads.
[Guid( "..." ), InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
interface iMyDictionary
{
    void getValue( string key, out float value );
    void setValue( string key, float value );
}
[DllImport("my.dll")]
extern static void useMyDictionary( iMyDictionary dict );

// C++ side of the interop.
__interface __declspec( uuid( "..." ) ) iMyDictionary: public IUnknown
{
    HRESULT __stdcall getValue( const wchar_t *key, float& value );
    HRESULT __stdcall setValue( const wchar_t *key, float value );
};
extern "C" __declspec( dllexport ) HRESULT __stdcall useMyDictionary( iMyDictionary* dict );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM