標籤:

C++/CLI,C++/CX,WRL 有什麼區別?

在UWP中,C++/CLI,C++/CX,WRL(Windows Runtime C++ Template Library)有什麼區別?在運行性能上有什麼差別?


C++/CLI是.net的。C++/CX可以看成是C++/CLI的native winrt移植。WRL是通過封裝成庫的方式把winrt的東西暴露給標準C++。

其實所有winrt都可以用標準C++來表達,只是那樣的話代碼會很長。舉個例子,獲取app local folder,用C++/CX只要一行:

folder = ApplicationData.Current.LocalFolder

用標準C++就得這樣:

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Storage;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);

ComPtr& appDataStatics;
GetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_ApplicationData).Get(), appDataStatics);

ComPtr& appData;
appDataStatics-&>get_Current(appData);

ComPtr& localFolder;
appData-&>get_LocalFolder(localFolder);

ComPtr& storageItem;
localFolder.As(storageItem);

HString folderName;
storageItem-&>get_Path(folderName.GetAddressOf());

folder = folderName.GetRawBuffer(nullptr);

這還是在忽略錯誤處理和用了WRL的情況下,否則會更長。

效率方面,毫無疑問C++/CLI最慢,因為不是native。C++/CX比WRL慢一點點。另外,我經常發現C++/CX編譯出來後要比WRL的binary大10-20%。


補充一下,現在標準C++也有WinRT的language projection了,通過頭文件實現的,用起來比WRL方便多了,不想用C++/CX的可以考慮下。

Microsoft/cppwinrt

據說Windows團隊已經開始用這個給Windows寫WinRT組件了。


參考:

1. stackoverflow How can I develop universal windows 10 apps with pure C++ and not C++/CLI? (2015年9月)

C++/CLI (/CLR) a.k.a. Managed C++ is not supported for the universal Windows apps platform, and is not supported for Windows 8 Store or Windows phone 8 either.

What you are seeing are C++/CX (/ZW) language extensions that are used on MSDN and in most of the C++ samples to consume WinRT APIs. The confusion you are having is common because the same language keywords from C++/CLI were reused when implementing C++/CX. There is no .NET Runtime, no garbage collection, and no interpreted code as there is in C++/CLI. See the Visual C++ Team blog series C++/CX Part 0 of [n]: An Introduction for more on the history of C++/CX.

You do not have to use C++/CX to author or consume WinRT APIs, but they are much easier to code against using C++/CX language extensions than pure C++. You can make use of the C++ template library Windows Runtime Library (WRL) rather than C++/CX, but you will mostly have to figure out how on your own as there are very few samples that use it and MSDN presumes you are going to use C++/CX.

The standard samples make use of Microsoft::WRL::ComPtr smart-pointer when dealing with non-WinRT COM APIs like Direct3D, but generally that"s the only part of WRL used by the standard samples. I make use of WRL in DirectX Tool Kit to consume some specific WinRT APIs if you want to see some limited examples of using it instead of C++/CX.

Authoring WinRT APIs with WRL also requires a lot of manual sync work with MSIL files to generate the required metadata (but not code). If you are an expert in ATL, you would be equipped to use WRL for authoring WinRT APIs, but otherwise it"s likely not worth the productivity hit to avoid C++/CX entirely.

That said, you can easily isolate C++/CX usage to specific modules in your application. I do this in myDirect3D UWP Visual Studio template so that the majority of the code is in fact "pure" C++.

2. 演練:使用 WRL 和 Media Foundation 創建 Windows 應用商店應用程序(win8.x)

大多數情況下,你可以使用 Visual C++ 組件擴展 (C++/CX) 來創建 Windows 運行時組件。 (有關詳細信息,請參閱 Visual C++ 語言參考 (C++/CX)。) 但是,有時候需要使用 WRL。 例如,在為 Microsoft 媒體基礎創建媒體擴展時,必須創建一個可同時實現 COM 介面和 Windows 運行時介面的組件。 因為 C++/CX 只能創建 Windows 運行時對象,而要創建媒體擴展,就必須使用 WRL,因為它可以同時實現 COM 介面和 Windows 運行時介面。

3. VS2015中,DirectX12通用應用模板項目用的是C++/CX

4. msdn 在 Hilo(使用 C++ 和 XAML 的 Windows 應用商店應用)中編寫現代 C++ 代碼

由於 XAML UI 框架是採用 C++ 編寫的,因此當你與 C++ 應用中的 XAML 互操作時不會導致封送開銷。

WRL 是用於創建和使用 Windows 運行時 API 的一種編譯器不可知的方法。你可以使用 WRL 代替 C++/CX 語法。使用它,你可以優化代碼的性能或者針對特定方案對代碼進行優化。它還支持不使用異常的應用開發方法。


推薦閱讀:

對IT不是很在行的人,已有Windows10,再安裝一個Ubuntu有什麼用呢,這麼做的意義在哪裡?
使用手機熱點上網時,如何避免win10自動更新消耗流量?
Windows 10 免費之舉考慮過先前已購買 Windows 8 用戶的感受嗎?
win10教育版如何購買?售價多少?
Windows10系統文件怎麼這麼多?

TAG:C | Windows10 | CCX |