观看 //BUILD 内容,我看到 WinRT API 可以由 C 代码使用:
我对 Win32 开发人员可以使用新的 C API 感到非常兴奋。
在哪里可以找到有关 C WinRT API 的信息?它比现有的 Win32 C API 更好在哪里?
WinRT 本质上是 COM,因此使用 C 中的 WinRT 组件就像使用 C 中的 COM 组件一样。像以前一样,您可以获得所有 WinRT 组件的 .idl 文件,以及从这些 .idl 文件生成的 .h 文件。 .h 文件包含 C++ 和 C 声明(根据需要用
#ifdef __cplusplus
括起来)。你可以#include它们并开始破解。
不过,这并不完全整洁,例如像这样的 C++/CX:
Windows::UI::Xaml::Controls::TextBlock^ tb = ...;
tb->Text = "Foo";
相当于这个普通的 C++:
Windows::UI::Xaml::Controls::ITextBlock* tb = ...;
HSTRING hs;
HRESULT hr = WindowsStringCreate(L"Foo", 3, &hs);
// check hr for errors
hr = tb->set_Text(hs);
// check hr for errors
tb->Release();
用 C 语言可以写成:
__x_Windows_CUI_CXaml_CControls_CITextBlock* tb = ...;
HRESULT hr;
HSTRING hs;
hr = WindowsCreateString(L"Foo", 3, &hs);
// check hr for errors
hr = __x_Windows_CUI_CXaml_CControls_CITextBlock_put_Text(tb, hs);
// check hr for errors
IUnknown_Release(tb);
在 Developer Preview 中查看“C:\Program Files (x86)\Windows Kits\8.0\Include\winrt”以查看 .idl 和 .h 文件。
Wine 有测试代码,可用作如何从 C: 调用 WinRT 的示例
#define COBJMACROS
#include "initguid.h"
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winstring.h"
#include "roapi.h"
#define WIDL_using_Windows_Foundation
#define WIDL_using_Windows_Foundation_Collections
#include "windows.foundation.h"
#define WIDL_using_Windows_Devices_Bluetooth
#include "windows.devices.bluetooth.h"
#include "wine/test.h"
#define check_interface( obj, iid ) check_interface_( __LINE__, obj, iid )
static void check_interface_( unsigned int line, void *obj, const IID *iid )
{
IUnknown *iface = obj;
IUnknown *unk;
HRESULT hr;
hr = IUnknown_QueryInterface( iface, iid, (void **)&unk );
ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr );
IUnknown_Release( unk );
}
static void test_BluetoothAdapterStatics(void)
{
static const WCHAR *default_res = L"System.Devices.InterfaceClassGuid:=\"{92383B0E-F90E-4AC9-8D44-8C2D0D0EBDA2}\" "
L"AND System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True";
static const WCHAR *bluetoothadapter_statics_name = L"Windows.Devices.Bluetooth.BluetoothAdapter";
IBluetoothAdapterStatics *bluetoothadapter_statics;
IActivationFactory *factory;
HSTRING str, default_str;
HRESULT hr;
INT32 res;
LONG ref;
hr = WindowsCreateString( bluetoothadapter_statics_name, wcslen( bluetoothadapter_statics_name ), &str );
ok( hr == S_OK, "got hr %#lx.\n", hr );
hr = RoGetActivationFactory( str, &IID_IActivationFactory, (void **)&factory );
WindowsDeleteString( str );
ok( hr == S_OK || broken( hr == REGDB_E_CLASSNOTREG ), "got hr %#lx.\n", hr );
if (hr == REGDB_E_CLASSNOTREG)
{
win_skip( "%s runtimeclass not registered, skipping tests.\n", wine_dbgstr_w( bluetoothadapter_statics_name ) );
return;
}
check_interface( factory, &IID_IUnknown );
check_interface( factory, &IID_IInspectable );
check_interface( factory, &IID_IAgileObject );
hr = IActivationFactory_QueryInterface( factory, &IID_IBluetoothAdapterStatics, (void **)&bluetoothadapter_statics );
ok( hr == S_OK, "got hr %#lx.\n", hr );
hr = IBluetoothAdapterStatics_GetDeviceSelector( bluetoothadapter_statics, NULL );
ok( hr == E_POINTER, "got hr %#lx.\n", hr );
hr = IBluetoothAdapterStatics_GetDeviceSelector( bluetoothadapter_statics, &str );
ok( hr == S_OK, "got hr %#lx.\n", hr );
hr = WindowsCreateString( default_res, wcslen(default_res), &default_str );
ok( hr == S_OK, "got hr %#lx.\n", hr );
hr = WindowsCompareStringOrdinal( str, default_str, &res );
ok( hr == S_OK, "got hr %#lx.\n", hr );
ok( !res, "got unexpected string %s.\n", debugstr_hstring(str) );
WindowsDeleteString( str );
WindowsDeleteString( default_str );
ref = IBluetoothAdapterStatics_Release( bluetoothadapter_statics );
ok( ref == 2, "got ref %ld.\n", ref );
ref = IActivationFactory_Release( factory );
ok( ref == 1, "got ref %ld.\n", ref );
}
START_TEST(bluetooth)
{
HRESULT hr;
hr = RoInitialize( RO_INIT_MULTITHREADED );
ok( hr == S_OK, "RoInitialize failed, hr %#lx\n", hr );
test_BluetoothAdapterStatics();
RoUninitialize();
}
https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/windows.devices.bluetooth/tests/bluetooth.c