Solved

Create new control from MFC dll

Posted on 2013-01-28
3
441 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 33

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 185
t-sql sp_addlinkedserver possible risks 3 66
rhino JavaScript import, load 25 82
Resolve Dependency Issues 4 64
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

808 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