我是从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
都有值>??????????????????
////////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
都有值>??????????????????
解决方案 »
- 通过kernel32.dll调用动态库调时第一个接口函数能成功,第二个时“无法加载 DLL“kernel32.dll”
- 生活中常用的桌面软件有多少是C#开发的?
- 用C#向ACCESS插入数据的问题?
- 怎样把combox绑定到datagridview上哦
- 在一个控件的事件里面如何动态生成别的控件,并且使用它
- 100分:一个终止进程的问题(高手请进)
- asp调用c# windows COM ?
- 索引器在开发过程中,用在什么地方??
- DateTimePicker的问题
- 用c#编写的WINDOWS应用程序如何编译成本地代码?
- IDisposable对象的问题
- 使用DataSet和DataGridView时手动插入数据的问题!
都有值>??????????????????
------------------------------
what's your matter?
hHookDLL, hSystemHook, pHookFunction
都有值
程序执行了,且无异常,但是没有效果~!!!
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