Solved

Create new control from MFC dll

Posted on 2013-01-28
3
417 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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now