【发布时间】:2015-04-21 23:10:42
【问题描述】:
主题看起来像重复,但不是:
主要项目是:
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 Runtime Component 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++ DLL 与 C# 项目挂钩的正确方法是什么? 如何让 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
【问题讨论】:
-
错误信息是什么?
标签: c# silverlight windows-phone-8 dll windows-phone-8.1