Home » C++ ProgrammingRSS

DLL

i am creating global hooking for mouse and keyboard in vc++ and vb6.And in vc++ dll i want to open the text file when the dll is loaded first time in the memory and want to close that text file when the dll is unloaded.how to code dll for this purpose?plz reply any idea about that fast.
 

7 Answers Found

 

Answer 1

global  hook DLL has to be hosted by an EXE that calls (Set|Unhook)WindowsHookEx, so do it in the EXE.

 

 

Answer 2

i am creating  global hooking  for mouse  and keyboard  in vc++ and vb6.And in vc++ dll  i want to open  the text  file when the dll is loaded  first time  in the memory  and want to close  that text file  when the dll is unloaded.how to code  dll for this purpose?plz  reply  any idea  about that fast.

I do not quite understand why you need to use hooks.

Each DLL has an entry point or initialization function; for example Win32 DLL has DllMain and one of parameters is a reason to call. On loading (DLL_PROCESS_ATTACH) open file and close it when DLL is unloaded  (DLL_PROCESS_DETACH).

You can also implement two exported functions that can be called from any module to maintain file stream.

 

Answer 3

Doug, I respectfully disagree.

All (but two) windows hooks that are to be global must have hook procedure in a DLL; hence it easier to also set hook in a DLL. However it is also possible to set hook in an executable module enquiring module handle of a dll  that contains hook procedure. It is more complicated though since dll would have to export it.

 

Only two hooks can be initiated by passing hook procedure located in an executable module: WH_MOUSE_LL and WH_KEYBOARD_LL.

 

Answer 4

I was not saying that the hook procedure should exist in the EXE. (I was not aware that was even possible.)

I was saying that that DLL that hosts the hook must be loaded  by an EXE to become established, and since that EXE is the first thing to load the hook DLL, it would seem to meet the OP's requirements.

 

Answer 5

Sorry I misunderstood your post.

As I have mentioned, two hooks are global  and they can be set in an exe module.

Also any hook that is not global can be set in an exe module. The difference is that instead passing module handle hook is set by passing thread ID.

I am still not sure why OP needs a hook.

 

Answer 6

LRESULT CALLBACK _stdcall MouseJi(int code,WPARAM w,LPARAM l)
{
                        if(code==HC_ACTION)
                        {
                                            MSLLHOOKSTRUCT *m=(MSLLHOOKSTRUCT *)l;
                                            ofstream fout("D:\\a.txt",ios::app);
                                            fout<<m->pt.x<<"\t"<<m->pt.y<<"\t"<<w<<"\n";
                                            fout.close();
                        }
                        return CallNextHookEx(ms,code,w,l);
                   
}

But setwindowshookex() will be in vb6  code.

so my problem is each time mouse  event occurs and file  will be opened/closed and all.

and if  i open  the file in dll_process_attach,it will be opened/closed each time  the new process is attached/deattached to the dll.

but i want to do it only once when dll  loads/unloads.how?

 

Answer 7

xxxyyyzzz123 wrote:

*LRESULT CALLBACK _stdcall MouseJi(int code,WPARAM w,LPARAM l)
{
if(code==HC_ACTION)
{
MSLLHOOKSTRUCT *m=(MSLLHOOKSTRUCT )l;
ofstream fout("D:\\a.txt",ios::app);
fout<<m->pt.x<<"\t"<<m->pt.y<<"\t"<<w<<"\n";
fout.close();
}
return CallNextHookEx(ms,code,w,l);
}


and if i open  the file  in dll_process_attach,it will be opened/closed  each time  the new process is attached/deattached to the
dll.


*but i want to do it only once when dll  loads/unloads.how?
*

"Process attached to a DLL" is just another way of saying "the DLL was  loaded  into a process". File streams cannot be shared across processes,  so each instance of the DLL in every process would need to open its own.


Igor Tandetnik

 
 
Search for  DLL
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter