[源码下载]

与众不同 windows phone (48) - 8.0 其它: C# 调用 C++

作者:webabcd

介绍
与众不同 windows phone 8.0 之 其它

  • C# 中调用 Windows Phone Runtime Component(C++)
  • 让 Windows Phone Runtime Component(C++) 作为代理以调用 DLL(C++)
  • 通过 C++ 和 D3D 获取屏幕分辨率

示例
一、演示如何在 C# 中调用 Windows Phone Runtime Component(C++),以及 Windows Phone Runtime Component(C++) 如何作为代理调用 DLL(C++)
1、PhoneDLL 项目
PhoneDLL.h

#pragma once

// 用于演示 C# 调用 Windows Phone Dynamic Link Library(C++) 中的函数(需要通过 Windows Phone Runtime Component 做为代理)
extern "C" _declspec(dllexport) int Add(int x, int y);

PhoneDLL.cpp

#include "pch.h"
#include "PhoneDLL.h" int Add(int x, int y)
{
return x + y;
}

2、WPRuntimeComponent 项目
WPRuntimeComponent.h

#pragma once
#include <windows.h> using namespace Platform; namespace WPRuntimeComponent
{
public ref class WindowsPhoneRuntimeComponent sealed
{
public:
// 用于演示 C# 调用 Windows Phone Runtime Component(C++) 中的函数
int Add(int x,int y); // 用于演示通过此 Windows Phone Runtime Component 做为代理,然后调用 Windows Phone Dynamic Link Library(C++) 中的函数
typedef int(*myAdd)(int x,int y);
int Add2(int i,int j);
};
}

WPRuntimeComponent.cpp

#include "pch.h"
#include "WPRuntimeComponent.h" using namespace WPRuntimeComponent;
using namespace Platform; int WindowsPhoneRuntimeComponent::Add(int x, int y)
{
return x + y;
} // 作为代理,调用 PhoneDLL.dll 中的函数
int WindowsPhoneRuntimeComponent::Add2(int i,int j)
{
HINSTANCE hDll = LoadPackagedLibrary(L"PhoneDLL.dll",);
myAdd add = (myAdd)GetProcAddress(hDll, "Add"); int result = add(i, j); FreeLibrary(hDll); return result;
}

3、调用者
CPP/Demo.xaml

<phone:PhoneApplicationPage
x:Class="Demo.CPP.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True"> <Grid Background="Transparent">
<StackPanel> <TextBlock x:Name="lblMsg" TextWrapping="Wrap" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

CPP/Demo.xaml.cs

/*
* 演示如何在 C# 中调用 Windows Phone Runtime Component(C++),以及 Windows Phone Runtime Component(C++) 如何作为代理调用 DLL(C++)
*
*
* 注:
* 1、Windows Phone Runtime Component(C++) 项目参见:WPRuntimeComponent 项目
* 2、DLL(C++) 项目参见:PhoneDLL 项目
* 3、将 PhoneDLL.dll 复制到本项目的根目录下,以便 WPRuntimeComponent 项目调用
*/ using System;
using System.Windows.Navigation;
using Microsoft.Phone.Controls; namespace Demo.CPP
{
public partial class Demo : PhoneApplicationPage
{
public Demo()
{
InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 引用 Windows Phone Runtime Component 项目
WPRuntimeComponent.WindowsPhoneRuntimeComponent component = new WPRuntimeComponent.WindowsPhoneRuntimeComponent(); // 调用 Windows Phone Runtime Component(C++) 中的函数
lblMsg.Text = "调用 Windows Phone Runtime Component 中的函数:" + component.Add(, ).ToString();
lblMsg.Text += Environment.NewLine; // 调用 DLL(C++) 中的函数,方式:Windows Phone Runtime Component(C++) 作为一个代理调用 DLL(C++),然后 C# 调用 Windows Phone Runtime Component(C++)
lblMsg.Text += "调用 Windows Phone Runtime Component 中的函数(其仅作为一个代理,实际调用的是 PhoneDLL 中的函数):" + component.Add2(, ).ToString(); base.OnNavigatedTo(e);
}
}
}

二、演示如何在 C# 中调用 Windows Phone Runtime Component(C++ & D3D),从而获取屏幕的分辨率
1、WPRuntimeComponent 项目
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> namespace DX
{
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
// 抛出 DirectX API 的错误
throw Platform::Exception::CreateException(hr);
}
}
} namespace WPRuntimeComponent
{
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

/*
* 注意:
* 由于需要 D3D,所以需要在 项目属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项 中增加“d3d11.lib”
*
* 参考:
* http://blogs.microsoft.co.il/blogs/tomershamam/archive/2012/07/24/get-screen-resolution-in-windows-8-metro-style-application.aspx
*/ #include "pch.h"
#include "Helper.h"
#include <agile.h> using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace WPRuntimeComponent;
using namespace Platform; Helper::Helper()
{
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; #if defined(_DEBUG)
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
}; ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
DX::ThrowIfFailed(
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
,
creationFlags,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
&device,
&m_featureLevel,
&context
)
); DX::ThrowIfFailed(device.As(&m_d3dDevice));
} // 获取屏幕分辨率
Point Helper::ScreenResolution::get()
{
ComPtr<IDXGIDevice> dxgiDevice;
DX::ThrowIfFailed(m_d3dDevice.As(&dxgiDevice)); ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(dxgiDevice->GetAdapter(&dxgiAdapter)); IDXGIOutput * pOutput;
if (dxgiAdapter->EnumOutputs(, &pOutput) != DXGI_ERROR_NOT_FOUND)
{
DXGI_OUTPUT_DESC desc;
pOutput->GetDesc(&desc);
return Point(desc.DesktopCoordinates.right, desc.DesktopCoordinates.bottom);
} return Point(, );
}

2、调用者
CPP/GetResolution.xaml

<phone:PhoneApplicationPage
x:Class="Demo.CPP.GetResolution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True"> <Grid Background="Transparent">
<StackPanel> <TextBlock x:Name="lblMsg" TextWrapping="Wrap" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

CPP/GetResolution.xaml.cs

/*
* 演示如何在 C# 中调用 Windows Phone Runtime Component(C++ & D3D),从而获取屏幕的分辨率
*
*
* 注:
* 用于获取屏幕分辨率的 Windows Phone Runtime Component(C++ & D3D) 项目参见:WPRuntimeComponent 项目
*/ using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Windows.Foundation; namespace Demo.CPP
{
public partial class GetResolution : PhoneApplicationPage
{
public GetResolution()
{
InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 引用 C++ 项目,实例化 Helper 类
WPRuntimeComponent.Helper helper = new WPRuntimeComponent.Helper(); // 调用 Helper 中的属性,得到屏幕分辨率
Point screenResolution = helper.ScreenResolution; // 显示分辨率
lblMsg.Text = string.Format("分辨率:{0}×{1}", screenResolution.X.ToString(), screenResolution.Y.ToString()); base.OnNavigatedTo(e);
}
}
}

OK
[源码下载]

与众不同 windows phone (48) - 8.0 其它: C# 调用 C++的更多相关文章

  1. 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector

    [源码下载] 与众不同 windows phone (34) - 8.0 新的控件: LongListSelector 作者:webabcd 介绍与众不同 windows phone 8.0 之 新的 ...

  2. 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask

    [源码下载] 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirec ...

  3. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  4. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  5. 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议

    [源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...

  6. 与众不同 windows phone (39) - 8.0 联系人和日历

    [源码下载] 与众不同 windows phone (39) - 8.0 联系人和日历 作者:webabcd 介绍与众不同 windows phone 8.0 之 联系人和日历 自定义联系人存储的增删 ...

  7. 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能

    [源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...

  8. 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频

    [源码下载] 与众不同 windows phone (41) - 8.0 相机和照片: 通过 AudioVideoCaptureDevice 捕获视频和音频 作者:webabcd 介绍与众不同 win ...

  9. 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片

    [源码下载] 与众不同 windows phone (42) - 8.0 相机和照片: 通过 PhotoCaptureDevice 捕获照片 作者:webabcd 介绍与众不同 windows pho ...

随机推荐

  1. file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did

    file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did 解决办法 ,如果你是用的服务器,可以参考这个办法,修改 ...

  2. VS2013 解决方案文件结构分析

    http://www.cnblogs.com/haogj/p/4248030.html Visual Studio 的解决方案文件是一个文本文件,其中的内容不是太复杂,有些时候 Visual Stud ...

  3. Java与邮件系统交互之使用Socket验证邮箱是否存在

    最近遇到一个需求:需要验证用户填写的邮箱地址是否真实存在,是否可达.和普通的正则表达式不同,他要求尝试链接目标邮箱服务器并请求校验目标邮箱是否存在. 先来了解 DNS之MX记录 对于DNS不了解的,请 ...

  4. Spark源码系列(八)Spark Streaming实例分析

    这一章要讲Spark Streaming,讲之前首先回顾下它的用法,具体用法请参照<Spark Streaming编程指南>. Example代码分析 val ssc = )); // 获 ...

  5. 图文详解远程部署ASP.NET MVC 5项目

    话外篇: 由于感觉自己的机器比较慢,配置不好,所以最近想把之前的项目部署到实验室的服务器上,但是由于常不在实验室,所以在想能不能远程部署.因此今天专门研究了一下具体的过程,下面和大家分享一下.本人新手 ...

  6. S5PV210之Sate210-F DIY硬件,移植uboot,kernel,android 活动现在已经进入实施阶段吗,欢迎广大网友参与 !

    大家一起来diy 超低价四核的exynos4412或者Cortex A8S5pv210开源开发板 商业版Sate210已经完成了好久了.Sate4412 也已经出来.但是这两个接口非常全,主要是针对企 ...

  7. 配置 Sublime Text 用 Node.js 执行 JavaScript 程序

    1. 首先到 nodejs.org 下载 Node.js 安装包并安装. 2. 打开 Sublime Text 2 编辑器.选择菜单 Tools --> Build System --> ...

  8. Unit Of Work--工作单元(二)

    回顾 上一篇我们根据工作单元的原理基于ADO.NET进行了简单的实现,但是当项目需求发生变化的时候,比如需要引入ORM框架又要兼容当前ADO.NET实现的方式时,先前的实现就无法满足这个需求了. 话就 ...

  9. Visual Studio与Eclipse与IntelliJ快捷键对比

  10. hibernate的多对多例子讲解(加图片)

    在hibernate中也有多对多的关系.但是这样关系执行的效率不高,所以我们可以通过两个多对1或者两个1对多来实现. 在现实生活中多对多的关系也比较常见.比如说老师和学生.一个老师有多个学生,一个学生 ...