简体   繁体   中英

How to get HWND in ATL DLL (for SendMessage or PostMessage)

I want to get HWND in ATL DLL for SendMessage or PostMessage function in Thread. but, ATL DLL doesn't have a window.

How to get HWND in ATL DLL?

Project Application Setting : DLL(Dynamic-link library), Security Development Lifecycle, ('not' Support MFC)
Class Option : Apartment, Aggregation Yes, Dual InterFace, Connection points.

HelloCtrl.cpp (VB Client is Handling ShowMessage())

STDMETHODIMP CHelloCtrl::ShowMessage(BSTR bstrCaption, VARIANT_BOOL* lpvbResult)
{
    DWORD dwThreadID;
    m_hThread_ReadData = CreateThread(NULL, 0, T_ReadData, (LPVOID)this, 0, &dwThreadID);
    return S_OK;
}

DWORD WINAPI CHelloCtrl::T_ReadData(LPVOID pParam)
{
    CHelloCtrl* hCtrl = (CHelloCtrl*) pParam;
    ::PostMessage(hCtrl->m_hWnd, WM_KEYDOWN, (WPARAM)NULL, (LPARAM)NULL);
    return S_OK;
}

void CHelloCtrl::LeftButton()
{
    Fire_OnMouseClick(123, 123);
}

HelloCtrl.h

 #define WM_THREADFIREEVENT (WM_USER+1)
BEGIN_MSG_MAP(CHelloCtrl)
    CHAIN_MSG_MAP(CComControl<CHelloCtrl>)
    DEFAULT_REFLECTION_HANDLER()
    MESSAGE_HANDLER(WM_THREADFIREEVENT, OnLeftButtonDown)
END_MSG_MAP()

public:
    STDMETHOD(ShowMessage)(BSTR bstrCaption, VARIANT_BOOL* lpvbResult);
    LRESULT OnLeftButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
    void LeftButton();
private:
    HANDLE m_hThread_ReadData;
    static DWORD WINAPI T_ReadData(LPVOID pParam);

Window is an object that a process or module might have or might not have, or it might create one if needed. That is, your question does not have an answer without specifying what kind of window and its HWND handle you are looking for. DLL and HWND are unrelated.

From the context, it looks like you want a window which you can use for messaging and to transfer execution control between threads. That is, you post somewhere then handle elsewhere leaving the threading magic to window API.

In this case you can either reuse one of existing windows, such as window created for an ActiveX control, our you simply create your own window you fully control and use for your purposes. For the latter you derive from CWindowImpl and... see Implementing a Window with CWindowImpl . The former might be simpler might be not: ActiveX controls don't have to have a window in which case they are windowless controls. In the same time, you have an option to force windowed control using m_bWindowOnly , see How do I get the HWND for an ActiveX control after the control has been initialised/activated? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM