| 发表于:2007-01-26 21:30:44 楼主 |
我是从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 都有值> ?????????????????? |
|
|
|
|