日期:2014-05-18  浏览次数:20855 次

根据VB代码 改写成C#.net代码后报错
VB code

Public Const GWL_WNDPROC = -4
Public Const GWL_USERDATA = (-21)

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Function Hook(ByVal hWnd As Long) As Long

    Dim pOld As Long
    '指定自定义的窗口过程
    pOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
    '保存原来默认的窗口过程指针
    SetWindowLong hWnd, GWL_USERDATA, pOld
    Hook = pOld
End Function



以上为VB原语句

改写成以下C#语句后

C# code

        public const short GWL_WNDPROC = -4;
        public const short GWL_USERDATA = (-21);

        [DllImport("user32", EntryPoint = "SetWindowLongA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int SetWindowLong(int hWnd, int nIndex, int dwNewLong);

        public static int Hook(int hWnd)
        {
            int returnValue;
            int pOld;
            pOld = System.Convert.ToInt32(SetWindowLong(hWnd, System.Convert.ToInt32(GWL_WNDPROC), System.Convert.ToInt32(new int(WindowProc))));
            SetWindowLong(hWnd, System.Convert.ToInt32(GWL_USERDATA), pOld);
            returnValue = pOld;
            return returnValue;
        }


下面这句话总是报错,不会改
C# code

pOld = System.Convert.ToInt32(SetWindowLong(hWnd, System.Convert.ToInt32(GWL_WNDPROC), System.Convert.ToInt32(new int(WindowProc))));



请高手指点

------解决方案--------------------
编译是过去了。。。。。。

C# code


public const int GWL_WNDPROC  = -4;
        public const int GWL_USERDATA = -21;

        public delegate IntPtr NewWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        private NewWndProc wpr = null;

        //备份的默认处理函数
        private IntPtr oldWndProc = IntPtr.Zero;

        [DllImport("user32", EntryPoint = "SetWindowLongA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, NewWndProc dwNewLong);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private IntPtr GridControlWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
        {
            IntPtr returnVar = IntPtr.Zero;   
            
            // 你的操作。。。

            returnVar = CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);
            return returnVar;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public IntPtr Hook(IntPtr hWnd)
        {                       
            IntPtr returnValue = IntPtr.Zero;
            wpr = new NewWndProc(this.GridControlWndProc);
            IntPtr pOld = SetWindowLong(hWnd, GWL_WNDPROC, wpr);
            SetWindowLong(hWnd, GWL_USERDATA, (NewWndProc)Marshal.GetDelegateForFunctionPointer(pOld,typeof(NewWndProc)));
            returnV