您的位置:首页 > 编程语言 > C语言/C++

c#调用C++ 写的DLL(带CALLback函数)

2011-06-10 16:50 387 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;

namespace DLLTest
{
public partial class Form2 : Form
{
public static CallbackFunc1 CF;//静态全局变量
public Form2()
{
InitializeComponent();
}

//VMInterRP.dll
[DllImport("VMInterRP", EntryPoint = "ScannerInit", CharSet = CharSet.Unicode)]
public static extern int ScannerInit([MarshalAs(UnmanagedType.FunctionPtr)] CallbackFunc1 pCallbackFunc1);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CallbackFunc1(int i, [MarshalAs(UnmanagedType.LPArray, SizeConst = 32)] byte[] p);

private void Form2_Load(object sender, EventArgs e)
{

//CallbackFunc1 CF = RD;
CF = RD;//CF = new CallbackFunc1(RD);//fum 要声明为全局的静态变量,避免被c#的垃圾回收释放

int i = ScannerInit(CF);

}

public static int RD(int i, [MarshalAs(UnmanagedType.LPArray, SizeConst = 32)] byte[] b)
{

string t = "";// b.ToString();

foreach (byte d in b)
{
t += d.ToString();

}

MessageBox.Show(i.ToString() + "_" + t.ToString().Substring(0,t.Length-1));

return 0;
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: