Solved

Create new control from MFC dll

Posted on 2013-01-28
3
468 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:cjustas
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 38829036
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

0
 

Author Comment

by:cjustas
ID: 38829053
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.
0
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 38831299
if you don't have control of the application, the AddEditButton function was not called. even if you would get be called by some kind of callback, messages for the new control were not handled by the application.

the only way i know you could achieve what you want is to hook the application and catch some suitable messages of the message queue of the main application. even then you could not use standard cedit windows proc (cause the application still was not aware of the control) but need to subclass the window with your own windows proc (or use your hook for that purpose).  beside that you would need administration privileges for installing a hook, it is a lot of works to handle a new control by messages only which in my opinion could not be done with reasonable efforts.

Sara
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 58
numbers ascending pyramid 101 243
Access to class from any project within a solution. 6 35
what type of pointer is this pointer? 3 24
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question