您的位置:程序门 -> .net技术 -> c#



郁闷死,没错呀.....关于调用c++写的.dll


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


郁闷死,没错呀.....关于调用c++写的.dll[已结贴,结贴人:cs920]
发表于: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
都有值> ??????????????????
发表于:2007-01-26 22:03:111楼 得分:5
为什么我写的没用????????但hhookdll,   hsystemhook,   phookfunction
都有值> ??????????????????
------------------------------
what 's   your   matter?
发表于:2007-01-27 00:30:072楼 得分:5
给了   0   初始值,看看结果如何。
   

发表于:2007-01-27 10:16:213楼 得分:0
你用vb测试可以成功吗?
发表于:2007-01-27 10:30:584楼 得分:0
是不是这里的原因?? "alias   "loadlibrarya "...

对vb不了解...
发表于:2007-01-27 11:15:345楼 得分:0
vb测试成功~
发表于:2007-01-27 11:16:446楼 得分:0
回复:shrinerain(圣影雨)
hhookdll,   hsystemhook,   phookfunction
都有值
程序执行了,且无异常,但是没有效果~!!!
发表于:2007-01-27 12:44:247楼 得分:70
我刚测试了一下,得到的hhookdll的值为0..

那你vb执行后是什么效果呢?
发表于:2007-01-28 19:16:128楼 得分:15
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




快速检索

最新资讯
热门点击