How to Call [Unmanaged C++ DLL func] from [C# WP8.

2019-05-08 05:51发布

This is not a duplicate of "how to call C++ function from C#"

I know how to do it for regular PC app and I tried and it works:

This is called P/Invoke

1.
Create a C++ dll project,
but you can't add the C++ dll project as a reference to the C# project.
Copy the dll from C++, and paste the dll to the C# exe folder

2.
in C#  use [DllImport...] 
in C++ use extern "C" __declspec(dllexport) ...

But when I do this on WindowsPhone, failed.

There are 2 type of unmanaged c++ native dll project for windows phone, I tried both

DLL (Windows Phone) <- WRT DLL 8.1
DLL (Windows Phone Silverlight 8.1)

I don't know where to place the dll file. Because the finally exe is a package. I can't place the dll in the same folder with the package. The dll should be in the package, as a part of the package.

So I tried to add the dll as a resorce file in the C# WP8.1 SL project.

But the function call failed.

So what I want to know is, how to place the dll, how to hook up the dll with the WP8.1 SL project?

Is there any one try that before?

Thank you guys for the answer.

//------------------------------------------------------------

So the correct way is For a [C# WP8.1 SL] project, I need to

create a [C++ Runtime Component DLL] <- as bridge
create a [C++ Native WP SL DLL] <- real dll

Add the [C++ Runtime Component DLL] as a reference to [C# WP8.1 SL]
And the [C++ Native WP SL DLL] as a reference to [C++ Runtime Component DLL]

1条回答
▲ chillily
2楼-- · 2019-05-08 06:30

Unmanaged assembly cannot be referenced in managed windows phone project. Create windows run time component inside your solution which acts as a wrapper for the dll. You can link other unmanaged dll to the run time component.

Please read the following links for more details:

Creating Windows Runtime Components

How to import C++ dll in Windows Phone Project

Consuming a C++ library project from a Windows Phone Store app

Windows Runtime Component Sample-WP8.0

查看更多
登录 后发表回答