Download - Windows ストアアプリを C++/CX で作る
Windows ストアアプリをC++/CX で作るHokuriku.NET vol.122013/8/3 Sat
遥佐保(はるか・さお)
はじめに
@hr_sao
コミュニティ• Room metro (大阪)• ExceptionalC++ 読書会(大阪)• SQLWorld (大阪)Microsoft MVP for Client App Dev
[Jan,2010 - Dec,2013]
自己紹介
C++/CX に対する理解度を深める
本日の目的
網羅性があるわけではありません個人の体験と志向に基づきます
注意点
1. How about C++/CX
2. Windows Store Apps
3. grammar
4. How to program
5. Problem
6. summary
Topics
1. How about C++/CX
メリット1. Portability
Linux と Windows で同じコードを動かす
2. Reusability
既存ライブラリ使いたい3. Performance
.NET がリッチすぎる
Now… use cases for C++
こういうところに憧れる… ?
例えば、 Windows で C++ を使う人の本音は• ファイルの操作、もっと楽に出来ないのかなぁ• 表示といえば、 MFC/DirectX/GDI… なんでこんなにコー
ディング量多いの… (  ̄ д  ̄ ) エー• いつも「それ C# がよしなにやってくれるよ」と言われる• Java/C# 使いたくない!!
そこでC++の拡張です!
Why Language Extensions?
C++ 言語の拡張 (/ZW オプション )
Visual C++ コンパイラの拡張機能です C++11 対応 C++/CLI のサブセットに相当しますWindows ストアアプリの作成用 ※ストアアプリ以外でも利用可能
C++/CXCLIはマネージ
CXはネイティブ
2. Windows Store Apps
Windows 8 デバイス上で動作する新しい種類のアプリケーション• ウィンドウは1つ• タッチ UI が快適• アプリ同士の連携• アプリバー・チャームバー• タイル
Windows Store Appshttp://msdn.microsoft.com/ja-jp/library/windows/apps/hh974576.aspx
WinRT は API – 言語単なるインタフェース 実体は DLL
Desktop apps
CC++
Win32
C#VB
.NET SL
HTMLJavaScrip
t
Internet Explorer
Windows store apps
Windows Kernel Services
Communication
& Data
Devices & Printing
WinRT APIs
Graphics & Media
Application Model
XAMLJavaScrip
t
HTML / CSS
C/C++ C#/VB
DirectX
デスクトップからも呼べるよ
※ XAML/HTML系以外
Windows Runtime を各言語から利用
機能C++/CX名前空間
C#/VB名前空間
JavaScriptオブジェクト
Windows Runtime
Windows Windows Windows
ABI 互換用 Platform System WinJS
C++/CX ⇔ C#/VB
JavaScript ⇒ C++/CX , C#/VB
Windows Runtime APIs
Windows Kernel Services
Windows Runtime APIs
Communication & Data
Devices & Printing
Graphics & Media
Application Model
Application binary Interface (ABI)
• アプリケーションバイナリインタフェース (ABI)• バイナリレベルの互換が保障
• ABI を利用して API を提供
メタデータを記載しているWindows Runtime の実体は EXE や DLLHKLM\Software\Microsoft\WindowsRuntime\ActivatableClassId
WinMD で紐付け
Store apps Desktop
Visual Studio 2012 の C++ の世界
Windows8
DirectX
Windows Phone8
DirectX apps for phone
XAML for C++
WRL WTLMFC
WindowsRuntime のコンポーネントを作成できるテンプレートライブラリ• ABI レベルで動作• 直接 COM アクセスできる• C++/CX 使わなくてもいいデモ: ABI::Windows::UI::Notifications
(余談) Windows Runtime C++ Template Library (WRL)http://msdn.microsoft.com/ja-jp/library/vstudio/hh438466.aspx
OS process
WindowsStoreApps 環境 (UI XAML)http://msdn.microsoft.com/ja-jp/library/windows/apps/jj160326.aspx
Windows Kernel Services
Windows Runtime Win32 and COM CRT STL PPL
App container ( package manifest )
UI controls( XAML )
相互互換のためのC++ 言語拡張機能( 中間の変換無し ) C++ 実装クラス
( ネイティブ )VS プロジェクト
テンプレートC++/CX
混ぜるな危険!
Access Documents-Pakage.appxmanifest
3. grammar
• enum class {}
• Platform::Object^
• Platform::String^
• T^ 識別子• T% 識別子• ref new
C++/CX の文法抜粋http://msdn.microsoft.com/ja-jp/library/windows/apps/br212455.aspx
• Windows Runtime オブジェクトへのポインタ• C++ 標準の std::shared_ptr* と同等
• 参照カウンタを取る• コピー可能(所有元をカウント)• ムーブ可能(データコピーは発生しない)• GC 対象• C++ の場合、配列 / 文字列などムーブかコピーか選択出来
る
ポインター: ^http://msdn.microsoft.com/ja-jp/library/yk97tc08.aspx
• Windows Runtime オブジェクトを割り当て• C++ 標準の new 相当 =ref new
• C++ 標準の final 相当 =sealedref class Person sealed{ ← Person^ p = ref new Person();
public:
Person();
property Windows::Foundation::Metadata:: … }
ref new / sealedhttp://msdn.microsoft.com/ja-jp/library/0w2w91tf.aspx http://msdn.microsoft.com/ja-jp/library/windows/apps/hh699870.aspx
Windows Runtime で利用する文字コードは UTF-16
• 内部処理は std::wstring or L“xxx”
• XAML/ 外部公開は Platform::String^ // 直接代入 Platform::String^ str1 = “aaa”; // String⇒wstring std::wstring ws1( str1->Data() ); // wstring⇒String Platform::String^ str2 = ref new String( ws1.c_str());
文字列: String
<Button x:Name="btn00" Content="Cleck me :)"/>
XAML Obj を利用可能な pointer に
void ::StoreApp1::MainPage::InitializeComponent()
{ …
btn00 = safe_cast<::Windows::UI::Xaml::Controls::Button^>
(static_cast<Windows::UI::Xaml::IFrameworkElement^>
(this)->FindName(L"btn00"));
… }
MainPage.g.hpp
MainPage.xamlAuto Generated
4. How to program
MSDN : C++ を使った Windows ストア アプリのためのロードマップhttp://msdn.microsoft.com/ja-jp/library/windows/apps/xaml/hh700360.aspx
Windows ストアアプリのための C++ 情報まとめ• concurrency::create_async …
• task – then(), when_all() …
• casablanca – C++ REST SDK bindings to HTTP, JSON, and URIs
ストアアプリで C++ を選択したら…
http://hilo.codeplex.com/
C++ と XAML を利用するための実践的サンプル
HiloWindows7の時は DirectXでした
今は C++/XAML版と、 JavaScript版があるよ
C++/CX プログラミングノウハウ• 型変換のオーバーヘッドに気を付ける• Ref クラスは相互運用を目的とする• 内部型を public ref クラスで公開しない …などなど
Hilo
Hilo で Windows ストアアプリの作り方を覚えたら、次は通信がしたくなる http://casablanca.codeplex.com/
Casablanca – C++ REST SDK
• REST サービス (Windows Vista/7/8, Linux) VS2010/2012, クラウドベースの通信サポート
• 非同期処理、ストリームバッファ• HTTP Client
• C++11 base
• Apache オープンソース
Casablanca とは
Azureなど
5. problem
実際には「 Windows ストアアプリ用」の設計を
理解する必要があります
• 型違いによるコンパイルエラー• ネイティブ C++ 型を public メンバとするア
プリケーション設計• だいたい C++ のふりしてさ… (*´Д` )• そもそもサンプルソースコードや解説が少ない様に感じる
学習してもつまづく部分が多いC++ という言語を知っているがゆえに、ストア
用設計に頭が切り替わりにくい
実際やってみると障害
public ref class MainPage sealed{
public:
MainPage();
private:
pplx::task<void> get_http_request();
};
例) Web から情報をとってきたい#include <pplx.h>#include <http_client.h>などインクルードしな
いといけない
using namespace concurrency;using namespace pplx;using namespace web::http::client;など宣言しないといけない
• MainPage.xaml.h に include ファイルを追加する
→ App.xaml.cpp でエラーが出る ( 場合もある )
• 全ての include ファイルを pch.h (プリコンパイルヘッダ)に固定化ヘッダファイルとして追加する
→コンパイル時メモリ不足になる ( 場合もある )
例)ヘッダファイルに追加したい
MainPage.xaml に通信処理をまるっと入れようとしたのが間違い
ネイティブで処理できる処理と、Windows ストアアプリの処理の境界は?
例)何がダメだったのか?
summary
• C++/CX は Visual C++ コンパイラの拡張機能
コンパイルオプションは /ZW
• Windows Runtime API は ABI 互換• C++ ネイティブと C++/CX の役割は異なる
public 部分→ Platform名前空間
• 情報は多いが、実際には慣れが必要
まとめ