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;
}
}
}
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;
}
}
}
相关文章推荐
- C#调用C++Dll封装时遇到的一系列问题
- C#调用C++Dll封装时遇到的小问题
- C# 调用C++ DLL 的类型转换
- 浅谈C++调用C#的DLL程序方法
- 在c#中调用c++生成的dll(wince开发)
- C#调用C++编写的COM DLL
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#调用DLL的几种方法,包括C#调用C\C++\C#DLL
- C#时常需要调用C++DLL
- c# 调用 c++ dll 参数为结构体数组指针
- C++调用C#的DLL
- C# 中静态调用C++dll 和C# 中动态调用C++dll
- VS2012环境下C#调用C++生成的DLL
- C#中调用c++的dll具体创建与调用步骤,亲测有效~
- C#调用c++的dll,结构体数组作为引用参数的传递方式
- C#调用C++ Build 写的DLL时,有时会出错,错误为无法加载 DLL "XXX.dll": 找不到指定的模块。(异常来自 HRESULT:0xXXXXXX)。。
- C#时常需要调用C++DLL
- C# 调用C++ dll
- c#调用c++dll找不到入口点,一秒搞定
- c++建立DLL 及c# 调用