34
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

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

Embed Size (px)

Citation preview

Page 1: 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

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

Page 3: 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

#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;

}

Page 4: 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

#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();}

Page 5: 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

#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();}

Page 6: 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

#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)){

}

Page 7: 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

#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();}

Page 8: 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

#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);

}

Page 9: 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
Page 10: 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
Page 11: 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
Page 12: 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
Page 13: 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
Page 14: 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

#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;}

Page 15: 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
Page 16: 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

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);

Page 17: 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
Page 18: 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
Page 19: 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

Provides simple and well-designed .NET APIs specifically tailored for development of Windows Store apps

Page 20: 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

#include <iostream> using namespace std;using namespace Platform; int main(Array<String^>^ args){    String^ message = "Hello World!";    wcout << message->Data() << endl;    return 0;}

Page 21: 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

int main(void); int main(); int main(int argc, char** argv); int main(int argc, char* argv[]);

Page 22: 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

namespace CppCx{    ref class MyRefClass sealed    {};     ref struct MyRefStruct sealed    {};     value class MyValueClass sealed    {};     value struct MyValueStruct sealed    {};     interface struct IMyInterface    {};}

Page 23: 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

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__"();");        }         };

}

Page 24: 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

[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;}

Page 25: 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

ref class MyRefClass sealed{};  ref struct MyRefStruct sealed{};

value class MyValueClass sealed{};  value struct MyValueStruct sealed{};

Page 26: 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

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;}

Page 27: 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

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);        }    };}

Page 28: 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

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    {};}

Page 29: 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

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;}

Page 30: 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

ref class Foo {    Platform::String _name;public:         property Platform::String^ Name    {        Platform::String^ get()         { return _name; }         void set(Platform::String^ value)         { _name = value; }    } };

Page 31: 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

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;}

Page 32: 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

Platform::String^ Name{    void set(Platform::String^ value)    {        if (value->IsEmpty())        {            throw ref new Platform::InvalidArgumentException();        }        _name = value;    }     Platform::String^ get()    {        return _name;    }}

Page 33: 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

#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;

}