BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / dot-net / #3151同步于 2011/7/20
该镜像源已超过 30 天没有更新,可能在源站已被删除。
dotNET机器人发帖

C#调用供mfc使用的dll中的函数问题

Zdp
2011/7/20镜像同步4 回复
各位大牛们,小弟最近在做一个测试程序,遇到了点问题,就是想把一个供mfc使用的dll文件里面的函数导入到c#里面,并调用这个函数,这个函数原型声明是这样的: __declspec(dllimport) void __stdcall NetusbSetCallback(NETUSB_PFUNC pFunc);其中NETUSB_FUNC的定义如下:typedef void (DLL_CALLCONV * NETUSB_PFUNC) (unsigned char* buff, size_t n); 下面的是在mfc程序中调用这个函数: NetusbSetCallback(TheCallbackFunc); 其中实参TheCallbackFunc是这样的: #define RCV_MSG (WM_USER + 0x0800) extern HWND hwnd=NULL; static void __stdcall TheCallbackFunc(unsigned char* buf, size_t n) { PostMessage(hwnd,RCV_MSG,(UINT_PTR)buf,(LONG_PTR)(n) ); } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 我的问题就是我如何将这个函数导入到C#中,参数表该怎么写,我用C#程序调用这个函数的时候,具体的实参该如何去写?求大牛指导,小弟在此先谢过了
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
ahomer机器人#1 · 2011/7/21
搜下 P/Invoke MFC
Zdp机器人#2 · 2011/7/21
【 在 ahomer 的大作中提到: 】 : 搜下 P/Invoke MFC : -- 谢谢
Zdp机器人#3 · 2011/7/30
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; namespace WindowsFormsApplication1 { public delegate void TheCallbackFunc(byte[] buf, byte n); public partial class Form1 : Form { // public delegate void TheCallbackFunc(byte[] buf, byte n); public static IntPtr hwnd; public class cCdll { //由于要使用postmessage函数,所以导入user32.dll中的postmessage函数 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool PostMessage(IntPtr hwnd,int msg, byte[] wParam, uint lParam); [DllImport("Netusb.dll", EntryPoint = "_NetusbGetNumDevices@8", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern int NetusbGetNumDevices(int vid, int pid); [DllImport("Netusb.dll", EntryPoint = "_NetusbOpen@12", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern char NetusbOpen(int deviceIndex, int vid, int pid); [DllImport("Netusb.dll", EntryPoint = "_NetusbSetTxaddr@16", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern byte NetusbSetTxaddr(byte addr0, byte addr1, byte addr2, byte addr3); [DllImport("Netusb.dll", EntryPoint = "_NetusbSetRxaddr@16", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern byte NetusbSetRxaddr(byte addr0, byte addr1, byte addr2, byte addr3); [DllImport("Netusb.dll", EntryPoint = "_NetusbSetFrequence@4", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern byte NetusbSetFrequence(byte fre); [DllImport("Netusb.dll", EntryPoint = "_NetusbSendData@8", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern byte NetusbSendData(byte[] buffer0, int bufferSize); [DllImport("Netusb.dll", EntryPoint = "_NetusbSetCallback@4", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern void NetusbSetCallback(TheCallbackFunc pfunc); [DllImport("Netusb.dll", EntryPoint = "_NetusbStartListen@0", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern int NetusbStartListen(); [DllImport("Netusb.dll", EntryPoint = "_NetusbGetData@8", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern byte NetusbGetData(byte[] buffer,byte[] bytesReturned); [DllImport("Netusb.dll", EntryPoint = "_NetusbStopListen@0", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern int NetusbStopListen(); [DllImport("Netusb.dll", EntryPoint = "_NetusbClose@0", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern void NetusbClose( ); [DllImport("Netusb.dll", EntryPoint = "_NetusbRegisterNotification@4", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern void NetusbRegisterNotification(IntPtr handle); [DllImport("Netusb.dll", EntryPoint = "_NetusbUnregisterNotification@0", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern void NetusbUnregisterNotification(); [DllImport("Netusb.dll", EntryPoint = "_NetusbBroadCast@4", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern IntPtr NetusbBroadCast(); [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow(); //委托函数定义 public static void myCallbackFunc(byte[] buf, byte n) { s6= cCdll.PostMessage(hwnd, 0x0400 + 0x0800, buf, n); } } public Form1() { InitializeComponent(); } static byte[]buffer0=new byte[32]{01, 01, 01, 00,00,0x63,0x61,00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //前四位数据格式是固定的,从第六位开始就是正式的净荷,一直到0为止,中间的全都是数据 static int buffersize=32; byte[] buffer=new byte[32]; static byte[] bytesreturned=new byte[32]; static byte [] f=new byte[32]; static byte n; int s1 = -99; int s2 = -99; int s3 = -99; int s4 = -99; int s5 = -99; static bool s6=true; int s7 = -99; private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //开启设备 System.Threading.Thread.Sleep(10); // s3 = cCdll.NetusbSetTxaddr(0XCC, 0XCC, 0XCC, 0XCC);//设置发送地址,函数工作正常,返回值为0 // s4 = cCdll.NetusbSetRxaddr(0XCC, 0XCC, 0XCC, 0XCC);//设置接收地址,函数工作正常,返回值为0 // s5 = cCdll.NetusbSetFrequence(0x6A);//函数工作正常,设置频率为433mhz,返回值为0 textBox6.Text = s1.ToString(); } private void button2_Click(object sender, EventArgs e) { //接收数据 /* cCdll.NetusbGetData(buffer, bytesreturned); //以上这几个函数没有调用?????????????????? textBox6.Text = buffer[5].ToString(); int res = cCdll.NetusbStopListen(); cCdll.NetusbClose();*/ //以上这几个函数没有调用?????????????????? textBox6.Text = buffer[5].ToString(); } private void button3_Click(object sender, EventArgs e) { cCdll.NetusbRegisterNotification(cCdll.GetForegroundWindow()); hwnd = cCdll.GetForegroundWindow(); cCdll.NetusbGetNumDevices(0x10C4, 0x8568);//寻找设备,返回值为1,说明有设备,为0,无设备,该函数工作正常 s2 = cCdll.NetusbOpen(0, 0x10C4, 0x8568);//开启找到的设备,该函数工作正常,返回值为0 TheCallbackFunc tcb = new TheCallbackFunc(cCdll.myCallbackFunc); cCdll.NetusbSetCallback(tcb); Thread.Sleep(50); s7 = cCdll.NetusbStartListen(); Thread.Sleep(5); cCdll.NetusbGetData(buffer, bytesreturned); textBox1.Text = buffer[0].ToString(); textBox6.Text = buffer[5].ToString(); cCdll.NetusbStopListen(); cCdll.NetusbClose(); Thread.Sleep(10); cCdll.NetusbUnregisterNotification(); } private void button4_Click(object sender, EventArgs e) { int res = cCdll.NetusbStopListen(); cCdll.NetusbClose(); } private void button5_Click(object sender, EventArgs e) { textBox5.Text=s5.ToString(); } private void button6_Click(object sender, EventArgs e) { textBox6.Text = (cCdll.NetusbStartListen()).ToString(); } private void button7_Click(object sender, EventArgs e) { s3 = cCdll.NetusbSetTxaddr(0XCC, 0XCC, 0XCC, 0XCC);//设置发送地址,函数工作正常,返回值为0 s4 = cCdll.NetusbSetRxaddr(0XCC, 0XCC, 0XCC, 0XCC);//设置接收地址,函数工作正常,返回值为0 s5 = cCdll.NetusbSetFrequence(0x6A);//函数工作正常,设置频率为433mhz,返回值为0 // cCdll.NetusbSetCallback(f, n); //MessageBox.Show(Convert.ToString(s7)); } private void netusbclose_Click(object sender, EventArgs e) { cCdll.NetusbStopListen(); } private void netusbclose_Click_1(object sender, EventArgs e) { cCdll.NetusbClose(); } private void button8_Click(object sender, EventArgs e) { textBox8.Text = (cCdll.NetusbSendData(buffer0, buffersize)).ToString(); } private void button9_Click(object sender, EventArgs e) { /* cCdll.NetusbStopListen(); cCdll.NetusbUnregisterNotification(); cCdll.NetusbClose(); */ //s6 = cCdll.PostMessage((IntPtr)0, 0x0400 + 0x0800, buffer, n); // textBox1.Text = buffer[0].ToString(); // textBox2.Text=buffer[1].ToString(); // textBox3.Text=buffer[2].ToString(); // textBox4.Text=buffer[3].ToString(); // textBox5.Text=buffer[4].ToString(); /* string ss = null; for (int i = 0; i < buffer.Length; i++) { ss += buffer[i].ToString(); } */ // textBox7.Text = buffer[6].ToString(); // textBox8.Text=buffer[7].ToString(); } } }
Anti2005机器人#4 · 2011/8/3
整得乱乱的,都不知道你想要问什么了.