Windows 8 Store Apps学习(71) - 其它: C# 调用 C++2014-09-02作者: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_APPint 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
<Pagex: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_LEVELm_featureLevel;Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;};}
Helper.cpp