如何导入Windows Phone的项目C ++ DLL [英] How to import C++ dll in Windows Phone Project

查看:127
本文介绍了如何导入Windows Phone的项目C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了C ++中的DLL文件。我想导入它在我的Windows Phone项目。我遵循了一些来自不同来源的指令,甚至当我跑我的代码我收到以下错误:




尝试访问方法失败:rough.MainPage.Add(System.Int32,System.Int32)




我的windows phone的C#代码是在这里:

  * //下面是使用系统的Windows Phone 
命名空间testRsa
{
C#代码.Runtime.InteropServices;

公共部分类的MainPage:的PhoneApplicationPage
{
函数[DllImport(myfunc.dll,入口点=添加,CallingConvention = CallingConvention.StdCall)]
静EXTERN INT添加(INT A,INT b);

//构造
公众的MainPage()
{
的InitializeComponent();
INT结果=添加(27,28);
System.Diagnostics.Debug.WriteLine(7);
}
}
}



我的DLL .h文件中是这里:

 的#includestdafx.h中
#包括myfunc.h
的#include < stdexcept>

使用命名空间std;


双__stdcall添加(双一,双二)
{
返回A + B;

}



我的DLL .cpp文件是在这里:
#包括stdafx.h中
#包括myfunc.h
的#include

 使用命名空间STD; 
双__stdcall添加(双一,双二)
{
返回A + B;

}


解决方案

要进口利用一个C ++到您的C​​#项目,你必须让它从托管代码可见。要做到这一点,你应该创建一个新的Windows Phone的Runetime组件,下的New Project菜单Visual C ++的部分。您可以命名,例如您的项目DLL。



在项目创建,您可以修改源有东西看起来像这样。



Dll.cpp:

 的#includeDll.h

命名空间NS {

双Cpp_class :: cppAdd(双一,双二)
{
返回A + b;
}
}



Dll.h:

 的#pragma一次

命名空间NS {
公共引用类Cpp_class密封/ *这是什么使你的类可见托管代码* /
{
公众:
静态双cppAdd(双一,双二);
};
}



编译它来验证你没有做错任何事。
一旦做到这一点,创建一个新的Windows Phone应用程序项目(下的Visual C#中的新项目菜单,右键单击该解决方案名称,然后选择添加>添加现有项目,选择您的DLL项目。
一旦你这样做,右键单击Windows Phone应用程序项目,选择添加引用中,解决方案选项卡下,您会看到DLL项目。



如果你做了这一切正确,你现在可以通过使用使用Windows Phone应用程序的C#部分内本机代码是:

 利用DLL; 

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

记住,你将无法使用的组件,如果你没有添加引用。



我真的希望帮助!


I have created a DLL file in C++. I want to import it in my Windows Phone project. I have followed a number of instructions from different sources, even when I run my code I am getting the following error:

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

My windows phone c# code is here:

*//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);
        }
    }
}

My dll .h file is here:

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

using namespace std;


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

}

My Dll .cpp file is here: #include "stdafx.h" #include "myfunc.h" #include

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

}

解决方案

To import make use of a C++ into your C# project you have to make it visible from managed code. To do so, you should create a new 'Windows Phone Runetime' component, under the Visual C++ section in the New Project menu. You can name your project "Dll" for example.

Once your project is created you can modify the source to have something that looks like this.

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);
    };
}

Compile it to verify you didn't do anything wrong. Once this is done, create a new Windows Phone app project (under the Visual C# in the New Project menu. Right click the solution name and select 'Add' > 'Add Existing Project', select your Dll project. Once you did this, right click the Windows Phone app project, select 'Add Reference', under the 'Solution' tab, you'll see you Dll project.

If you did all this correctly, you can now use your native code inside the C# portion of the Windows Phone App by "using" it :

using Dll;

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

Remember that you won't be able to use the component if you didn't add the reference.

I really hope that helps !

这篇关于如何导入Windows Phone的项目C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆