[DLLImport C# UWP] UWP use DLL compiled by VS2015 C++
来源:互联网 发布:php mysql支持 编辑:程序博客网 时间:2024/06/07 22:22
Firstly it is a sample thing to complete it if you just want to have a try!
Secondly ,if we use it in a project for business or other complex environment ,it may seem not easy .
1.
In the beginning ,let`s have a try, it is easy.
In (1) dll_dll_uwp_test project and (2) runtime_uwp_test project we create two DLLs ,in two of these,(1) is create by DLL( windows universal ) and (2) is created by windows Runtime (windows universal).
//.cpp#include "pch.h"#include "dll_uwp_test.h"int add(int a,int b) { return a + b;}//.h#pragma once//using namespace runtime_uwp_test;namespace runtime_uwp_test{ extern "C" __declspec(dllexport) int addp(int a, int b); public ref class Class1 sealed { public: Class1(); };}
(2)
//.cpp#include "pch.h"#include "Class1.h"using namespace runtime_uwp_test;using namespace Platform;namespace runtime_uwp_test { int addp(int a, int b) { return a + b; }}//.h#pragma oncenamespace runtime_uwp_test{ extern "C" __declspec(dllexport) int addp(int a, int b);}
In use_dll_OR_runtime project ,we use these two DLLs.
namespace use_dll_OR_runtime{ public sealed partial class MainPage : Page { [DllImport("dll_uwp_test.dll")] static extern int add(int a,int b); [DllImport("runtime_uwp_test.dll")] static extern int addp(int a, int b); public MainPage() { this.InitializeComponent(); } private void dllcommon_click(object sender, RoutedEventArgs e) { int a = 1; int b = 2; int c = add(a,b); textBlock.Text = c.ToString(); } private void Dllruntime_click(object sender, RoutedEventArgs e) { int a = 1; int b = 2; int c = addp(a, b); textBlock.Text = c.ToString(); } }}
Just two buttons ,via button click event to use add addp function.
It is the first try.
2.
About extern “C”
we must use it! when we export a function ,we use it to get the function name right.
Use depends walker to see what happens.
when we use it ,in the denpends walker we can get
addp
if not we will get
?addp@runtime_uwp_test@@YAHHH@Z
as function name! that is why we use extern “C”
3.
About the bug report
(1) System.DllNotFoundException
Unable to load DLL ‘runtime_uwp_tes.dll’: Exception from HRESULT: 0x8007007E
when you get this report , first thing you can do is to check where is your app and where is your DLL ,whether the app can find your DLL
(In the same folder &
In system32 &
system runtime environment )
or just you have a wrong DLL name.
Use the code:
`
if (System.IO.File.Exists("XXXX.dll") != true)
{
OutputTextBlock.Text = "Cannot find DLL";
}
if you can find the DLL, it still say cannot “find”, use Depends walker to see what other DLLs it need, add them to your project or select the create DLL project property
C/C++ -> generation->runtime lib->MT&MTd (Multi thread (debug))
(2)
System.EntryPointNotFoundException
Unable to find an entry point named ‘XXXXXXX’ in DLL XXXXXX.dll’.
when you get this report you need to check whether the DLL has been correctly exported or you get the right function name.
Use depends walker. Reference the 2. Extern “C”
4.
some DLL are usually depended!
such as msvcp(140)(d).dll such as 80 120 ect.
It means that the environment need the VC runtime lib like “The Microsoft Visual C++ 2015 Redistributable Package “—-with it , you will not worry the 140dll .
BUT what if a device without the VC runtime ? What I can say is : re-code your DLL to make it independent to runtime or copy the DLLs what you need to your project.
- [DLLImport C# UWP] UWP use DLL compiled by VS2015 C++
- UWP
- UWP-C#-关键字
- win10 UWP Controls by function
- UWP之C++/CX开发
- UWP简介:什么是UWP?
- C#中使用DllImport调用C++dll
- 解决VS2015加载不了UWP项目的方法
- c#创建(UWP)桌面图形应用程序
- CSDN-UWP
- UWP C++/CX开荒记 序
- UWP C++/CX开荒记 第一回 C++/CX简介
- [UWP]在UWP中使用EntityFrameworkCore + SQLite
- 【UWP开发】uwp应用安装失败
- 【一分钟了解UWP】微信UWP
- 【一分钟了解UWP】微信UWP
- UWP C++_CX开荒记 第二回 设计框架
- 通过VS2015查看GitHub上的微软官方例子学习UWP
- 安卓线程池和任务:
- 动态规划练习——滑雪
- box-shadow的内外阴影
- 重定向和请求转发的区别
- oracle安全管理
- [DLLImport C# UWP] UWP use DLL compiled by VS2015 C++
- 【IMWeb训练营作业】组件间通讯
- 798B
- [JZOJ5083].【GDSOI2017第三轮模拟】Gift
- python中的list和array的区别
- servlet生命周期
- java 基础用法
- echarts散点图使用(转:http://www.suchso.com/UIweb/echarts-sandiantu.html)
- Yii 2.0 笔记之 rules 验证规则