试图在 Windows 运行时类型上调用 Close() 的 Visual C++ DLL 错误 C2039
Visual C++ DLL Error C2039 trying to call Close() on a Windows Runtime type
我正在尝试编写一个 DLL 以从 C# 应用程序访问仅 C++ 的方法 Windows.Devices.Bluetooth.BluetoothLEDevice.Close()。我似乎能够访问 class,但如果我尝试使用该方法,Visual Studio 将无法构建。它显示在您键入时出现的成员列表中,并且没有 Intellisense 错误,只有构建错误。
using namespace Windows::Devices::Bluetooth;
__declspec(dllexport) void CloseBleDeviceUnmanaged(BluetoothLEDevice^device)
{
if (device->ConnectionStatus == BluetoothConnectionStatus::Connected) //no complaints for a property
{
device->GetDeviceSelector(); //no complaints for a method either
device->Close(); //Error C2039 | 'Close': is not a member of 'Windows::Devices::Bluetooth::BluetoothLEDevice'
}
return;
}
我如何至少构建它?
(编辑:根据 Nico Zhu - MSFT 的回答删除了无关的语法问题)
CloseBleDeviceUnmanaged
方法的参数在你的例子中是Value Type
。当您将参数传递给方法时。该参数将生成一个副本。并且原始参数没有改变。请通过
引用类型参数如下。
void DevicesTool::CloseBleDeviceUnmanaged(Windows::Devices::Bluetooth::BluetoothLEDevice^device)
{
if (device->ConnectionStatus == BluetoothConnectionStatus::Connected)
{
device->GetDeviceSelector();
device->Close();
}
}
关于Close
方法,我已经在我这边重现了这个问题(目标平台版本16299),我已经向相关团队报告了。请关注跟帖更新
您无需执行任何操作即可从 C# 调用 Close
——只需将其放入 using(...)
块中,它会自动为您处理(关闭)该对象。如果异议的生命周期不适合 using
块,您可以直接调用 IDisposabe.Dispose()
。
对于编译器错误,这是 documented in MSDN,因为您永远不应该从 C++/CX 调用 Close
:
Close
methods aren't callable through Visual C++ component extensions (C++/CX) on Windows Runtime class instances where the class implemented IClosable
. Instead, Visual C++ component extensions (C++/CX) code for runtime classes should call the destructor or set the last reference to null.
我正在尝试编写一个 DLL 以从 C# 应用程序访问仅 C++ 的方法 Windows.Devices.Bluetooth.BluetoothLEDevice.Close()。我似乎能够访问 class,但如果我尝试使用该方法,Visual Studio 将无法构建。它显示在您键入时出现的成员列表中,并且没有 Intellisense 错误,只有构建错误。
using namespace Windows::Devices::Bluetooth;
__declspec(dllexport) void CloseBleDeviceUnmanaged(BluetoothLEDevice^device)
{
if (device->ConnectionStatus == BluetoothConnectionStatus::Connected) //no complaints for a property
{
device->GetDeviceSelector(); //no complaints for a method either
device->Close(); //Error C2039 | 'Close': is not a member of 'Windows::Devices::Bluetooth::BluetoothLEDevice'
}
return;
}
我如何至少构建它?
(编辑:根据 Nico Zhu - MSFT 的回答删除了无关的语法问题)
CloseBleDeviceUnmanaged
方法的参数在你的例子中是Value Type
。当您将参数传递给方法时。该参数将生成一个副本。并且原始参数没有改变。请通过
引用类型参数如下。
void DevicesTool::CloseBleDeviceUnmanaged(Windows::Devices::Bluetooth::BluetoothLEDevice^device)
{
if (device->ConnectionStatus == BluetoothConnectionStatus::Connected)
{
device->GetDeviceSelector();
device->Close();
}
}
关于Close
方法,我已经在我这边重现了这个问题(目标平台版本16299),我已经向相关团队报告了。请关注跟帖更新
您无需执行任何操作即可从 C# 调用 Close
——只需将其放入 using(...)
块中,它会自动为您处理(关闭)该对象。如果异议的生命周期不适合 using
块,您可以直接调用 IDisposabe.Dispose()
。
对于编译器错误,这是 documented in MSDN,因为您永远不应该从 C++/CX 调用 Close
:
Close
methods aren't callable through Visual C++ component extensions (C++/CX) on Windows Runtime class instances where the class implementedIClosable
. Instead, Visual C++ component extensions (C++/CX) code for runtime classes should call the destructor or set the last reference to null.