20. 使用 Win32 API 以及 COM
• 追求極致的效能
• 移植過去的程式
• 只有部份的 Win32 API 及 COM 可以在 Metro Style App
中使用
– WACK 可以協助確認是否可以用
– 參考可以使用的列表:
http://msdn.microsoft.com/en-us/library/windows/apps/br205753.aspx
– 或是看標頭檔是否有標示:
#pragma region Application Family
#pragma region Desktop Family
21. 使用 COM
• 透過 Windows Runtime C++ Template Library (WRL)來
呼叫
ComPtr<interface_name> ptr;
HRESULT hr = CoCreateInstanceEx(CLSID_XXX...);
if (FAILED(hr))
{
// process error
}
• 自己實作的 COM 物件必須實作 IInspectable 介面,並且
由 Microsoft::WRL::Details::RuntimeClass 實作物件
• 除非有必要使用的 COM,否則不建議這麼做
22. 範例: 使用 MSXML
// 實作 IXMLHttpRequest2Callback 介面
class HttpRequestCallback
: public
RuntimeClass<RuntimeClassFlags<ClassicCom>, IXMLHTTPRequest2Callback, Ft
mBase>
{
...
// 實作收到回應的回呼函式
IFACEMETHODIMP
OnResponseReceived(IXMLHTTPRequest2*, ISequentialStream*
pResponseStream)
{
...
}
...
}
27. 使用 AMP
• #include <amp.h>
concurrency
– array array_view
– extent index
– accelerator accelerator_view
parallel_for_each
restrict
– Asks compiler to check your code is ok for GPU (DirectX)
28. Array 相加範例
void AddArrays(int n, int * #include <amp.h>
pA, int * pB, int * pSum) using namespace concurrency;
{ void AddArrays(int n, int * pA, int
for (int i=0; i<n; i++) * pB, int * pSum)
{ {
array_view<int,1> a(n, pA);
pSum[i] = pA[i] + pB[i]; array_view<int,1> b(n, pB);
} array_view<int,1> sum(n, pSum);
}
parallel_for_each(
sum.extent,
[=](index<1> i)
restrict(amp)
{
sum[i] = a[i] + b[i];
}
);
}
32. 用 C++ 的理由
• 只會寫 C/C++ (?)
• 移植 C/C++-based library
– Using SQLite in a Metro Style App
• 為了更好的效能
– 針對平台的加速
– 使用 AMP 利用 GPU 做平行運算
– 操作 COM
• 更極致的效能調校
• 使用 DirectX 11 開發遊戲
33. 參考資源
• Visual C++ Language Reference (C++/CX)
http://msdn.microsoft.com/en-us/library/windows/apps/hh699871.aspx
• Developing Windows 8 Metro style apps with C++
http://channel9.msdn.com/Events/Windows-Camp/Developing-Windows-8-Metro-style-apps-in-Cpp
• Win32 and COM API
http://msdn.microsoft.com/en-us/library/windows/apps/br205757
• AMP MSDN Library
http://msdn.microsoft.com/en-us/library/hh265137(v=vs.110)
Native Concurrency Blog
http://blogs.msdn.com/b/nativeconcurrency/
• Developing games
http://msdn.microsoft.com/en-us/library/windows/apps/hh452744.aspx
• Visual C++ Team Blog
http://blogs.msdn.com/b/vcblog/