簡體   English   中英

如何從Windows Phone上的C#項目調用C ++ DLL函數

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

該主題看起來像重復的,但不是:

主要項目是:

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

C ++ DLL項目有3個選項:

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)

好的,我沒有任何語法問題,在線上有很多教程,例如

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

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

問題是,如何將dll添加到C#項目中,如何將dll項目連接到C#項目中,以使C#檢測DLL中的C ++函數?

我試圖添加該項目的引用,例如一些教程中的ppl所說的(它適用於C ++到C ++)

但是,當將ProjectA,ProjectB,ProjectC添加到[C#Project-> WP8.1 Silverlight]

除了將[WinRuntimeComponent Silverlight8.1]添加到[WP8.1 Silverlight]外,所有操作均失敗

將ProjectA,ProjectB,ProjectC添加到[C#Project-> WP8.1 RT]

全部失敗

A reference to 'DLLWP81RT' could not be added.

關於唯一被發現的一個[C#WP8.1 Silverlight]-[C ++ Windows運行時組件Silverlight 8.1],它在運行時崩潰了。

// 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

所以我的問題是:用C#項目連接C ++ DLL的正確方法是什么? 如何讓C#在WindowsPhone 8.1 RT / Silverlight項目上檢測C ++功能?

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

對大衛·赫弗南的回答:

在[C#WP8.1 Silverlight]項目上

嘗試添加[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.

嘗試添加[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.

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

在[C#WP8.1 WRT]項目中

嘗試添加[C ++ DLL WP8.1 RT]作為參考時

A reference to 'DLLWP81RT' could not be added

嘗試添加[C ++ DLL WP8.1 Silverlight]作為參考時

A reference to 'DLLWP81SL' could not be added

嘗試添加[C ++ WindowsRuntimeComponent WP8.1]作為參考時

A reference to 'WRCWP81SL' could not be added

所以到目前為止

我只能將[C ++ WindowsRuntimeComponent WP8.1]添加到[C#WP8.1 Silverlight]項目,但無法調用C樣式函數-

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

好的,我找到了答案。

無法將“ un_managed C ++ DLL項目”添加為對“托管C#項目”的引用。

連接的正確方法是將* .dll從C ++項目手動添加到C#項目並執行P / Invoke。

但是仍然無法通過電話,我不知道為什么。 :-P

//我找到原因:

有關更多詳細信息,請查看我創建的此線程:

如何從[C#WP8.1 WRT / SL]項目中調用[非托管C ++ DLL函數]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM