简体   繁体   中英

How to call a C++ DLL function from C# project on Windows Phone

The subject looks like a duplicate, but it's not:

The main project is:

C# Project -> WP8.1 Silverlight
or
C# Project -> WP8.1 RT

There are 3 options for the C++ DLL porject:

C++ ProjectA ->Windows Runtime Component (Windows Phone Silverlight 8.1) <- this is DLL
C++ ProjectB ->DLL (Windows Phone Silverlight 8.1)
C++ PorjectC ->DLL (Windows Phone)  <- (WinRT 8.1)

OK, I don't have any syntax question, there are tons of tutorial online, like

// In C#
[DllImport("WRCWP81SLd.dll", EntryPoint = "DllWP81SLFunc")]

// In C++
extern "C" __declspec(dllexport) int DllWP81SLFunc();

The problem is, how do I add the dll to the C# Project, How do I hook up the dll project to the C# project to let the C# detect the C++ function in DLL?

I tried to Add reference of the project like what ppl said in some of the tutorial, (it works on C++ to C++)

But When add ProjectA, ProjectB, ProjectC to [C# Project -> WP8.1 Silverlight]

All failed except Adding [WinRuntimeComponent Silverlight8.1] to [WP8.1 Silverlight]

When add ProjectA, ProjectB, ProjectC to [C# Project -> WP8.1 RT]

All failed

A reference to 'DLLWP81RT' could not be added.

And about the only suceed one - [C#WP8.1 Silverlight] - [C++ Windows Runtime Component Silverlight 8.1], it crashed during the runtime.

// declare
[DllImport("WRCWP81SLd.dll", EntryPoint = "DllWP81SLFunc", CallingConvention = CallingConvention.StdCall)]
public static extern int DllWP81SLFunc();

// crash at this line, couldn't detect the function
int w = DllWP81SLFunc(); <- crash

So my question is: What's the correct way to hook up the C++ DLL with C# project? How to let C# detect the C++ function on WindowsPhone 8.1 RT/Silverlight project?

//-------------------------------------------------------------------------------------
//-----------The beautiful devider
//-------------------------------------------------------------------------------------

Answer to David Heffernan:

On [C# WP8.1 Silverlight] project

When trying to add [C++ DLL WP8.1 RT]

Unable to add the reference to DLLWP81RT.A reference to 'DLLWP81RT' could not be added.
The method or operation is not implemented.

When trying to add [C++ DLL WP8.1 Silverlight]

Unable to add the reference to DLLWP81SL.A reference to 'DLLWP81SL' could not be added.
The method or operation is not implemented.

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

On [C# WP8.1 WRT] project

When trying to add [C++ DLL WP8.1 RT] as a reference

A reference to 'DLLWP81RT' could not be added

When trying to add [C++ DLL WP8.1 Silverlight] as a reference

A reference to 'DLLWP81SL' could not be added

When trying to add [C++ WindowsRuntimeComponent WP8.1] as a reference

A reference to 'WRCWP81SL' could not be added

So, so far,

I can only add [C++ WindowsRuntimeComponent WP8.1] to [C# WP8.1 Silverlight] project But failed at calling C style functions -

An exception of type 'System.NotSupportedException' occurred in 'YYYY.DLL' but was not handled in user code
Additional information: DllImport cannot be used on user-defined methods

OK, I found the answer.

There's no way to add 'un_managed C++ DLL project' as reference to a 'managed C# project'.

The correct way to hook up is to manually add the *.dll from the C++ project to the C# project and do a P/Invoke.

But still failed at the calling, I don't know why. :-P

// I find the reason why:

For more detail, Please take a look at this thread that I created:

How to Call [Unmanaged C++ DLL func] from [C# WP8.1 WRT/SL] project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM