2013-04-18 55 views
1

我已經在C++中創建了一個DLL文件。我想在我的Windows Phone項目中導入它。我遵循了一些來自不同來源的指令,甚至當我跑我的代碼我收到以下錯誤:如何在Windows Phone項目中導入C++ dll

Attempt to access the method failed: rough.MainPage.Add(System.Int32, System.Int32).

我的windows phone的C#代碼是在這裏:

*//Here is C# code for Windows Phone 
namespace testRsa 
{ 
    using System.Runtime.InteropServices; 

    public partial class MainPage : PhoneApplicationPage 
    { 
     [DllImport("myfunc.dll", EntryPoint = "Add", CallingConvention =   CallingConvention.StdCall)] 
     static extern int Add(int a, int b); 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      int result = Add(27, 28); 
      System.Diagnostics.Debug.WriteLine(7); 
     } 
    } 
} 

我的DLL的.h文件是在這裏:

#include "stdafx.h" 
#include "myfunc.h" 
#include <stdexcept> 

using namespace std; 


double __stdcall Add(double a, double b) 
{ 
    return a + b; 

} 

我的DLL .cpp文件是在這裏:#包括 「stdafx.h中」 的#include 「myfunc.h」 的#include

using namespace std; 
double __stdcall Add(double a, double b) 
{ 
    return a + b; 

} 
+0

根據這個問題:http://stackoverflow.com/questions/4730774/import-c-dll-to-windows-phone-project?rq=1這是不可能的 – 2013-04-18 05:11:29

+0

所以,謝爾蓋庫徹爵士,是嗎?任何其他方式來實現我的目標?實際上,我有c + +的RSA代碼,我想在我的Windows Phone項目中使用該代碼的dll文件。 – creativemujahid 2013-04-18 05:23:20

+0

請閱讀我提供的鏈接中的評論,這裏有我知道的所有信息。 – 2013-04-18 05:27:09

回答

1

要導入化妝使用C++到C#項目,你必須讓它從託管代碼可見。爲此,您應該在New Project菜單的Visual C++部分下創建一個新的'Windows Phone Runetime'組件。例如,您可以將項目命名爲「Dll」。

創建項目後,您可以修改源代碼,使其看起來像這樣。

Dll.cpp:

#include "Dll.h" 

namespace ns { 

    double Cpp_class::cppAdd(double a, double b) 
    { 
     return a + b; 
    } 
} 

Dll.h:

#pragma once 

namespace ns { 
    public ref class Cpp_class sealed /* this is what makes your class visible to managed code */ 
    { 
     public: 
      static double cppAdd(double a, double b); 
    }; 
} 

編譯它來驗證你沒有做錯任何事。 完成後,創建一個新的Windows Phone應用程序項目(在新建項目菜單中的Visual C#下)右鍵單擊解決方案名稱並選擇'添加'>'添加現有項目',選擇您的Dll項目 這個,右鍵點擊Windows Phone應用程序項目,選擇'添加參考',在'解決方案'選項卡下,你會看到你的Dll項目

如果你做得這一切正確,你現在可以使用你的本地代碼通過「利用」它的Windows Phone應用程序的C#部分:

using Dll; 

[...] 
ns.Cpp_class.Add(1,3); 

請記住,你將無法使用該組件,如果你沒有添加引用

我真的希望有所幫助!