• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2753
  • Last Modified:

Create an invisble window in DLL C++

Hello,
I'd like to create an invisible window in DLL with dialog box so I can capture  specific event  in the invisible window.
Then using this event I want to send a message to a visible window(written in PB) or trigger an event in the visible window.

Thank you.

0
yehiel
Asked:
yehiel
  • 2
1 Solution
 
DefreeComanCommented:
The following code will make an invisible window as the main window of a program.  To create the window from a DLL, you should be able to just insert the appropriate peices into appropriate functions in the DLL (instead of having WinMain)

#include <windows.h>
LRESULT CALLBACK InvisWinHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

    switch(uMsg){
        //TODO: Handle events here

        default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

return 0;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int iCmdShow){
    WNDCLASSEX wc;
    MSG msg;
   
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = &InvisWinHandler;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;
    wc.hInstance = hInst;
    wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "InvisWin";
    wc.hIcon = NULL;
    wc.hIconSm = wc.hIcon;
    wc.hCursor = LoadCursor(NULL, IDC_HAND);

    RegisterClassEx(&wc);

    HWND hInvisWin = CreateWindowEx(NULL, "InvisWin", NULL, WS_POPUP, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
    ShowWindow(hInvisWin, iCmdShow);
    UpdateWindow(hInvisWin);
   
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
return 0;
}

The parts to notice are
wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
which makes it so the window is painted with a null brush (so no color is actually painted on)
and
WS_POPUP
from HWND hInvisWin = CreateWindowEx(NULL, "InvisWin", NULL, WS_POPUP, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
which makes it so the borders and captions from styles such as WS_OVERLAPPEDWINDOW don't show up.
0
 
yehielAuthor Commented:
In this code I still see the WinMain function. Do what do you mean by 'insert the appropriate pieces into the DLL?
Thanks.


The following code will make an invisible window as the main window of a program.  To create the window from a DLL, you should be able to just insert the appropriate peices into appropriate functions in the DLL (instead of having WinMain)

#include <windows.h>
LRESULT CALLBACK InvisWinHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

    switch(uMsg){
        //TODO: Handle events here

        default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

return 0;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int iCmdShow){
    WNDCLASSEX wc;
    MSG msg;
   
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = &InvisWinHandler;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;
    wc.hInstance = hInst;
    wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "InvisWin";
    wc.hIcon = NULL;
    wc.hIconSm = wc.hIcon;
    wc.hCursor = LoadCursor(NULL, IDC_HAND);

    RegisterClassEx(&wc);

    HWND hInvisWin = CreateWindowEx(NULL, "InvisWin", NULL, WS_POPUP, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
    ShowWindow(hInvisWin, iCmdShow);
    UpdateWindow(hInvisWin);
   
    while(GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
return 0;
}

The parts to notice are
wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
which makes it so the window is painted with a null brush (so no color is actually painted on)
and
WS_POPUP
from HWND hInvisWin = CreateWindowEx(NULL, "InvisWin", NULL, WS_POPUP, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
which makes it so the borders and captions from styles such as WS_OVERLAPPEDWINDOW don't show up.
0
 
DefreeComanCommented:
Sorry, I assumed the main question was about how to get an invisible window, and it was quicker to test my idea without making a DLL.  Here is code from a DLL that creates an invisible window:

#include <windows.h>
ATOM invisClassAtom;

LRESULT CALLBACK InvisWinHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch(uMsg){
        //TODO: process events here

        default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

return 0;
}

bool WINAPI DLLMain(HINSTANCE hInst, DWORD reason, LPVOID reserved){
    switch(reason){
        case DLL_PROCESS_ATTACH:
            invisClassAtom = 0;
        break;
        case DLL_PROCESS_DETACH:
            if(invisClassAtom != 0) UnregisterClass("InvisWin", hInst);
        break;
        default:
            return false;
    }
return true;
}

__declspec(dllexport) void InitInvisWinClass(HINSTANCE hInst){
    WNDCLASSEX wc;
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = &InvisWinHandler;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;
    wc.hInstance = hInst;
    wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "InvisWin";
    wc.hIcon = NULL;
    wc.hIconSm = wc.hIcon;
    wc.hCursor = LoadCursor(NULL, IDC_HAND);
    invisClassAtom = RegisterClassEx(&wc);
}

__declspec(dllexport) void CreateInvisWindow(HINSTANCE hInst){
    HWND hInvisWin = CreateWindowEx(NULL, "InvisWin", NULL, WS_POPUP, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
    ShowWindow(hInvisWin, SW_SHOWNORMAL);
    UpdateWindow(hInvisWin);
}

In the file for the main program:
__declspec(dllimport) InitInvisWinClass(HINSTANCE hInst);
__declspec(dllimport) CreateInvisWindow(HINSTANCE hInst);
 and call the functions (in the right order) with a handle to the instance of the program, then have a message loop:
while(GetMessage(&msg, NULL, 0, 0)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
0
 
gravit9Commented:
To create a invisible windows in a DLL you create a window uning CreateWindow using hWndParent=HWND_MESSAGE.

Code sample:

    WNDCLASSEX wc;
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = MyWndProc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra = 0;
    wc.cbClsExtra = 0;
    wc.hInstance = (HINSTANCE)hModule; // Get this from DLLMain
    wc.hbrBackground = (HBRUSH) GetStockObject(NULL_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "MSGWND";
    wc.hIcon = NULL;
    wc.hIconSm = NULL;
    wc.hCursor = NULL
    RegisterClassEx(&wc);

LRESULT callback MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    // Some code
}

hwnd = CreateWindow(NULL, "MSGWND", NULL, NULL, CW_USEDEFAULT,
                                    CW_USEDEFAULT, 0, 0, HWND_MESSAGE, NULL, (HINSTANCE)hModule, NULL);

Reference: http://msdn2.microsoft.com/en-us/library/ms632599.aspx

You may have to modify this code according to your needs.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now