如何从 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] 之外,其他都失败了
将项目A、项目B、项目C添加到[C#项目-> 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
好的,我找到答案了。
无法添加 'un_managed C++ DLL project' 作为对 'managed C# project' 的引用。
正确的挂接方式是手动将C++工程中的*.dll添加到C#工程中,然后P/Invoke.
但是还是调用失败,不知道为什么。 :-P
// 我找到原因了:
如需了解更多详情,
请看看我创建的这个线程:
主题看起来像重复,但不是:
主要项目是:
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] 之外,其他都失败了
将项目A、项目B、项目C添加到[C#项目-> 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
好的,我找到答案了。
无法添加 'un_managed C++ DLL project' 作为对 'managed C# project' 的引用。
正确的挂接方式是手动将C++工程中的*.dll添加到C#工程中,然后P/Invoke.
但是还是调用失败,不知道为什么。 :-P
// 我找到原因了:
如需了解更多详情, 请看看我创建的这个线程: