Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi again.

I have a Com server DLL that I have given a window to that is for the sole purpose
of retrieving messages from the Windows message queue. The window is created in a different thread. I got the code for this from a post over at the codeguru.com

This works nicely. And provides the IPC I was after. Yay.

What I would like however, is individual windows attached (for lack of a better word) to object "instances" that are created in the classfactory. While I could try and try, I don't quite know what I need to change.

Right now with each instance of the object created the same visible window's name is changed to reflect the current object instance ID, when I expected a new window separate and apart from the first.

It seems to me that if I could dynamically change the name of the window structure
name upon each creation that might do it, maybe not.

Here is the window code.

WindowName is a pointer to the created object instance name that is created upon
object creation time. Once it is all working I will change the windows to be invisible message only windows
who will be found by the window name. (object instance ID).

Thanks in advance.

//////////////////////////////////////////////////////////////////////////////////

Cbcrshimcontrol::CMyWindow::CMyWindow(char *WindowName,const int width,const int height)
 {
	 Cbcrshimcontrol::CMyWindow::WindowName = WindowName;
		 _beginthread( &CMyWindow::thread_entry, 0, this);
 }

Cbcrshimcontrol::CMyWindow::~CMyWindow(void)
    {
        SendMessage(_hWnd, WM_CLOSE, NULL, NULL);
    }


void Cbcrshimcontrol::CMyWindow::thread_entry(void * p_userdata)
    {
        CMyWindow * p_win = static_cast <CMyWindow*> (p_userdata);
        p_win->create_window();
        p_win->message_loop();
    }
void Cbcrshimcontrol::CMyWindow::create_window()
    {
		
		
		char AppName[10]; // the object instanceID holder
		strcpy(AppName,Cbcrshimcontrol::CMyWindow::WindowName);
        WNDCLASSEX wcex;

        wcex.cbSize             = sizeof(WNDCLASSEX);
        wcex.style              = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = &CMyWindow::WindowProc;
        wcex.cbClsExtra         = 0;
        wcex.cbWndExtra         = 0;
        wcex.hInstance          = GetModuleHandle(NULL);
        wcex.hIcon              = LoadIcon(NULL, IDI_APPLICATION);
        wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
		wcex.lpszClassName  = (LPCTSTR)AppName;  
//		wcex.lpszClassName  = g_AppName;
        wcex.hIconSm            = LoadIcon(NULL, IDI_APPLICATION);

        RegisterClassEx(&wcex);

        _hWnd = CreateWindow((LPCTSTR)AppName, (LPCTSTR)AppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,NULL /*HWND_MESSAGE*/ , NULL, GetModuleHandle(NULL), NULL);

        ShowWindow(_hWnd, SW_SHOWDEFAULT);
        UpdateWindow(_hWnd);
    }

void Cbcrshimcontrol::CMyWindow::message_loop()
    {
        MSG msg = {0};

        while (GetMessage(&msg, NULL, 0, 0))
        {
            if(msg.message == WM_QUIT)
            {
                break;
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

	void strappnd(char *dest, char *src)
{
	/* appends one string to another */
             while(*dest != '\0')
             {
                     *dest++;
             }
	while (*src != '\0')
		*dest++ = *src++;

	*dest = '\0';
}

 LRESULT WINAPI Cbcrshimcontrol::CMyWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
		if(uMsg == g_uiProcCommMsg)  // Registered Messages not working yet.
		{
			// do something
		}

        switch(uMsg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

		      case WM_KEYDOWN:      /// IPC Receive Message Testing
           // AfxMessageBox("WM_KEYDOWN");  /// Works.
			
            return 0;
 
        }

        return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }
Posted
Updated 1-Jul-11 7:32am
v3

1 solution

Every time you call CreateWindow() you're creating an independent instance of a window... so if you want to create a separate instance within a class, then just do it within any non-static member method of the class.
 
Share this answer
 
v2
Comments
Ron Anders 1-Jul-11 14:01pm    
Do you mean if I CALL CreateWindow from a static member there will be only 1 instance, but if I CALL CreateWindow from a non-static member I will create a separate instance?

Just trying to clarify what "do it" means in the context of your answer.

That you very much by the way. ;-)
Albert Holguin 1-Jul-11 14:48pm    
Actually, you made me rethink that... if its created within a static, you'll still end up with multiples, but any calls within a static are not allowed to access instance objects. But the only thing that you really need to know is that if you've created the window within your class, the created window will be instance unique. Meaning each instance will have its own window (you can look at the pointers and you'll see they're different).
Ron Anders 1-Jul-11 18:40pm    
I did just that Albert. I integrated the Window into the DLL class eliminating the Class in a Class that was easiest for proof of concept.

It now works. Three instances requested results in 3 windows with the proper window names I've still some testing but by george and all of you, I think I've got it!

Thanks a million everyone.
:Ron
Albert Holguin 1-Jul-11 19:39pm    
Great! :)

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