我是从VB中改过来的,原VB写法:
////////bas
Attribute VB_Name = "Module1"
Option ExplicitDeclare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongPublic Const WH_CBT = 5
////////frm
Option ExplicitPrivate Sub Form_Load()  Dim hHookDLL As Long
  Dim pHookFunction As Long
  Dim hSystemHook As Long
  
  App.TaskVisible = False
  
  hHookDLL = LoadLibrary("StickyApp32.DLL")
  
  If hHookDLL = 0 Then
    MsgBox "Could not locate StickyApp32.DLL", vbOKOnly Or vbCritical, "StickyApp32"
    End
  End If
  
  pHookFunction = GetProcAddress(hHookDLL, "HookFunction")
  hSystemHook = SetWindowsHookEx(WH_CBT, pHookFunction, hHookDLL, 0)
  
End SubPrivate Sub Form_Unload(Cancel As Integer)
  Cancel = True
End Sub
//////////////////////////////////CSHARP
=====================我写的:
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;//这是用到DllImport时候要引入的包namespace WindowsApplication4
{
  
    public partial class Form1 : Form
    {
        const int WH_CBT = 5;
[DllImport("kernel32", EntryPoint = "GetProcAddress", SetLastError = true)]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
        [DllImport("kernel32", EntryPoint = "LoadLibrary", SetLastError = true)]
        public static extern IntPtr LoadLibrary(string lpLibName);
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowsHookEx(int type, IntPtr hook, IntPtr instance, int threadID);
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            
            IntPtr hHookDLL, hSystemHook, pHookFunction;            
            hHookDLL = LoadLibrary("StickyApp32.DLL");
            if (hHookDLL.ToInt32() == 0)
            {
            MessageBox.Show("加载资源失败");
            Application.ExitThread();
            }
              pHookFunction = GetProcAddress(hHookDLL, "HookFunction");
              hSystemHook = SetWindowsHookEx(WH_CBT, pHookFunction, hHookDLL, 0);
            
        }       
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    } 
///////////////////////////////////////
为什么我写的没用????????但hHookDLL, hSystemHook, pHookFunction
都有值>??????????????????

解决方案 »

  1.   

    为什么我写的没用????????但hHookDLL, hSystemHook, pHookFunction
    都有值>??????????????????
    ------------------------------
    what's your matter?
      

  2.   

    是不是这里的原因??"Alias "LoadLibraryA"...对VB不了解...
      

  3.   

    回复:shrinerain(圣影雨)
    hHookDLL, hSystemHook, pHookFunction
    都有值
    程序执行了,且无异常,但是没有效果~!!!
      

  4.   

    我刚测试了一下,得到的hHookDLL的值为0..那你VB执行后是什么效果呢?
      

  5.   

    ok, I got it.NOTICE:SetWindowsHookEx(WH_CBT, pHookFunction, hHookDLL, 0);
    1.the 2nd parameter "pHookFunction" must be a delegate, not a function pointer.
    2.the 3rd parameter "hHookDLL" must be a self handle. because .Net can't hook global message except "WH_KEYBOARD_LL" and "WH_MOUSE_LL". So if you want to hook WH_CBT, the 3rd parameter must be a self handle. use "Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);" If you want set a global hook, please use C++ or VB.PS: only "WH_KEYBOARD_LL" and "WH_MOUSE_LL" can be global hook in .Net