340 likes | 448 Views
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. aka.ms/ roslyn. #include < windows.h > #include < shobjidl.h >
E N D
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> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. 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> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. if (SUCCEEDED(hr)) { MessageBox(NULL, pszFilePath, L"File Path", MB_OK); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return 0; } HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { CoUninitialize(); }
#include<windows.h> #include<shobjidl.h> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. if (SUCCEEDED(hr)) { MessageBox(NULL, pszFilePath, L"File Path", MB_OK); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return 0; } // CreatetheFileOpenDialogobject. hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen)); if (SUCCEEDED(hr)) { pFileOpen->Release(); }
#include<windows.h> #include<shobjidl.h> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. 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> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. 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> intWINAPIwWinMain(HINSTANCEhInstance, HINSTANCE, PWSTRpCmdLine, intnCmdShow) { HRESULThr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen; // CreatetheFileOpenDialogobject. 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); // Getthe file namefromthedialog box. if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. if (SUCCEEDED(hr)) { MessageBox(NULL, pszFilePath, L"File Path", MB_OK); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } return 0; } PWSTRpszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file nametotheuser. if (SUCCEEDED(hr)) { MessageBox(NULL, pszFilePath, L"File Path", MB_OK); CoTaskMemFree(pszFilePath); }
#pragmaonce namespaceWinRTComponentCpp { publicrefclassCalculatorsealed { public: Calculator(); intAdd(int a, int b); }; } #include"pch.h“ #include"Calculator.h" usingnamespaceWinRTComponentCpp; Calculator::Calculator(){} intCalculator::Add(inta, intb) { returna + b; }
Provides simple and well-designed .NET APIs specifically tailored for development of Windows Store apps
#include <iostream> usingnamespacestd; usingnamespacePlatform; intmain(Array<String^>^ args) { String^ message = "Hello World!"; wcout << message->Data() << endl; return0; }
intmain(void); intmain(); intmain(intargc, char** argv); intmain(intargc, char* argv[]);
namespaceCppCx { ref classMyRefClass sealed {}; ref structMyRefStruct sealed {}; value classMyValueClass sealed {}; value structMyValueStruct sealed {}; interface structIMyInterface {}; }
namespaceLearningCppCx { refclassFoosealed { 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__"();"); } voidPrint() { Console::WriteLine(Data); } propertyintData; private: ~Foo() { Console::WriteLine(__FUNCTION__"();"); } }; }
[MTAThread] intmain() { usingnamespaceLearningCppCx; Console::WriteLine("-- One --"); Foo^ one = ref newFoo(); one->Data = 42; Console::WriteLine("-- Two --"); Foo^ two = ref newFoo(one); Console::WriteLine("-- Three --"); Foo^ three = ref newFoo(); three = one; Console::WriteLine("-- Four --"); Foo^ four(one); Console::WriteLine("-- Five --"); Foo five(*one); return0; }
refclassMyRefClasssealed {}; refstructMyRefStructsealed {}; valueclassMyValueClasssealed {}; valuestructMyValueStructsealed {};
usingnamespacePlatform; value classFoo { public: intValue; }; [MTAThread] intmain() { // 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); return0; }
namespaceBoxingDemo { publicref classClass1 sealed { public: Class1(){} Platform::IBox<int>^ Multiply(Platform::IBox<int>^ a, Platform::IBox<int>^ b) { if(a == nullptr || b == nullptr) returnnullptr; else returnref newPlatform::Box<int>(a->Value * b->Value); } }; }
interface classIFoo { voidA(); voidB(); }; interface classIA { voidF(); }; interface classIB { voidF(); }; ref classC : IA, IB { public: virtualvoidF1() = IA::F {}; virtualvoidF2() = IB::F {}; }
interface IFoo { voidDoSomething(); }; refclassFoo : IFoo { virtualvoidDoSomething() = IFoo::DoSomething {} } [MTAThread] intmain() { auto f = refnewFoo(); IFoo^ if= f; f->DoSomething(); // BOOM; if->DoSomething(); // Funciona return0; }
ref classFoo { Platform::String _name; public: property Platform::String^ Name { Platform::String^ get() { return_name; } voidset(Platform::String^ value) { _name = value; } } };
namespaceCppCxDelegates { refclassFoo; publicdelegateString^ GetNameDelegate(Foo^ f); publicrefclassFoosealed { String^ _name; public: Foo() {} Foo(String^ name) { _name = name; } propertyString^ Name { String^ get() { return_name; } } }; } • [MTAThread] • intmain() • { • usingnamespaceCppCxDelegates; • auto instance = refnewFoo("Elemar"); • auto ldelegate = refnewGetNameDelegate( • [](Foo^ f){ returnf->Name; } • ); • auto lstr = ldelegate(instance); • wcout << lstr->Data() << endl; • return0; • }
Platform::String^ Name { voidset(Platform::String^ value) { if(value->IsEmpty()) { throwrefnewPlatform::InvalidArgumentException(); } _name = value; } Platform::String^ get() { return_name; } }
#include <amp.h> #include <iostream> intmain() { usingnamespacestd; usingnamespaceconcurrency; inta_data[] = {1, 2, 3, 4, 5}; intb_data[] = {6, 7, 8, 9,10}; intsum_data[5]; array_view<constint, 1> a(5, a_data); array_view<constint, 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(inti = 0; i < 5; i++) cout << sum[i] << "... "; return0; }
Obrigado! http://elemarjr.net @elemarjr facebook.com/elemarjr github.com/elemarjr elemarjr@gmail.com