C
Chris Tacke, eMVP
I'm not sure whether this is a C# or C++ question, so I'm asking in both
groups.
Here's my scenario:
I need to get all text added to a ListBox that is in another process that I
have no control over. I want to get this information in my C# application
through a callback.
Here's what I've done:
Since global hooks must be done in C, I've written a simple DLL that finds
the handle of the ListBox I want the info from, hooks it, and gets the
string data as it is added. This all works just fine.
The problem:
From C# I create a delegate and pass it into the DLL prior to hooking the
ListBox. The DLL then saves the function pointer to a global variable for
use in the hook procedure. I *can* successfully call the callback from the
routine that accepts the handler. When I try to use said handle from the
hook proc, it is NULL and things go bad.
So this is basically what I've got:
#pragma data_seg(".shared")
CallBack m_callback = NULL;
#pragma data_seg()
typedef void (__stdcall *CallBack)(LPCTSTR);
__declspec(dllexport)
BOOL __stdcall LoadHook(CallBack callback)
{
// If already hooked, don't do it again.
// save the hook to a global area
m_callback = callback;
// I *CAN* call the callback here - this works:
m_callback("test");
}
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
CWPSTRUCT *lpcwt;
lpcwt = (CWPSTRUCT *)lParam;
switch (lpcwt->message)
{
case LB_ADDSTRING:
// my global callback handle is ALWAYS NULL here
if(m_callback)
m_callback("test");
}
}
I've also tried using a memory-mapped file to store the callback function
pointer, but that also fails.
Is there something special about a hook proc that makes this so it can't
work?
Any pointers are GREATLY appreciated!
groups.
Here's my scenario:
I need to get all text added to a ListBox that is in another process that I
have no control over. I want to get this information in my C# application
through a callback.
Here's what I've done:
Since global hooks must be done in C, I've written a simple DLL that finds
the handle of the ListBox I want the info from, hooks it, and gets the
string data as it is added. This all works just fine.
The problem:
From C# I create a delegate and pass it into the DLL prior to hooking the
ListBox. The DLL then saves the function pointer to a global variable for
use in the hook procedure. I *can* successfully call the callback from the
routine that accepts the handler. When I try to use said handle from the
hook proc, it is NULL and things go bad.
So this is basically what I've got:
#pragma data_seg(".shared")
CallBack m_callback = NULL;
#pragma data_seg()
typedef void (__stdcall *CallBack)(LPCTSTR);
__declspec(dllexport)
BOOL __stdcall LoadHook(CallBack callback)
{
// If already hooked, don't do it again.
// save the hook to a global area
m_callback = callback;
// I *CAN* call the callback here - this works:
m_callback("test");
}
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
CWPSTRUCT *lpcwt;
lpcwt = (CWPSTRUCT *)lParam;
switch (lpcwt->message)
{
case LB_ADDSTRING:
// my global callback handle is ALWAYS NULL here
if(m_callback)
m_callback("test");
}
}
I've also tried using a memory-mapped file to store the callback function
pointer, but that also fails.
Is there something special about a hook proc that makes this so it can't
work?
Any pointers are GREATLY appreciated!