[源码下载]

重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++

作者: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", );
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(, ).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(, ).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

/*
* 注意:
* 由于需要 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>
#include <windows.ui.xaml.media.dxinterop.h> using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace WindowsRuntimeComponent;
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

<Page
x:Class="XamlDemo.CPP.GetResolution"
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/GetResolution.xaml.cs

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

OK
[源码下载]

重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  3. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  4. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  5. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  6. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  7. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  8. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  9. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

随机推荐

  1. Java对象生命周期

    [TOC] 1. 创建阶段(Created) 为对象分配存储空间 开始构造对象 从父类到子类对static成员进行初始化 父类成员变量按照顺序初始化,递归调用父类的构造方法 子类成员变量按照顺序初始化 ...

  2. 【Cocos2d-x 3.X 资源及脚本解密】

    加密就不用说了,看上一篇2.X加密的方式,怎么弄都可以.的保证解密规则就行: 现在重点说3.X解密: 在新的3.X引擎中官方整合了大部分获取资源的方法,最终合成一个getdata: 可以从源码,和堆栈 ...

  3. WIN7,WIN8,WIN8.1,64位客户端使用32位的ODBC配置

    运行64位的ODBC管理器,点开始-->运行-->odbcad32回车即可打开,但打开后看不到32位的驱动, 如果要运行32位的ODBC管理器,该怎么办呢,其实很简单, 只要执行C:\Wi ...

  4. ITF Demo代码(用VBScript构建的接口测试框架)

    ITF Demo代码(用VBScript构建的接口测试框架) http://blog.csdn.net/testing_is_believing/article/details/20872629

  5. Ubuntu 16.04 LTS更新

    Canonical今天正式发布了新版的Ubuntu系统,针对PC.笔记本.上网本.平板和智能手机各类设备.这次的Ubuntu 16.04代号为Xenial Xerus——这个代号是由Canonical ...

  6. linux标准daemon编写方式

    daemon定义 运行在后台的程序,通常不需要与用户进行交互的. 任何父进程id是0的通常是kernel进程,作为系统启动的一部分,除了init是用户态的命令. 规则 第一件事情是调用umask设置文 ...

  7. SQL Server 2008 R2 开启数据库远程连接

    今天要测试一个.net系统~因为配置的数据库是SQL Server~我就不得不安装SQL Server 2008 R2~现在我们就一起来看看SQL Server 2008 R2是如何打开远程连接端口1 ...

  8. Eclipse 常用最新插件.标记

    Properties Editor     编辑java的属性文件,并可以自动存盘为Unicode格式 http://marketplace.eclipse.org/content/propertie ...

  9. 移动端前端框架UI库(Frozen UI、WeUI、SUI Mobile)

    Frozen UI 自述:简单易用,轻量快捷,为移动端服务的前端框架. 主页:http://frozenui.github.io/ 开发团队:QQVIP FD Team Github:https:// ...

  10. ReentrantLock的使用

    class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondi ...