Question

Window CreateWindow() in DllMain()

Asked by: biswabiet

Hi if anybody can help me on this
i need my DLLMain should work like a Winmain().
i need all the controll in the DLL . if any aplication loads the DLL , the DLL shoul self sufficinet to create the window and able handle the event in a thread.
i am failing in CreateWindow()  getting 1407 in getlasterrro().
find the code
thanks

#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "resource.h"
 
 
#define MAX_LOADSTRING	100
 
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
 
	switch (message) 
	{
		case WM_CREATE:
			MessageBox(hWnd, "test", "test", MB_OK);
			MessageBox(NULL, "test1", "test", MB_OK);
			break;	
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
//				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
 
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;
 
		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}
 
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
 
	wcex.cbSize = sizeof(WNDCLASSEX); 
 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_VISALI);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_VISALI;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
 
	return RegisterClassEx(&wcex);
}
 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 HWND hWnd;
   int ret;
   char buf[100];
 
   hInst = hInstance; // Store instance handle in our global variable
 
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   ret = GetLastError();
 
   sprintf(buf,"ret = %d",ret);
   MessageBox(NULL, buf, "ret", MB_OK);
 
   if (!hWnd)
   {
      MessageBox(NULL, "After register", "3", MB_OK);
	   return FALSE;
   }
 
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
 
   return TRUE;
}
 
int __stdcall Fun()
{
	MessageBox(NULL, "Fun", "Fun", MB_OK)
 
	return 1;
}
 
BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
SG msg;
	HACCEL hAccelTable;
	int       nCmdShow =1;
		// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_VISALI, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);
 
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
 
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_VISALI);
 
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
 
	return msg.wParam;
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-09-04 at 09:28:22ID24708210
Topics

Windows MFC Programming

,

Windows 3.x Setup

,

Open Source Programming

Participating Experts
4
Points
125
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. CreateWindow
    When using CreateWindow , the first classname could be like "button" or "static". When using the "listbox" it should be able to make a drop down listbox.. How do i fill this listbox with variables ? How do i get the ID of what i clicked on ? I w...
  2. Use hModule of DLL or HINSTANCE of mainapp in CreateWi…
    I have a dll in which I create a Popup Window. In the code for creating the window I must supply an HINSTANCE to both the WNDCLASS definition and in the call to CreateWindow(). I am not sure if I should use the HINSTANCE of the main app (which I send to the dll through a fu...
  3. CreateWindow
    Hi! Why this code does not work ? :( ---------- INITCOMMONCONTROLSEX cc; cc.dwSize = sizeof(INITCOMMONCONTROLSEX); cc.dwICC = ICC_INTERNET_CLASSES; ::InitCommonControlsEx(&cc); ::CreateWindow("WC_IPADDRESS","btn",WS_CHILD|WS_VISIBLE,300,300,50,50,thi...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: pgnatyukPosted on 2009-09-04 at 10:59:57ID: 25261925

1407 - Check the window class registration. Probably there is a problem with hInstance. Use GetModuleHandle() there. GetModuleFileName gives the dll name. This name will be the parameter for GetModuleHandle().
If it does not help - you use a lot from the resources in the windows class registration structure. Set only default values there, or variables declared in the same cpp-file.
When CreateWindow will work, you can try to modify these parameters again.
Compare your code with the attached here.

GetClassInfo - checks if you window was registered.

//somewhere
static LPCWSTR s_szWndClassName = L"TheWindowIwillCreate";
 
//then
WNDCLASSW wc = { 0 };
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = NULL;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = m_szWndClassName;
 
//then just egister
RegisterClassW(&wc);
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:

Select allOpen in new window

 

by: Gideon7Posted on 2009-09-04 at 14:13:57ID: 25263386

First, your DllMain routine is ignoring dll_reason_for_call.  Your DllMain routine will be called in four different situations: 1) on program entry, 2) new thread start, 3) thread termination, and 4) program termination.  You must inspect dll_reason_for_call and only run your code on the first case (dll_reason_for_call == DLL_PROCESS_ATTACH).

Second, when your code is running inside of DllMain it has the loader lock set.  While the loader lock is set you cannot load any other DLLs into your application.  

Your code never returns from DllMain until termination.  It holds the loader lock indefinitely.   This will deadlock if another DLL is loaded for any reason.

You need to completely rethink your approach; it is fundamentally broken.

 

by: pgnatyukPosted on 2009-09-04 at 14:43:08ID: 25263561

This window works.

the header file looks so:


#ifdef WND_TEST_DLL_EXPORTS
#define WND_TEST_DLL_API __declspec(dllexport)
#else
#define WND_TEST_DLL_API __declspec(dllimport)
#endif
WND_TEST_DLL_API void Create();
WND_TEST_DLL_API void Destroy();

// wnd_test_dll.cpp : Defines the entry point for the DLL application.
//
 
#include "stdafx.h"
#include "wnd_test_dll.h"
 
static LPCWSTR s_szWndClassName = L"TheWindowIwillCreate";
static HWND s_hWnd = NULL;
 
 
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
		Create();
		break;
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		Destroy();
		break;
	}
    return TRUE;
}
 
ATOM RegisterWndClass(WNDPROC WndProc)
{
	WNDCLASSW wc = { 0 };
	wc.style = CS_HREDRAW | CS_VREDRAW ;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hIcon = NULL;
	wc.hInstance = GetModuleHandle(NULL);
	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = s_szWndClassName;
 
	return (RegisterClassW(&wc));
}
 
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lResult = -1;
 
	switch (uMsg)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT paint = { 0 };
			HDC hDC = ::BeginPaint(hWnd, &paint);
			::EndPaint(hWnd, &paint);
			lResult = 0;
		}
		break;
 
	default:
		lResult = DefWindowProc(hWnd, uMsg, wParam, lParam);
		break;
	}
 
	return lResult;
}
 
void Create()
{
	WNDCLASSW wndcls = { 0 };
	if (!::GetClassInfoW(GetModuleHandle(NULL), s_szWndClassName, &wndcls))
	{
		if (!RegisterWndClass(WindowProc))
			return;
	}
 
	s_hWnd = CreateWindowW(s_szWndClassName, 
		NULL, WS_POPUP, 100, 100, 300, 300, NULL, (HMENU)0,
		GetModuleHandle(NULL), NULL);
 
	//DWORD nErr = GetLastError();
 
	if (s_hWnd == NULL)
		return;
 
	ShowWindow(s_hWnd, SW_SHOWNORMAL); 
	UpdateWindow(s_hWnd); 
	::SetFocus(s_hWnd);
 
	MSG msg = { 0 };
	// Pump messages until a PostQuitMessage.
	while(::GetMessage(&msg, NULL, 0, 0))	
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}
 
void Destroy()
{
	if (::IsWindow(s_hWnd))
	{
		::SendMessage(s_hWnd, WM_CLOSE, 0, 0);
		s_hWnd = NULL;
	}
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-09-04 at 14:55:21ID: 25263666

Sorry, there is a message loop in the end of Create() - it should be removed.

 

by: Gideon7Posted on 2009-09-04 at 14:56:20ID: 25263675

The latter example is a little better, assuming there are no COMCTL32.DLL controls in the window (such as Edit, Checkbox, etc) or anything else that loads a DLL during window creation.

Creating a non-trival window - such as any window that includes sub-windows like edit controls or checkboxes - in DllMain is fraught with danger.  In the general case you are better off deferring the window creation via PostMessage where no such DLL-deadlocks can occur.

 

by: pgnatyukPosted on 2009-09-05 at 01:21:15ID: 25265346

I'd say it is better to have few exported futions in this DLL that will initialize an internal data and manage the window. Open a window when the dll is attaching to a process... I cannot say that this a nice idea or a good trick. For example, what to do with the message loop? Should it be in the DLL or the application that loads the DLL has own message loop?
But it was a question about the window creation from DllMain. We have solved it.

 

by: biswabietPosted on 2009-09-06 at 11:01:02ID: 25270851

HI pgnatyu:

Thanks a lot, really it worked, but i have a doubt, if i use message loop i could not come out from my calling application. if i dont use mesage loop its happening but i have a doubt, wheather i could reacieve the data(returned from event) in lparam parameter in WndProc().
kindly do inform me.

thanks
biswa

 

by: wieser-software-ltdPosted on 2009-09-06 at 12:47:27ID: 25271225

You really shouldn't be doing anything like this in your DLL main.

Raymond Chen from Microsoft has quite a lot to say on the subject:
http://blogs.msdn.com/oldnewthing/archive/2004/01/27/63401.aspx

From the article:
"And absolutely under no circumstances should you be doing anything as crazy as creating a window inside your DLL_PROCESS_ATTACH. In addition to the thread affinity issues, there's the problem of global hooks. Hooks running inside the loader lock are a recipe for disaster. Don't be surprised if your machine deadlocks."

 

by: pgnatyukPosted on 2009-09-06 at 22:08:56ID: 25272785

I'd added just few functions into the DLL. For example:
1. CreateWindow
2. Run
3. Get/Set additional parameters and retrieve data.
4. Close/Destroy.
If you will have separetely Create and Run, it will look more or less as a standard dialog that has also Create and DoModal methods.
If you use this DLL from a console application, you must have a message loop (the Run function), otherwise your DLL window will not work - will not receive any message. If you have a dialog-based application, you already have a message loop, and your DLL window will work somehow - if you need this window to be the modal dialog, you can use Run. For the modaless window, you will need to pass to it only its messages.

DllMain is like constructor and destructor in your class - you want them to be as small as possible - so your class will be created very fast.

It is possible to add a window or whatever you want into the DllMain. But from my point of view, it may make sense for a security mechanism, registration, etc.

 

by: biswabietPosted on 2009-09-07 at 00:16:55ID: 25273207

hi,
i really dont want to do any thing inside DLL main.
i am creating window outside DLL Main. but i need to get the lparam value as on when windows
through any event, i have a 3rd party windows patch which fires event when some thing goes wrong, and fills the resulting data in to lparam. i need that data.  my question is if will not use message loop , can i capture all windows events in wndproc() including the lparam data.

i am really novice on this
kindly help.

thanks
biswa

 

by: pgnatyukPosted on 2009-09-07 at 00:30:15ID: 25273267

I cannot say that I understand what you need. :)
Probably you need to learn the hooks. It is not a trivial question:
http://msdn.microsoft.com/en-us/library/ms997537.aspx

http://www.codeproject.com/KB/system/WindowsHookLib.aspx

I hope I'm wrong. So, if you create your window in your application, so you receive all messages and you can pass the data from this window to a DLL. For example, load the DLL, create the window, and, if you get WM_KEYDOWN message, call a DLL function.

 

by: biswabietPosted on 2009-09-07 at 02:21:05ID: 25273680

let me very clear
i want a dll which wil have start() and stop() as exported to the calling application.
my calling application is a VB console application.
VB calling part will only call Start() where in DLL code window will be created and wait for events. but start() should be a nonblocking method and should return to to calling part. and when ever VB code will call Stop() then all the window destruction and other de-initialisation would be done in that.

plz answer any suitable way
thanks
biswa

 

by: itsmeandnobodyelsePosted on 2009-09-07 at 02:46:57ID: 25273805

First, as Gideon7 already told, move all windowing code out of DllMain. DllMain is not comparable with main or WinMain. It has dll initializing functionality only.

Second, if you want to have asynchronous access to dll functions, call the dll functions from threads. Then, after creating the thread the calling function could return to VB, thus is not blocking. In the thread the window can be created and the message loop can run, until the Stop from VB would send a WM_CLOSE (or other) message to the window created in thread. That would trigger an appropriate handler you have to provide (with WinProc) and  then you properly can terminate from thread.  

 

by: pgnatyukPosted on 2009-09-07 at 02:47:46ID: 25273811

Ok.
I hope you know how to work with threads.
In DLL in Create function we need to create a thread and create window there. This thread will have also the message loop. This thread will not block your console application.
Function Stop will send WM_CLOSE to the dll window and, after the destroying of the window, will close the thread.

 

by: wieser-software-ltdPosted on 2009-09-07 at 03:05:45ID: 25273900

Just moving it out of dll main isn't enough though.  It shouldn't be called by it either.

 

by: pgnatyukPosted on 2009-09-07 at 03:12:31ID: 25273939

In the code I posted, remove Create and Destroy from the DllMain. You can call them from your app directly. And make a thread in your app as itsmeannobyelse said or ... implement all related to that window in the DLL.
So do you have the answer?
If you will decide to implement the thread in DLL - I will assist you.

 

by: biswabietPosted on 2009-09-07 at 08:12:43ID: 25275577

hi pgnatyuk:

i got it nicely as you explained now its working fine with my calling application. what are the mesures to take while synchronising threads in DLL as i have many exproted methods those are also called at the time of any windows event occurs.
thanks biswa

 

by: pgnatyukPosted on 2009-09-07 at 08:23:38ID: 25275644

You are welcome.
Because you have only one window, so you cannot receive 2 messages simulteniously - you will handle them one by one. So I don't think you need a synchronization in this case. Of course, if you don't have a function that will launch one more thread.
If anyway you wish it (or I'm wrong), you can add a critical section into your DLL. In this case you will need to initialize this critical section before you create your window (it can be done in DllMain even) and UnInitilize when you close your window (or in DllMain on Detach).
Then, in each function you will have to write EnterCriticalSection in the beginning and LeaveCriticalSection before return.
If you use this DLL from different applications simulteniously, you need a named object. Critical section is not enough. You will have to implement a named mutex.
Sorry, if you know all that. I just understand that you are asking.

 

by: itsmeandnobodyelsePosted on 2009-09-07 at 08:37:45ID: 25275746

>>>> what are the mesures to take while synchronising threads in DLL as i have many exproted methods those are also called at the time of any windows event occurs.

The other exported functions only must be synchronized with the GUI thread if they want to outputs their results in that GUI window. The easiest synchronization is by calling PostMessage from the functions and handling the messages in the GUI thread cause PostMessage is a thread-safe (even system-safe) function.  E, g. if one of the exported functions makes some unzip of a huge zip file and you want to signal progress in the GUI, you would send a private messgage after each file unzipped and the GUI could update a progress bar accordingly.

 

by: pgnatyukPosted on 2009-09-07 at 10:58:35ID: 25276464

Kill me if I understand why you set B. Why you marked answer ID:25275644?
Read your original question. The answer was ID:25263561. The code posted there created a window from the DLL as you requested in the question. Than, we proposed a better design and help to implement it. Than, answered on additional question. Is it B?

 

by: biswabietPosted on 2009-09-07 at 11:37:11ID: 25276628

hi pgnatyuk:

i am really new in this space sorry i will do the best here after.
thanks
bis

 

by: pgnatyukPosted on 2009-09-07 at 11:47:37ID: 25276684

You can re-open this question. There is "Request attention" near the question. Ask the moderator to open for you and you will close again

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...