郁闷死,没错呀.....关于调用C++写的.DLL
我是从VB中改过来的,原VB写法:
////////bas
Attribute VB_Name = "Module1 "
Option Explicit
Declare 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 Long
Public Const WH_CBT = 5
////////frm
Option Explicit
Private 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 Sub
Private 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
都有值> ??????????????????
[解决办法]
为什么我写的没用????????但hHookDLL, hSystemHook, pHookFunction
都有值> ??????????????????
------------------------------
what 's your matter?
[解决办法]
给了 0 初始值,看看结果如何。
[解决办法]
我刚测试了一下,得到的hHookDLL的值为0..
那你VB执行后是什么效果呢?
[解决办法]
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