[DLLImport C# UWP] UWP use DLL compiled by VS2015 C++

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 once
namespace 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.
  
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值