Post on 24-Dec-2015
C# e C++/CX – O que há de novo nas linguagens para
suportar o desenvolvimento de aplicações modernas
Elemar Júnior (@elemarjr)Microsoft Visual C# MVP
http://elemarjr.net
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);if (SUCCEEDED(hr)){
CoUninitialize();}
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)){
pFileOpen->Release();}
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
// Show the Open dialog box.hr = pFileOpen->Show(NULL); if (SUCCEEDED(hr)){
}
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
pItem->Release();}
#include <windows.h>#include <shobjidl.h> int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow){
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr)){
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr)){
// Show the Open dialog box.hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.if (SUCCEEDED(hr)){
IShellItem *pItem;hr = pFileOpen->GetResult(&pItem);if (SUCCEEDED(hr)){
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}pItem->Release();
}}pFileOpen->Release();
}CoUninitialize();
}return 0;
}
PWSTR pszFilePath;hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user.if (SUCCEEDED(hr)){
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);CoTaskMemFree(pszFilePath);
}
#pragma once
namespace WinRTComponentCpp{
public ref class Calculator sealed{public:
Calculator();int Add(int a, int b);
};}
#include "pch.h“#include "Calculator.h"
using namespace WinRTComponentCpp;
Calculator::Calculator(){} int Calculator::Add(int a, int b){
return a + b;}
void MainPage::OnNavigatedTo(NavigationEventArgs^ e) { (void)e; // Unused parameter WinRTComponentCS::Calculator^ calculator = ref new WinRTComponentCS::Calculator(); this->AddResultTextBox->Text = calculator->Add(7, 3).ToString(); } protected override void OnNavigatedTo(NavigationEventArgs e) { var calculator = new WinRTComponentCpp.Calculator(); this.AddResultTextBox.Text = calculator.Add(4, 45).ToString(); } document.getElementById('CppResult').textContent = WinRTComponentCpp.Calculator().add(5, 7);
Provides simple and well-designed .NET APIs specifically tailored for development of Windows Store apps
#include <iostream> using namespace std;using namespace Platform; int main(Array<String^>^ args){ String^ message = "Hello World!"; wcout << message->Data() << endl; return 0;}
int main(void); int main(); int main(int argc, char** argv); int main(int argc, char* argv[]);
namespace CppCx{ ref class MyRefClass sealed {}; ref struct MyRefStruct sealed {}; value class MyValueClass sealed {}; value struct MyValueStruct sealed {}; interface struct IMyInterface {};}
namespace LearningCppCx{ ref class Foo sealed { internal: Foo(Foo%other) { Console::WriteLine(__FUNCTION__"(Foo%);"); Data = other.Data; } public: Foo(Foo^ other) { Console::WriteLine(__FUNCTION__"(Foo^);"); Data = other->Data; } Foo() { Console::WriteLine(__FUNCTION__"();"); } void Print() { Console::WriteLine(Data); } property int Data; private: ~Foo() { Console::WriteLine(__FUNCTION__"();"); } };
}
[MTAThread]int main(){ using namespace LearningCppCx; Console::WriteLine("-- One --"); Foo^ one = ref new Foo(); one->Data = 42; Console::WriteLine("-- Two --"); Foo^ two = ref new Foo(one); Console::WriteLine("-- Three --"); Foo^ three = ref new Foo(); three = one; Console::WriteLine("-- Four --"); Foo^ four(one); Console::WriteLine("-- Five --"); Foo five(*one); return 0;}
ref class MyRefClass sealed{}; ref struct MyRefStruct sealed{};
value class MyValueClass sealed{}; value struct MyValueStruct sealed{};
using namespace Platform; value class Foo{public: int Value;}; [MTAThread]int main(){ // instanciando um value type; Foo number; number.Value = 34; // boxing por atribuição Object^ boxed = number; // unboxing por cast Foo unboxed = safe_cast<Foo>(boxed); return 0;}
namespace BoxingDemo{ public ref class Class1 sealed { public: Class1(){} Platform::IBox<int>^ Multiply(Platform::IBox<int>^ a, Platform::IBox<int>^ b) { if(a == nullptr || b == nullptr) return nullptr; else return ref new Platform::Box<int>(a->Value * b->Value); } };}
interface class IFoo{ void A(); void B();};
interface class IA { void F(); };interface class IB { void F(); }; ref class C : IA, IB{public: virtual void F1() = IA::F {}; virtual void F2() = IB::F {};}
interface IFoo{ void DoSomething();}; ref class Foo : IFoo{ virtual void DoSomething() = IFoo::DoSomething {}} [MTAThread]int main(){ auto f = ref new Foo(); IFoo^ if = f; f->DoSomething(); // BOOM; if->DoSomething(); // Funciona return 0;}
ref class Foo { Platform::String _name;public: property Platform::String^ Name { Platform::String^ get() { return _name; } void set(Platform::String^ value) { _name = value; } } };
namespace CppCxDelegates{ ref class Foo; public delegate String^ GetNameDelegate(Foo^ f); public ref class Foo sealed { String^ _name; public: Foo() {} Foo(String^ name) { _name = name; } property String^ Name { String^ get() { return _name; } } };}
[MTAThread]int main() { using namespace CppCxDelegates; auto instance = ref new Foo("Elemar"); auto ldelegate = ref new GetNameDelegate( [](Foo^ f){ return f->Name; } ); auto lstr = ldelegate(instance); wcout << lstr->Data() << endl; return 0;}
Platform::String^ Name{ void set(Platform::String^ value) { if (value->IsEmpty()) { throw ref new Platform::InvalidArgumentException(); } _name = value; } Platform::String^ get() { return _name; }}
#include <amp.h>#include <iostream> int main(){ using namespace std; using namespace concurrency; int a_data[] = {1, 2, 3, 4, 5}; int b_data[] = {6, 7, 8, 9,10}; int sum_data[5]; array_view<const int, 1> a(5, a_data); array_view<const int, 1> b(5, b_data); array_view<int, 1> sum(5, sum_data); sum.discard_data(); parallel_for_each(sum.extent, [=] (index<1> i) restrict(amp) { sum[i] = a[i] + b[i]; }); for (int i = 0; i < 5; i++) cout << sum[i] << "... "; return 0;
}
Obrigado!http://elemarjr.net@elemarjrfacebook.com/elemarjrgithub.com/elemarjrelemarjr@gmail.com