Author: WEBABCD
Introduced
Re-imagine the other Windows 8 Store Apps
Call Windows Runtime Component (c + +) in C #
Let Windows Runtime Component (c + +) Act as proxies to invoke DLLs (C + +)
Get screen resolution through C + + and D3D
Example
First, demonstrates how to invoke Windows Runtime Component (c + +) in C #, and how Windows Runtime Component (c + +) invokes DLLs as proxies (c + +)
1. Windowsdll Project
WindowsDll.h
#pragma once
//for demonstrating functions in C # calling Windows Dynamic Link Library (c + +) (required to be represented through Windows Runtime Component)
extern "C" _declspec (dllexport) int Add (int x, int y);
WindowsDll.cpp
#include "pch.h"
#include "WindowsDll.h"
/Note: In order for C # to invoke this DLL, you must have the following two lines (WP8 not required)
#include " Winapifamily.h ""
#define winapi_family winapi_partition_app
int Add (int x, int y)
{return
x + y;
}
2. Windowsruntimecomponent Project
MyRuntimeComponent.h
#pragma once
#include <windows.h>
namespace windowsruntimecomponent
{public
ref class Myruntimecomponent sealed
{public
:
//For demonstrating functions
int Add (int x, int y, in C # calling Windows Runtime Component (c + +) );
Used to demonstrate the function
typedef int (*MYADD) in the Windows Dynamic Link Library (c + +) by using this Windows Runtime Component as a proxy, 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;
}
As a proxy, the function
int myruntimecomponent::add2 (int i, int j)
{
hinstance hDLL = WindowsDLL.dll is called Loadpackagedlibrary (L "Cpp/windowsdll.dll", 0);
Myadd add = (myadd) GetProcAddress (hDLL, "add");
int result = Add (i, j);
FreeLibrary (hdll);
return result;
}
3. Caller
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= "0 0 0" >
<textblock name= "lblmsg" fontsize= "14.667" textwrapping= "Wrap" />
</StackPanel>
</Grid>
</Page>
Cpp/demo.xaml.cs
* * Demonstrates how to invoke Windows Runtime Component (c + +) in C # and how Windows Runtime Component (c + +) can invoke DLLs as proxies (c + +) * * Note: * 1, Windows Runtime Component (c + +) projects See: Windowsruntimecomponent project * 2, DLL (c + +) project See: Windowsdll Project * 3, copy PhoneDLL.dll to this project
root directory so that the Wpruntimecomponent project calls/using System;
Using Windows.UI.Xaml.Controls;
Using Windows.UI.Xaml.Navigation; Namespace XamlDemo.CPP {public sealed partial class Demo:page {public Demo () {thi
S.initializecomponent (); } protected override void Onnavigatedto (NavigationEventArgs e) {//referencing Windows Runtime Co Mponent Project Windowsruntimecomponent.myruntimecomponent component = new Windowsruntimecomponent.myruntimecompone
NT (); Call functions in Windows Runtime Component (c + +) Lblmsg.text = "Invoke functions in Windows Runtime Component:" + Component. ADD (10, 10).
ToString ();
Lblmsg.text + = Environment.NewLine; //Invoke the functions in DLL (c + +), by: Windows Runtime Component (c + +) as a proxy calling DLL (c + +), and then C # calls Windows Runtime Component (c + +) Lblmsg.text + = "Invokes a function in Windows Runtime Component, which acts only as a proxy, and actually calls a function in Windowsdll:" + Component. ADD2 (10, 10).
ToString (); Base.
Onnavigatedto (e); }
}
}
Second, demonstrates how to call Windows Runtime Component (c + + & D3D) in C # to get the resolution of the screen
1. Windowsruntimecomponent Project
Helper.h
* Note: * For D3D, it is necessary to add "->" * reference in the project properties-> Configuration Properties-> linker-> Input D3d11.lib Additional dependencies: * Http://blogs.micro soft.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))
{///throws Error throw Platform::exception::createexception (HR) for DirectX API;
}} namespace Windowsruntimecomponent {Public ref class Helper sealed {Public:helper (); An attribute used to get the screen resolution property Windows::foundation::P oint screenresolution {Windows:
: Foundation::P oint get (); } private:d3d_feature_level M_featurelEvel;
Microsoft::wrl::comptr<id3d11device1> M_d3ddevice;
}; }
Helper.cpp
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/