将 C# 字典封送至C++(非托管)
Marshal C# dictionary to C++ (unmanaged)
我目前正在开发.NET Framework 4.7.2应用程序。我需要使用非托管C++库中的逻辑。我不能使用 C++/CLI(托管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 );
相关文章:
- C++中的MSD基排序(字典序)
- C++中是否有任何函数等同于python中字典的get函数?
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- C++中类似Python字典的结构?
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 与C++std::map::insert行为相比,C#排序字典的效率
- 为字典cpp创建[]=运算符
- C++ 随机生成的字典单词将仅显示以 B 开头的单词
- 如何将字典添加到向量?
- 蟒蛇字典到C++映射
- 使用给定的字典构建C++翻译器?
- C++类:如何使用字典["hello"]=20;像蟒蛇
- 如何在 c++ 中展平字典
- 在 c++ 中访问嵌套字典unordered_map<字符串、void*>?
- 如何将返回的 Python 字典转换为 C++ std::map<string,string>
- 拉丁语的二进制字典 - 如何构建或在哪里找到
- STL 中字典的最佳容器
- 调用C 字典的功能图
- 编写链接函数的更短/更有效的方法,该函数按字典顺序添加新元素