C# 使用c++;c语言中的函数# < >我在C++中定义了一个函数,我想把它包含在一些C语言GUI中。 这就是我所做的:

C# 使用c++;c语言中的函数# < >我在C++中定义了一个函数,我想把它包含在一些C语言GUI中。 这就是我所做的:,c#,c++,user-interface,C#,C++,User Interface,在cpp文件中: extern "C" //No name mangling __declspec(dllexport) //Tells the compiler to export the function int //Function return type __cdecl //Specifies calling convention, cdelc is default,

在cpp文件中:

extern "C"             //No name mangling
__declspec(dllexport)  //Tells the compiler to export the function
int                    //Function return type     
__cdecl                //Specifies calling convention, cdelc is default, 
                   //so this can be omitted 
test(int number){
    return number + 1;
}
在cs文件中:

...
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = test(9);
        }
    }

    public static class NativeTest
    {
        private const string DllFilePath = @"c:\test.dll";
        [DllImport(DllFilePath , CallingConvention = CallingConvention.Cdecl)]
        static extern int test(int a);

    }
}
我得到了这个错误:

错误1当前上下文行28中不存在名称“test”

谢谢你的帮助


谢谢

您需要调用
NativeTest.test()
而不仅仅是
test()
您需要调用
NativeTest.test()
而不仅仅是
test()

这个问题实际上与互操作无关。这个问题实际上与互操作无关。标记
测试
方法的访问修饰符提升(内部或公共或任何情况)@richiehindie请解释原因。谢谢!使用它并将功能折叠为“不安全”,其工作原理如下charm@Sorin:因为
test
函数是
NativeTest
类的成员,因此位于该类的命名空间中。正如@chris在他对这个问题的评论中指出的,这与interop无关。而mark
test
方法的访问修饰符被提升(内部或公共或任何时候)@richiehindie请解释原因。谢谢!使用它并将功能折叠为“不安全”,其工作原理如下charm@Sorin:因为
test
函数是
NativeTest
类的成员,因此位于该类的命名空间中。正如@chris在他对这个问题的评论中指出的,这与互操作无关。