设为首页 收藏本站
查看: 936|回复: 0

重新想象 Windows 8 Store Apps (71)

[复制链接]
YunVN网友  发表于 2015-5-22 07:08:18 |阅读模式
  [源码下载]




重新想象 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
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










  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
#include
#include
#include
#include
#include
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           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
#include
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 device;
ComPtr context;
DX::ThrowIfFailed(
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
0,
creationFlags,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
&device,
&m_featureLevel,
&context
)
);
DX::ThrowIfFailed(device.As(&m_d3dDevice));
}
// 获取屏幕分辨率
Point Helper::ScreenResolution::get()
{
ComPtr dxgiDevice;
DX::ThrowIfFailed(m_d3dDevice.As(&dxgiDevice));
ComPtr dxgiAdapter;
DX::ThrowIfFailed(dxgiDevice->GetAdapter(&dxgiAdapter));
IDXGIOutput * pOutput;
if (dxgiAdapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND)
{
DXGI_OUTPUT_DESC desc;
pOutput->GetDesc(&desc);
return Point(desc.DesktopCoordinates.right, desc.DesktopCoordinates.bottom);
}
return Point(0, 0);
}
  
2、调用者
CPP/GetResolution.xaml










  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
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69314-1-1.html 上篇帖子: Windows 8 开发之动态磁贴和辅助磁贴(二级磁贴) 下篇帖子: 重新想象 Windows 8 Store Apps (57)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表