Click here to Skip to main content
16,011,778 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Panel in C++?? Pin
20-Jun-01 17:01
suss20-Jun-01 17:01 
GeneralSimple CMainFrame App problem Pin
John Treubig20-Jun-01 16:42
John Treubig20-Jun-01 16:42 
GeneralWindows explorer command line Pin
Avneesh Bhatnagar20-Jun-01 16:18
Avneesh Bhatnagar20-Jun-01 16:18 
GeneralRe: Windows explorer command line Pin
20-Jun-01 16:54
suss20-Jun-01 16:54 
GeneralRe: Windows explorer command line Pin
Avneesh Bhatnagar20-Jun-01 17:25
Avneesh Bhatnagar20-Jun-01 17:25 
GeneralThreads and DLLs (Advanced Issues) Pin
Joe Hastings20-Jun-01 15:32
Joe Hastings20-Jun-01 15:32 
GeneralRe: Threads and DLLs (Advanced Issues) Pin
20-Jun-01 15:52
suss20-Jun-01 15:52 
GeneralRe: Threads and DLLs (Advanced Issues) Pin
Stephen Kellett21-Jun-01 5:07
Stephen Kellett21-Jun-01 5:07 
Ummm, its not exactly clear what the problem is. If you had stated what you are trying to acheive I'd have more to work with. However, I'll try. First off, you'll
be pleased to hear that COM has nothing to do with any of this.

1. Is there a way that the main thread in the EXE can call AfxLoadLibrary while a worker-thread is inside of a function in the DLL w/o causing lots of nasty problems? Right now I have hacked up a solution where it suspends any threads it thinks might be using the DLL, and it seems to work, but it causes memory leaks since that thread probably allocated memory inside of the DLL functions it might have called.


Once a DLL is loaded, subsequent calls to LoadLibrary just increment its reference count. So any worries about address space changes, or whatever it is you are worrying
about shouldn't matter.


2. On a completely different note, would there be a way to call AfxLoadLibrary within each thread, and have each thread obtain its own HINSTANCE so that each thread could call AfxFreeLibrary independently of the others? I have tried to do this, and noticed that all copies of the dll functions seem to be the same. I know that in general, DLLs are loaded on a process basis rather than a thread basis, and was wondering if there was a way to override this behavior? If this were possible, #1 wouldn't be so much of an issue.


Nope. The HINSTANCE returned is actually the load address (in the current Win32 implementation) of the DLL. YOu can check this in your debugger. Any calls to LoadLibrary after the first call, just increment the ref count and return the HINSTANCE as usual.


3. What is the safest way to share CWnd objects between threads, and within functions inside of DLLs? Basically, the main EXE opens the HINSTANCE to the dll, and then creates a separate thread to perform various lengthy operations in DLL functions, passing the thread an HWND (window created in EXE), and the thread then passes that HWND as an arguement into the GetProcAddress(ed) function. Inside the DLL, I call: CWnd::FromHandle. This seems to work if I use functions like SetWindowText or SendMessage, but does not work if I call PostMessage on that HWND (or use the CWnd wrapper). Since using SendMessage is dangerous because I don't want to overload the EXEs message pump, I'd really like to be able to use PostMessage. One weird issue here is that CStatic::SetWindowText works fine, while CProgressCtrl::SetPos does not seem to have any effect.


Using HWND is the way to go. Don't turn them in CWnd's as that requires access to the correct CWnd/HWND map which may not be in the thread you are calling from. USing
SendMessage is an absolute no-no in multithreaded code as you can get a deadlock.
You must use PostMessage or SendMessageTimeout. If you use PostMessage, your message
will not get processed until the thread that has the message loop processes the
message. This may be why you are not seeing the result of the posted message (the thread you are posting from may be too busy or in a higher priority group than the
message queue thread.

I don't know if those answers help, but maybe they'll help you post a clearer
explanation of the problem, or why you'd want multiple HINSTANCEs for the same DLL.


Stephen Kellett
--
C++/Java/Win NT/Unix variants
Memory leaks/corruptions/performance/system problems. UK based.
Problems with RSI/WRULD? Contact me for advice.
GeneralRe: Threads and DLLs (Advanced Issues) Pin
Joe Hastings22-Jun-01 3:15
Joe Hastings22-Jun-01 3:15 
Generalstandard search algorithm Pin
Nick Blumhardt20-Jun-01 15:12
Nick Blumhardt20-Jun-01 15:12 
GeneralRe: standard search algorithm Pin
20-Jun-01 15:17
suss20-Jun-01 15:17 
GeneralRe: standard search algorithm Pin
markkuk20-Jun-01 19:42
markkuk20-Jun-01 19:42 
GeneralRe: standard search algorithm Pin
Nick Blumhardt20-Jun-01 21:57
Nick Blumhardt20-Jun-01 21:57 
Generalsendinput & message queue saturation Pin
Amit Jain20-Jun-01 14:49
Amit Jain20-Jun-01 14:49 
GeneralRe: sendinput & message queue saturation Pin
20-Jun-01 15:20
suss20-Jun-01 15:20 
GeneralRe: sendinput & message queue saturation Pin
Amit Jain21-Jun-01 4:37
Amit Jain21-Jun-01 4:37 
GeneralUSB Keyboard Pin
DrunkerII20-Jun-01 13:58
DrunkerII20-Jun-01 13:58 
GeneralRe: USB Keyboard Pin
20-Jun-01 15:21
suss20-Jun-01 15:21 
QuestionHow to detect an application launch ? Pin
20-Jun-01 10:01
suss20-Jun-01 10:01 
AnswerRe: How to detect an application launch ? Pin
20-Jun-01 10:03
suss20-Jun-01 10:03 
GeneralRe: How to detect an application launch ? Where to start searching for shell extension ? Pin
20-Jun-01 10:40
suss20-Jun-01 10:40 
GeneralRe: How to detect an application launch ? Where to start searching for shell extension ? Pin
Michael Dunn20-Jun-01 20:58
sitebuilderMichael Dunn20-Jun-01 20:58 
Generalanother Important Question Pin
Fady Elias20-Jun-01 9:50
Fady Elias20-Jun-01 9:50 
GeneralRe: another Important Question Pin
Ben Burnett20-Jun-01 10:14
Ben Burnett20-Jun-01 10:14 
GeneralFile Sharing Violation Pin
Fady Elias20-Jun-01 9:48
Fady Elias20-Jun-01 9:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.