主题 : 那位帮忙看下这篇代码,为什么ADC值获取不到? 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 78060
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2012-09-15
最后登录: 2013-12-18
楼主  发表于: 2012-09-19 10:41

 那位帮忙看下这篇代码,为什么ADC值获取不到?

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;

using HANDLE = System.IntPtr;


namespace ADCTest
{
    public partial class Dialog : Form
    {
        private delegate void InvokeDelegate();
        private delegate void UpdateStatusDelegate(TextBox text_b, String message);
        private const int GWL_WNDPROC = -4;
        private const int WM_SETTINGCHANGE = 0x1A;
        //委托方法的定义

        const int WM_COPYDATA = 0x004A;
        const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
        const uint SYNCHRONIZE = 0x00100000;
        const uint EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3);
        const uint EVENT_MODIFY_STATE = 0x0002;
        const long ERROR_FILE_NOT_FOUND = 2L;

        delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

        [DllImport("coredll.dll", EntryPoint = "GetWindowLong")]
        private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("coredll.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr newWndProc);

        [DllImport("coredll.dll")]
        static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        private static IntPtr oldWndProc = IntPtr.Zero;
        private static WndProcDelegate newWndProc;

        [DllImport("Coredll.dll", SetLastError = true)]
        static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName);

        public Dialog()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "ADC Testing";

          
            newWndProc = new WndProcDelegate(WndProc);

            oldWndProc = GetWindowLong(this.Handle, GWL_WNDPROC);

            int success = SetWindowLong(this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(newWndProc));

            HANDLE ehandle = OpenEvent(EVENT_ALL_ACCESS, false, "ADC_EVENT_FROM_FRIENDLYARM");
            if (ehandle == null)
                MessageBox.Show("open error");
            SetEvent(ehandle);
            
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Handle.ToString());
        }

        public IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
           // switch (msg)
           // {
            if (msg == WM_COPYDATA)
            {
                MessageBox.Show("Catch the msg");
                COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                mystr = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
                //   MessageBox.Show(mystr.lpData.ToString());
                DoUpdate(textBox1, mystr.dwData.ToString());
            }
            else
                DoUpdate(textBox1, "NULL");
                //    break;
              //  default:
                    //MessageBox.Show("test");
                   // DoUpdate(textBox1,"test");
            //        break;
           // }
            return CallWindowProc(oldWndProc, this.Handle, msg, wParam, lParam);
        }

        private void DoUpdate(TextBox text_b, string message)                                      //委托修改textBox
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new UpdateStatusDelegate(DoUpdate), new Object[] { text_b, message });
                return;
            }
            text_b.Text = message;
        }

        [DllImport("coredll.dll", SetLastError = true)]        //start event
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EventModify(HANDLE hEvent, [In, MarshalAs(UnmanagedType.U4)] int dEvent);
        public enum EventFlags
        {
            PULSE = 1,
            RESET = 2,
            SET = 3
        }
        private static bool SetEvent(HANDLE hEvent)
        {
            return EventModify(hEvent, (int)EventFlags.SET);
        }
    }
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cbData;
        public string lpData;
    }
}
级别: 新手上路
UID: 78060
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2012-09-15
最后登录: 2013-12-18
1楼  发表于: 2012-09-20 21:08
还是自己回答吧。。。友善的ADC驱动发送给title为“ADC Testing”的dialog的WM_COPYDATA信息,C#中没有dialog这个系统类(自己命名的不行)。解决方法:(1)修改bsp然后自己编译,就是将findwindow中第一个参数改为NULL(2)将MFC的dialog封装成dll并提供接口共C#使用。
级别: 新手上路
UID: 118888
精华: 0
发帖: 7
金钱: 35 两
威望: 7 点
贡献值: 0 点
综合积分: 14 分
注册时间: 2015-09-11
最后登录: 2015-12-23
2楼  发表于: 2015-12-12 01:08
有封装的 dll么,借个用用吧,谢谢了