Problem with MFC CWnd::CreateControl method - visual-c++

I have a problem with CWnd::CreateControl method while loading custom ActiveX control from the MFC application.
I have list of Custom ActiveX controls which are implemented Create method inturn calling CWnd::CreateControl method.
I am having Dialog window, in the OnInitDialog, I have started timer thread using Settimer(). In the OnTimer event, I am loading all the controls by calling respective control's Create method. After opening and closing the dialog window more than 10 times, OnTimer is not able to load the contols.
I checked the return value which is false and the GetLastError which is 0x0 (Operation successful). I was debugging completely and checked all the possiblities of errors before this event. I couldn't find the root cause what made not loading the controls.

You may want to refer the below question similar occurence of the problem
Exception while opening file
The similarity being it was working fine but after sometime it would throw exception and when we check the error message it would be no error occured. The issue wont be directly at the line that causes exception. It would be lurking elsewhere in the application.
In my case when i changed the way i was accessing the method and it started working.

Related

I'm developing an application where I want to present information in the Main window status bar

I've coded Main precisely as described in Microsoft Docs - https://learn.microsoft.com/en-us/cpp/mfc/updating-the-text-of-a-status-bar-pane?view=msvc-170 and it processes the function when there's a command message to deal with. It will put anything that I want in a status bar pane. But when a Child of Main is opened, it stops. I've tried making the declaration of CStatusBar public and accessing its functions remotely, to no avail. Menu prompts display, but no status bar pane. I've placed a break in the update procedure which triggers while Main is on the surface but not when Child is in control. What's the problem?
While waiting, I found this, and corrected my mistake.
The only alteration was the instruction to include MainFrm.h in the Child which, in my case, caused a redefinition error. I removed all of the code from Main and placed it in Child. Now I'm getting the effect that I desired.

CMFCLinkCtrl throws runtime error when it is using with dll in VC++

I have placed a CMFCLinkCtrl - (control to show the links in the dialog frame) in my modal dialog box application which works perfectly.
upon setting the SetURL("www.google.com") it could able to invoke it.
When i do the same in a dll (dialog box is part of dll) and when the user clicks the control, it is throwing an runtime exception and i could not able to debug it.
I have two questions,
can we use the CMFCLinkCtrl controls inside a dll based out of dialog box?
if so, how to debug and find the issue?
error Image:
thanks,
Saravana
At some time I have removed the code
CWinApp theApp
from the dll implementation, since I'm not using it. That's the reason behind this error. When I added it back it is able to invoke the URL.
As I have mentioned it is get struck in the below line, just explored more on why the failure of AfxGetApp() and found this reason.
void CCmdTarget::BeginWaitCursor()
AfxGetApp()->DoWaitCursor(1)

Saving the states of JavaFX controls on exit

I have a bunch of control objects (TextBoxes, to be precise) that get injected into my code using the #FXML annotation during the FXML load.
I would like to save the states of these controls, specifically the text values, when the user closes the Scene by clicking the close box on the title bar.
However, when I trap the CloseRequest event in an OnCloseRequest handler I find that the values of the control variables are null (the original injection works, so this is something that happens in between the loading of the FXML and the calling of OnCloseRequest).
Can anyone explain this behavior and/or suggest how I can get the functionality that I want?
TIA
onCloseRequest is
Called when there is an external request to close this Window. ...
(from Javadoc). One of the meanings of "external request" is when you close the window through OS native window close button. The closeRequest event is not triggered through the programmatic stage.close() or stage.hide() calls. So consider to handle onHiding or onHidden events.
Otherwise post your OnCloseRequest handler code.

MFC/3rd party multithreading hang

I'm currently working on a program using MFC. The current third party function starts a thread after an action has been completed using MFC (ie. Checking a checkbox, which starts a MFC thread I believe).
The problem occurs when I check the checkbox, at which point the entire program hangs. I read a few interesting discussions on CProgramming and msdn, it seems that the problem occurs because the new third party thread is calling WaitToSomething() when MFC is updating a control.
Something interesting to note:
When I debug the program, the program hangs (aka. repeatedly calls WaitToRead() ) after I check the checkbox and a new thread is trying to start
When I run the program without debugger, the program is fine UNTIL I switch to another window (ie. Internet browser, Notepad, etc)
My hypothesis:
check to make sure that MFC has finished updating the control before starting a new thread
If anyone has any suggestions or solutions, please leave a comment. Thanks.
Edit:
MFC is not thread-safe at object level (only at class level), so problem occurs when two threads work on the same CButton object.
Q: How do I make it thread safe?
A colleague helped me figure out what the problem was.
The reason why it was hanging is because that the control containing the checkbox is a child dialog, and when it finished updating the message never got passed up to its parent (so when 3rd party thread calls WaitFor(), the MFC thread never completes because a parent dialog thinks its child is still updating the controls).
Fix:
Under 'Properties' in the child dialog's control, set the 'Control' flag to true (and if it has children, set the 'Control Parent' flag to true as well).
Hope this helps.

How to avoid use of timer when using TThread to communicate with UI thread

I have a TThread which receives and sends to a device on a COM port. After I read the data, I want to activate the GUI (not in the same thread) using Synchronize(function name). However, when I call the GUI's form function to perform a button click, I get an access violation. I checked to see if the form's value is null and it is not, since this would be an obvious reason for access violation. Right now, I am setting global flags and using a timer that continuously checks to see if a certain condition is met and if so, then I fire off the button click event in that form. That seems to be the only way to avoid getting the access violation.
I really don't like timers so is there a way to avoid having to use a timer on the form?
You can post a message to the window in question. The timer works in a similar manner. It just fires off a windows message inside the form. You obviously have a handle to the window.
CWnd::PostMessage(...) Don't use send message, it gets processed inline and could cause your thread to stop working.
Typically when you have a worker thread that attempts to access Guithread, they conflict. It's been a while since I've used MFC and threading but that's what I remember. I believe it's documented to work that way.
I found the problem. I thought I was checking if my Form was null, but I was not. I fixed it making sure the form I was referencing is not null.
Edit: Turns out that one of the forms that is called when I call Fbutton1Click() is Modal so it blocks my thread. I ended having to go back to a timer to call the button click instead.. oh well.

Resources