Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I wrote a DLL with hook of type WH_GETMESSAGE. What I want to do is store an std::vector<myobject> inside that DLL and being able to manipulate it from my app. But the problem is that when my app calls DLL function createMyObjects() vector's size is 10. But when my app receives a message and hook intercepts it (see the code), vector's size is 0.

The question is: when the hook catches a message to my app, is it the process of my app, or some kind of system process?

<pre lang="C++">
// MyApp "main.cpp"

DLL::createObjects(10); // creates 10 objects that are stored inside DLL

// DLL "hook.cpp"
namespace DLL {
    std::vector<MyObject> objects;
    
    EXPORT VOID CALLBACK createObjects(INT count) { 
       for(INT i=0; i < count; ++i)
          objects.push_back(MyObject());
    }
}


LRESULT CALLBACK hookProc(int code, WPARAM wParam, LPARAM lParam)
{
    if(code == HC_ACTION)
    {
        MSG* msg = (MSG*)lParam;

        if(msg->hWnd == myAppHWND && msg->message == WM_PAINT)
            assert(DLL::objects.size()); // why is it always has the size of 0?
    }

    return CallNextHookEx(0, code, wParam, lParam);
}


What I have tried:

I've spent about 2 weeks trying to manipulate DLL data from my app and tried to make shared data segment with events and it seemed to be a wrong way to do it
Posted
Updated 2-Dec-21 5:15am
v4
Comments
Richard MacCutchan 2-Dec-21 4:31am    
            assert(DLL::objects.size()); // shouldn't be this vector of size 10?

How can anyone here possibly answer that question? We have no idea what your DLL class does.
Avtem 2-Dec-21 4:37am    
Sorry, I try to remove all irrelevant code when I ask questions. I will add more code
Richard MacCutchan 2-Dec-21 5:15am    
The DLL function that is supposed to return the size is probably the most relevant part of your question.
Avtem 2-Dec-21 5:21am    
I do not have a function in DLL to retrieve the size of the vector. I use size() member function of the std::vector directly - objects.size() inside my hookProc()
Richard MacCutchan 2-Dec-21 5:23am    
Are you sure that the call to createObjects is actually succeeding? You should verify this with the debugger.

1 solution

Hook procedures are called in the context of the process that generates the message. Here is what the docs (Hooks Overview - Win32 apps | Microsoft Docs[^]) say on the topic :
Quote:
A global hook monitors messages for all threads in the same desktop as the calling thread. A thread-specific hook monitors messages for only an individual thread. A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module. A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL. For information, see Dynamic-Link Libraries.
 
Share this answer
 
Comments
Avtem 4-Dec-21 2:19am    
But who is the process that generates the message? Is it some kind of a system process or is it the process that receives the message (for example WM_PAINT)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900