[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.
  
     

你可能感兴趣的:(Code-UWP)