我有一個在C++中定義的函數,我想將它包含在一些c#gui中。 這是我做了什麼:在c中使用C++函數#
在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);
}
}
和我得到這個錯誤:
Error 1 The name 'test' does not exist in the current context Line 28
任何幫助讚賞
謝謝
這個問題的確與互操作無關。 – chris