Windows 8 Store Apps學習(71)

來源:互聯網
上載者:User

作者:webabcd

介紹

重新想象 Windows 8 Store Apps 之 其它

C# 中調用 Windows Runtime Component(C++)

讓 Windows Runtime Component(C++) 作為代理以調用 DLL(C++)

通過 C++ 和 D3D 擷取螢幕解析度

樣本

一、示範如何在 C# 中調用 Windows Runtime Component(C++),以及 Windows Runtime Component(C++) 如何作為代理調用 DLL(C++)

1、WindowsDll 項目

WindowsDll.h

#pragma once    // 用於示範 C# 調用 Windows Dynamic Link Library(C++) 中的函數(需要通過 Windows Runtime Component 做為代理)extern "C" _declspec(dllexport) int Add(int x, int y);

WindowsDll.cpp

#include "pch.h"#include "WindowsDll.h"    // 注意:要想 C# 能調用此 dll,則必須要有以下這兩行(wp8 則不需要)#include "winapifamily.h""#define WINAPI_FAMILY WINAPI_PARTITION_APP    int Add(int x, int y){    return x + y;}

2、WindowsRuntimeComponent 項目

MyRuntimeComponent.h

#pragma once#include <windows.h>    namespace WindowsRuntimeComponent{    public ref class MyRuntimeComponent sealed    {    public:        // 用於示範 C# 調用 Windows Runtime Component(C++) 中的函數        int Add(int x, int y);            // 用於示範通過此 Windows Runtime Component 做為代理,然後調用 Windows Dynamic Link Library(C++) 中的函數        typedef int(*myAdd)(int x, int y);        int Add2(int i, int j);    };}

MyRuntimeComponent.cpp

#include "pch.h"#include "MyRuntimeComponent.h"    using namespace WindowsRuntimeComponent;    int MyRuntimeComponent::Add(int x, int y){    return x + y;}    // 作為代理,調用 WindowsDLL.dll 中的函數int MyRuntimeComponent::Add2(int i, int j){    HINSTANCE hDll = LoadPackagedLibrary(L"CPP/WindowsDLL.dll", 0);    myAdd add = (myAdd)GetProcAddress(hDll, "Add");        int result = add(i, j);        FreeLibrary(hDll);        return result;}

3、調用者

CPP/Demo.xaml

<Page    x:Class="XamlDemo.CPP.Demo"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:XamlDemo.CPP"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">        <Grid Background="Transparent">        <StackPanel Margin="120 0 0 0">                <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap"  />            </StackPanel>    </Grid></Page>

CPP/Demo.xaml.cs

/* * 示範如何在 C# 中調用 Windows Runtime Component(C++),以及 Windows Runtime Component(C++) 如何作為代理調用 DLL(C++) *  *  * 註: * 1、Windows Runtime Component(C++) 項目參見:WindowsRuntimeComponent 項目 * 2、DLL(C++) 項目參見:WindowsDll 項目 * 3、將 PhoneDLL.dll 複製到本項目的根目錄下,以便 WPRuntimeComponent 項目調用 */    using System;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;    namespace XamlDemo.CPP{    public sealed partial class Demo : Page    {        public Demo()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            // 引用 Windows Runtime Component 項目            WindowsRuntimeComponent.MyRuntimeComponent component = new WindowsRuntimeComponent.MyRuntimeComponent();                // 調用 Windows Runtime Component(C++) 中的函數            lblMsg.Text = "調用 Windows Runtime Component 中的函數:" + component.Add(10, 10).ToString();            lblMsg.Text += Environment.NewLine;                // 調用 DLL(C++) 中的函數,方式:Windows Runtime Component(C++) 作為一個代理調用 DLL(C++),然後 C# 調用 Windows Runtime Component(C++)            lblMsg.Text += "調用 Windows Runtime Component 中的函數(其僅作為一個代理,實際調用的是 WindowsDLL 中的函數):" + component.Add2(10, 10).ToString();                base.OnNavigatedTo(e);        }    }}

二、示範如何在 C# 中調用 Windows Runtime Component(C++ & D3D),從而擷取螢幕的解析度

1、WindowsRuntimeComponent 項目

Helper.h

/* * 注意: * 由於需要 D3D,所以需要在 項目屬性 -> 配置屬性 -> 連結器 -> 輸入 -> 附加依賴項 中增加“d3d11.lib” * * 參考: * http://blogs.microsoft.co.il/blogs/tomershamam/archive/2012/07/24/get-screen-resolution-in-windows-8-metro-style-application.aspx */    #pragma once    #include <wrl/client.h>#include <d3d11_1.h>#include <d2d1_1.h>#include <d2d1effects.h>#include <dwrite_1.h>#include <wincodec.h>    namespace DX{    inline void ThrowIfFailed(HRESULT hr)    {        if (FAILED(hr))        {            // 拋出 DirectX API 的錯誤            throw Platform::Exception::CreateException(hr);        }    }}    namespace WindowsRuntimeComponent{    public ref class Helper sealed    {    public:        Helper();            // 一個屬性,用於得到螢幕解析度        property Windows::Foundation::Point ScreenResolution        {            Windows::Foundation::Point get();        }        private:        D3D_FEATURE_LEVEL                                m_featureLevel;        Microsoft::WRL::ComPtr<ID3D11Device1>           m_d3dDevice;    };}

Helper.cpp

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.