如何从Windows Phone上的C#项目调用C ++ DLL函数

爱丽儿

该主题看起来像重复的,但不是:

主要项目是:

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函数]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章