Solved

Create new control from MFC dll

Posted on 2013-01-28
3
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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 …

726 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