Link to home
Start Free TrialLog in
Avatar of cjustas
cjustas

asked on

Create new control from MFC dll

I have application (MFC based) that loads dlls and calls specific methods. In one of those methods, I want to create CEdit control on that applications one of windows.

1) I'm getting handle of parent window
2) Create new CEdit object and call CreateEx with obtained parent CWnd
3) At this point control is visible in application it self but then application freezes and do not accept any input.

I'm thinking its something to do with message handle on parent window, as I create control at runtime from dll. Is there any way to solve this?

Pseudo code of one of external dll methods:
extern "C" VOID PASCAL EXPORT AddEditBox(HWND hWnd)
{
	CWnd *hmain = CWnd::FromHandle(hWnd); //I've checked and hmain is CORRECT CWnd Object, I can do other manipulations with it (like resizing etc.)

	if(hmain != NULL) {
		m_edit = new CEdit;
		m_edit->CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""), 
			WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
			CRect(0, 0, 60, 20), hmain, 1);
	}
}

Open in new window


Long story short, I would like to inject/create completely new MFC control (CEdit) in to application (which I don't have control of) from my custom DLL without crashing application and receiving and handling messages as normal CEdit control.

Thank you.
Avatar of jkr
jkr
Flag of Germany image

Haven't done MFC in a while, but I do remember TN058 (http://msdn.microsoft.com/en-us/library/ft1t4bbc.aspx - "TN058: MFC Module State Implementation")



extern "C" VOID PASCAL EXPORT AddEditBox(HWND hWnd)
{
        AFX_MANAGE_STATE(AfxGetStaticModuleState( )) // <-- add this

	CWnd *hmain = CWnd::FromHandle(hWnd); //I've checked and hmain is CORRECT CWnd Object, I can do other manipulations with it (like resizing etc.)

	if(hmain != NULL) {
		m_edit = new CEdit;
		m_edit->CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""), 
			WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
			CRect(0, 0, 60, 20), hmain, 1);
	}
}
                                  

Open in new window

Avatar of cjustas
cjustas

ASKER

I'm sorry I forgot to add this, but in my code this line is already there, I just cleaned it out when I was removing unrelated code from pseudo code example. With this line is behaving same as described in initial post.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial