如何在WinRT中调用类似STL的Vector和Map

本文介绍在Windows运行时(WinRT)环境中使用C++进行组件开发的方法。重点讲解了C++内置类型、库类型及Windows运行时类型之间的区别,并通过实例演示了如何在WinRT组件中运用类似STL的类型系统。

资料参考:
1. http://msdn.microsoft.com/zh-cn/library/windows/apps/hh710418.aspx
2. http://msdn.microsoft.com/zh-cn/library/windows/apps/hh441569.aspx

通常情况下,在对C++组件进行编码时,可使用常规 C++ 库和内置类型,但抽象二进制接口 (ABI) 边

界处的 C++ 库和内置类型除外,该处需与 JavaScript 进行双向数据传递。

只有 Windows 运行时类型可以跨 ABI 边界传递。
分析:所以你在WinRT 组件里面编写的程序,需要使用Windows运行时类型,就是带有尖冒号“^”,而

不是传统的C++值类型程序。

虽然你也可以使用C++内置类型(如int,double),但是编译器会在公共方法的参数和返回类型中自动

将其转换为相应的 Windows 运行时类型,如 int32、flot64 等。除非跨 ABI 传递类型,否则不会进

行此类转换。


1. C++内置类型、库类型、和Windows运行时类型的区别?
答:
C++内置类型:就是我们经常使用的int,double,float,bool等和C++语言相关的类型。
库类型:
Windows运行时类型:声明为public ref class 类名 sealed。“^”这个尖冒号的类型,称为可激活类

(又称为ref类)是可通过其他语言(如JavaScript)进行实例化的类,这个尖冒号告诉编译器将该类

创建为与Windows运行时兼容的类型,sealed关键字指定该类不可继承。

 


2. 如何使用类似STL的类型系统呢?
你需要明白2点:第一点是具体类型集合,第二点是类型实现的公共接口。
具体集合类型(如 Platform::Collections::Map 类 和 Platform::Collections::Vector 类)在

Platform::Collections 命名空间 中定义。

类型实现的公共接口在 Windows::Foundation::Collections 命名空间 (C++/CX) 中定义。

这里重点说的是如何使用WinRT中IVector类型。
在WinRT里面的Class1.h里面加上

#include <map>
using namespace Platform;
namespace WFC = Windows::Foundation::Collections;
namespace WFM = Windows::Foundation::Metadata;


[WFM::WebHostHidden]
public ref class Person sealed
{
public:
 Person(String^ name);
 void AddPhoneNumber(String^ type, String^ number);
 property WFC::IMapView<String^, String^>^ PhoneNumbers
 {
  WFC::IMapView<String^, String^>^ get();
 }

private:
 String^ m_name;
 std::map<String^, String^> m_numbers;
};

Person::Person(String^ name): m_name(name)
{
}

void Person::AddPhoneNumber(String^ type, String^ number)
{
 m_numbers[type] = number;
}

IMapView< String^, String^>^ Person::PhoneNumbers::get()
{
 // Simple implementation. 
 return ref new MapView< String^, String^>(m_numbers);
}


 

下一步在你的Blank App(XAML)的MainPage.xaml.cpp的构造函数里面你就可以这样调用上述类了,
如下:

Person^ p = ref new Person("Clark Kent");
p->AddPhoneNumber("Home", "425-555-4567");
p->AddPhoneNumber("Work", "206-555-9999");
String^ workphone = p->PhoneNumbers->Lookup("Work");


 

你需要注意的是IMapView和MapView这2个在不同的命名空间定义的。
一个是接口,另一个是实现。

同理,你也该明白IVector和Vector等接口和实现的类了。

 

 

 

 


 

(study_1) C:\Windows\System32>pip install fasttext -i https://pypi.tuna.tsinghua.edu.cn/simple Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting fasttext Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9f/3b/9a10b95eaf565358339162848863197c3f0a29b540ca22b2951df2d66a48/fasttext-0.9.3.tar.gz (73 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Requirement already satisfied: pybind11>=2.2 in c:\users\34201\.conda\envs\study_1\lib\site-packages (from fasttext) (3.0.0) Requirement already satisfied: setuptools>=0.7.0 in c:\users\34201\.conda\envs\study_1\lib\site-packages (from fasttext) (78.1.1) Requirement already satisfied: numpy in c:\users\34201\.conda\envs\study_1\lib\site-packages (from fasttext) (1.26.4) Building wheels for collected packages: fasttext Building wheel for fasttext (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for fasttext (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [135 lines of output] C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\setuptools\dist.py:599: SetuptoolsDeprecationWarning: Invalid dash-separated key 'description-file' in 'metadata' (setup.cfg), please use the underscore name 'description_file' instead. !! ******************************************************************************** Usage of dash-separated 'description-file' will not be supported in future versions. Please use the underscore name 'description_file' instead. (Affected: fasttext). By 2026-Mar-03, you need to update your project and remove deprecated calls or your builds will no longer be supported. See https://setuptools.pypa.io/en/latest/userguide/declarative_config.html for details. ******************************************************************************** !! opt = self._enforce_underscore(opt, section) C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated. !! ******************************************************************************** Please consider removing the following classifiers in favor of a SPDX license expression: License :: OSI Approved :: MIT License See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details. ******************************************************************************** !! self._finalize_license_expression() running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-310\fasttext copying python\fasttext_module\fasttext\FastText.py -> build\lib.win-amd64-cpython-310\fasttext copying python\fasttext_module\fasttext\__init__.py -> build\lib.win-amd64-cpython-310\fasttext creating build\lib.win-amd64-cpython-310\fasttext\util copying python\fasttext_module\fasttext\util\util.py -> build\lib.win-amd64-cpython-310\fasttext\util copying python\fasttext_module\fasttext\util\__init__.py -> build\lib.win-amd64-cpython-310\fasttext\util creating build\lib.win-amd64-cpython-310\fasttext\tests copying python\fasttext_module\fasttext\tests\test_configurations.py -> build\lib.win-amd64-cpython-310\fasttext\tests copying python\fasttext_module\fasttext\tests\test_script.py -> build\lib.win-amd64-cpython-310\fasttext\tests copying python\fasttext_module\fasttext\tests\__init__.py -> build\lib.win-amd64-cpython-310\fasttext\tests running build_ext building 'fasttext_pybind' extension creating build\temp.win-amd64-cpython-310\Release\python\fasttext_module\fasttext\pybind creating build\temp.win-amd64-cpython-310\Release\src "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include -IC:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include -Isrc -IC:\Users\34201\.conda\envs\study_1\include -IC:\Users\34201\.conda\envs\study_1\Include "-ID:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include" "-ID:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\ATLMFC\include" "-ID:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-ID:\Windows Kits\10\include\10.0.22621.0\ucrt" "-ID:\Windows Kits\10\\include\10.0.22621.0\\um" "-ID:\Windows Kits\10\\include\10.0.22621.0\\shared" "-ID:\Windows Kits\10\\include\10.0.22621.0\\winrt" "-ID:\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /EHsc /Tppython/fasttext_module/fasttext/pybind/fasttext_pybind.cc /Fobuild\temp.win-amd64-cpython-310\Release\python\fasttext_module\fasttext\pybind\fasttext_pybind.obj /EHsc /DVERSION_INFO=\\\"0.9.3\\\" fasttext_pybind.cc D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\string_view(12): warning STL4038: The contents of <string_view> are available only with C++17 or later. C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(40): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(40): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(41): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(41): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(46): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(46): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(75): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(75): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(76): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(76): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(78): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(78): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(78): error C2535: “fasttext::entry_type fasttext::Dictionary::getType(int32_t) const”: 已经定义或声明成员函数 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(77): note: 参见“fasttext::Dictionary::getType”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C2146: 语法错误: 缺少“)”(在标识符“str”的前面) C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C3646: “str”: 未知重写说明符 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C2059: 语法错误:“)” C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C2143: 语法错误: 缺少“;”(在“const”的前面) C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): error C2208: “const int”: 没有使用此类型进行定义的成员 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): warning C4091: “ ”: 没有声明变量时忽略“const int”的左侧 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(103): error C2039: "string_view": 不是 "std" 的成员 D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(23): note: 参见“std”的声明 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(103): error C2061: 语法错误: 标识符“string_view” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\detail\struct_smart_holder.h(1): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以 防止数据丢失 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\cast.h(711): warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(71): error C2662: “uint32_t fasttext::Dictionary::hash(const int)”: 不能将“this”指针从“_Ty2”转换为“fasttext::Dictionary &” with [ _Ty2=const fasttext::Dictionary ] python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(71): note: 转换丢失限定符 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(91): note: 参见“fasttext::Dictionary::hash”的声明 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(71): note: 尝试匹配参数列表“(std::string)”时 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(72): error C2665: “fasttext::Dictionary::getId”: 没有 重载函数可以转换所有参数类型 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(76): note: 可能是“int32_t fasttext::Dictionary::getId(const int,uint32_t) const” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(72): note: “int32_t fasttext::Dictionary::getId(const int,uint32_t) const”: 无法将参数 1 从“std::string”转换为“const int” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(72): note: 没有可用于执行该转换的用户定义的转换运算符, 或者无法调用该运算符 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(72): note: 尝试匹配参数列表“(std::string, uint32_t)”时 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(73): error C2664: “fasttext::entry_type fasttext::Dictionary::getType(int32_t) const”: 无法将参数 1 从“std::string”转换为“int32_t” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(73): note: 没有可用于执行该转换的用户定义的转换运算符, 或者无法调用该运算符 C:\Users\34201\AppData\Local\Temp\pip-install-0rn9nx1t\fasttext_373565729c8148cc9d2936be79e6fc8a\src\dictionary.h(77): note: 参见“fasttext::Dictionary::getType”的声明 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(73): note: 尝试匹配参数列表“(std::string)”时 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): error C2065: “ssize_t”: 未声明的标识符 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): error C2672: “pybind11::init”: 未找到匹配的重载 函数 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2550): note: 可能是“Ret pybind11::init(CFunc &&,AFunc &&)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): note: “pybind11::init”:“CFunc”的 模板 参数无效,应为类型 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2542): note: 或 “Ret pybind11::init(Func &&)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): note: “pybind11::init”:“Func”的 模板 参数无效 ,应为类型 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2530): note: 或 “pybind11::detail::initimpl::constructor<Args...> pybind11::init(void)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): note: “pybind11::init”:“Args”的 模板 参数无效 ,应为类型 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): error C2672: “pybind11::class_<fasttext::Vector>::def”: 未找到匹配的重载函数 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2161): note: 可能是“pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(pybind11::detail::initimpl::pickle_factory<Args...> &&,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2155): note: 或 “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(pybind11::detail::initimpl::factory<Args...> &&,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2148): note: 或 “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(const pybind11::detail::initimpl::alias_constructor<Args...> &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2141): note: 或 “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(const pybind11::detail::initimpl::constructor<Args...> &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2129): note: 或 “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(const T &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2104): note: 或 “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(const char *,Func &&,const Extra &...)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(172): note: “pybind11::class_<fasttext::Vector> &pybind11::class_<fasttext::Vector>::def(const char *,Func &&,const Extra &...)”: 应输入 3 个参数,却提供了 1 个 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): error C2065: “ssize_t”: 未声明的标识符 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): error C2065: “ssize_t”: 未声明的标识符 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): error C2672: “pybind11::init”: 未找到匹配的重载 函数 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2550): note: 可能是“Ret pybind11::init(CFunc &&,AFunc &&)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): note: “pybind11::init”:“CFunc”的 模板 参数无效,应为类型 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2542): note: 或 “Ret pybind11::init(Func &&)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): note: “pybind11::init”:“Func”的 模板 参数无效 ,应为类型 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2530): note: 或 “pybind11::detail::initimpl::constructor<Args...> pybind11::init(void)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): note: “pybind11::init”:“Args”的 模板 参数无效 ,应为类型 python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): error C2672: “pybind11::class_<fasttext::DenseMatrix>::def”: 未找到匹配的重载函数 C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2161): note: 可能是“pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(pybind11::detail::initimpl::pickle_factory<Args...> &&,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2155): note: 或 “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(pybind11::detail::initimpl::factory<Args...> &&,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2148): note: 或 “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(const pybind11::detail::initimpl::alias_constructor<Args...> &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2141): note: 或 “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(const pybind11::detail::initimpl::constructor<Args...> &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2129): note: 或 “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(const T &,const Extra &...)” C:\Users\34201\AppData\Local\Temp\pip-build-env-n2a89dj4\overlay\Lib\site-packages\pybind11\include\pybind11\pybind11.h(2104): note: 或 “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(const char *,Func &&,const Extra &...)” python/fasttext_module/fasttext/pybind/fasttext_pybind.cc(186): note: “pybind11::class_<fasttext::DenseMatrix> &pybind11::class_<fasttext::DenseMatrix>::def(const char *,Func &&,const Extra &...)”: 应输入 3 个参数,却提供了 1 个 error: command 'D:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.39.33519\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for fasttext Failed to build fasttext ERROR: Failed to build installable wheels for some pyproject.toml based projects (fasttext)
07-23
在 **C++/WinRT** 中调用 `BluetoothLEDevice` 的 **GATT 接口**,可以实现对 BLE 设备的服务(Service)、特征值(Characteristic)进行读取、写入、订阅等操作。以下是完整的实现步骤代码示例。 --- ### ✅ 前提条件 - 开发环境:Visual Studio 2019 或以上版本 - 项目类型:C++/WinRT 项目(如 Windows 运行时组件或控制台应用) - 操作系统:Windows 10 16299 或更高版本 - 权限:应用需具有蓝牙权限(可在清单中添加) --- ### 🛠 实现步骤 #### 1. 获取 `BluetoothLEDevice` 实例 使用 `BluetoothLEDevice::FromIdAsync()` 获取设备实例: ```cpp #include <winrt/Windows.Devices.Bluetooth.h> #include <winrt/Windows.Devices.Enumeration.h> #include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h> using namespace winrt; using namespace Windows::Devices::Bluetooth; using namespace Windows::Devices::Bluetooth::GenericAttributeProfile; using namespace Windows::Foundation; IAsyncAction GetGattServices(hstring const& deviceId) { BluetoothLEDevice bleDevice = co_await BluetoothLEDevice::FromIdAsync(deviceId); if (bleDevice) { co_await GetGattServicesAsync(bleDevice); } } ``` #### 2. 获取 GATT 服务(GattDeviceServices) ```cpp IAsyncAction GetGattServicesAsync(BluetoothLEDevice bleDevice) { GattDeviceServicesResult result = co_await bleDevice.GetGattServicesAsync(); if (result.Status() == GattCommunicationStatus::Success) { auto services = result.Services(); for (auto const& service : services) { std::wcout << L"服务 UUID: " << service.Uuid() << std::endl; co_await GetCharacteristicsAsync(service); } } } ``` #### 3. 获取特征值(Characteristics) ```cpp IAsyncAction GetCharacteristicsAsync(GattDeviceService service) { GattCharacteristicsResult result = co_await service.GetCharacteristicsAsync(); if (result.Status() == GattCommunicationStatus::Success) { auto characteristics = result.Characteristics(); for (auto const& ch : characteristics) { std::wcout << L"特征值 UUID: " << ch.Uuid() << std::endl; co_await ReadCharacteristicValueAsync(ch); } } } ``` #### 4. 读取特征值 ```cpp IAsyncAction ReadCharacteristicValueAsync(GattCharacteristic characteristic) { GattReadResult result = co_await characteristic.ReadValueAsync(); if (result.Status() == GattCommunicationStatus::Success) { auto reader = DataReader::FromBuffer(result.Value()); std::wstring value = reader.ReadString(result.Value().Length()); std::wcout << L"特征值内容: " << value << std::endl; } } ``` #### 5. 写入特征值 ```cpp IAsyncAction WriteCharacteristicValueAsync(GattCharacteristic characteristic, hstring const& value) { DataWriter writer; writer.WriteString(value); auto buffer = writer.DetachBuffer(); GattCommunicationStatus status = co_await characteristic.WriteValueAsync(buffer); if (status == GattCommunicationStatus::Success) { std::wcout << L"写入成功" << std::endl; } } ``` #### 6. 订阅通知(特征值变化通知) ```cpp void SubscribeToNotifications(GattCharacteristic characteristic) { characteristic.ValueChanged([](GattCharacteristic const&, GattValueChangedEventArgs const& args) { std::wcout << L"收到特征值更新:" << args.CharacteristicValue().Length() << std::endl; }); co_await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue::Notify); } ``` --- ### ✅ 完整调用示例 ```cpp int main() { init_apartment(); hstring deviceId = L"你的设备ID"; GetGattServices(deviceId).get(); std::wcout << L"按任意键退出..." << std::endl; std::wcin.get(); } ``` --- ### ⚠️ 注意事项 - 所有 WinRT BLE 操作必须是 **异步(async)** 的 - 需要确保项目启用了 **C++/WinRT** 支持(通过项目模板或 NuGet 包) - 使用 `co_await` 需要编译器支持 C++20 协程 - 调用 BLE API 的线程必须是 **STA 线程** --- ### 🔚 总结 | 功能 | 是否支持 | |------|----------| | 获取 BluetoothLEDevice | ✅ | | 获取 GATT 服务 | ✅ | | 读写特征值 | ✅ | | 订阅通知 | ✅ | | 需要异步编程 | ✅ | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值