我使用 C 中的 Rhapsody Developer 编写了一个简单的求和函数,并将其声明为__declspec(dllexport) int Class_Sum_sum(Class_Sum* const me, int a, int b);
在我的 C 文件中。我是 C# 编程的初学者。
我的 C# 程序如下所示:
using System.Runtime.InteropServices;
namespace Test1_C_Sharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int x = Win32.Class_Sum_sum(5, 8);
textBox1.Text = x.ToString();
}
}
public class Win32
{
[DllImport("CalcSum.dll", CharSet = CharSet.Auto)]
public static extern int Class_Sum_sum(int a, int b);
}
}
当我执行此代码时,我得到一个带有文本框和“求和”按钮的表单,正如预期的那样,当我按下“求和”按钮时,抛出异常说
检测到 PInvokeStackImbalance
这实际上是有道理的,因为我的 C 函数 (Class_sum *,int,int) 中有三个参数,而且我不知道我的 C# 代码中的第一个参数到底应该是什么样子。
有谁知道这样做的正确方法?
编辑:我在 IBM Rhapsody 中为我的类“Class_sum”建模,它转换为struct
C 中的 a。我的 H 文件中的片段如下所示:
/*## class Class_Sum */
typedef struct Class_Sum Class_Sum;
struct Class_Sum {
RiCReactive ric_reactive;
int op1; /*## attribute op1 */
int op2; /*## attribute op2 */
int sum; /*## attribute sum */
/*#[ ignore */
int rootState_subState;
int rootState_active;
int MainState_subState;
/*#]*/
......
......
};
Rhapsody 会生成自己的函数和结构me
,例如翻译成this
OOP 语言。