Error: : PostMessage (AfxGetMainWnd () – & gt; M_hWnd, UM_XX, 0, 0);
or: : sendMessage (
AfxGetMainWnd()-> M_hWnd, UM_XX, 0, 0);
How to solve it?
Verify that afxGetMainWnd () returns NULL
int k = (int)(& (((CWnd*)NULL)-> m_hWnd));
The value of k is 0x20
In general: afXGetApp ()-> GetMainWnd (); AfxGetMainWnd() is used only on the main thread;
How: foreground thread and background thread, afxGetApp ->; GetMainWnd() differs from AFXGetMainWnd ()
AFXGetMainWnd () is an example of a window handle that can be used to retrieve a window handle. AFXGetMainWnd () is an example of a window handle that can be used to retrieve a window handle. Clever you must have already understand, if you have been tracking AfxGetMainWnd () call, will find that it earned a AFX_MODULE_THREAD_STATE threading module save active threads in the window handle, and a background thread since no window, then obtains the window handle is you from??(maybe someone’s understanding of the background is not display window, even with all the window, as long as it doesn’t show is a background thread, strictly speaking is not the case, the window is mainly used to interact with the user, a window is hard to avoid jam, And the background thread is often used to carry out some behind the operation or processing, is through the foreground information or data to carry out the corresponding operation, if the window is hidden to talk about what information transfer?The case can not be done in the background like other threads. The key is to know the difference and how to use it.)
AfxGetApp()-> GetMainWnd () is the main window handle, no matter in that thread calls are no problem, because it is first obtained the main thread handle, to achieve the main thread of the active window (such as switch view can lead to replacement, I am not sure this kind of circumstance), if there is no active window is the main window, any program must have a main window, so it calls there is no problem, if you want to achieve the program’s main window suggested AfxGetApp () – & gt; GetMainWnd().
Note that the console program has no window and its window handle is always 0. Second, the background thread is actually a desktop control program, just not the main thread. Also, threads created using functions such as API CreateThread cannot generate CWinThread objects. So if you want to use CWinThread object function, and some of the global function, as AfxGetMainWnd (), you must use CWinThread object CreateThread function, or use the AfxBeginThread () function creates a thread, or may be an error, because the MFC to the management of the thread is done through the CWinThread object, it is not hard to see through the tracking code below you. I remember that there was a predecessor once mentioned this problem, but I do not know whether it understands the reason
Here is the trace code:
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
{ CWinThread* pThread = AfxGetThread();
return pThread! = NULL ?pThread-> GetMainWnd() : NULL; }
CWinThread* AFXAPI AfxGetThread()
{
// check for current thread in module thread state
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
CWinThread* pThread = pState-> m_pCurrentWinThread;
// if no CwinThread for the module, then use the global app
if (pThread == NULL)
hread = afxGetApp ();
return pThread;
}
You can see that calling afxgetMainwnd () on the main thread is not a problem (except for console programs) because it returns the main thread handle if the fetched thread handle is empty.
_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
{ return afxCurrentWinApp; }
CWnd* CWinThread::GetMainWnd()
{
if (m_pActiveWnd ! = NULL)
return m_pActiveWnd;// probably – place in active
// when not inplace active, just return main window
if (m_pMainWnd ! = NULL)
return m_pMainWnd;
return CWnd::GetActiveWindow();
}
I don’t need to worry about AFXCurrentInApp.
Hey hey, all finished, if your program exists similar to the above problems can be sure to change back oh.
Read More:
- Opencv: 0xc0000005: an access conflict occurred while reading location 0x0000000010
- There is an unhandled exception at: 0xc0000005: an access conflict occurred while reading location 0x00000000.
- Bug resolution of 0xc0000005: access conflict occurred when reading location of 0x00000000.
- Three possibilities of “unhandled exception: 0xc0000005: access conflict when reading location 0x00000000”
- VC + + COM programming error of 0xc0000005: access conflict when reading location 0xfeefef6
- C language problem: 0xc0000005: access conflict occurred when writing to location 0xffffcc.
- Unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- 0xc0000005: solution to access conflict when writing to location 0x00000000
- C / C + + error handling (document): unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- [debug notes] pointer error when STD:: vector is used in VTK “access conflict when reading location XXX”
- Memory error unhandled exception: 0xc0000005: read location: 0x00000
- Unhandled exception at 0x00000000: 0xc0000005: access violation at position 0x0000000000
- Dxly solves the fatal error of AutoCAD2006: unhandled access violation reading 0x0000 exception at 6532b0h.
- CONDA reports an error: conflict package conflict
- register ASP.NET An error has occurred: 0x8007b799
- A problem occurred configuring project ‘:x x x‘. > java.lang.NullPointerException (no error message)
- Android Error: Consumer closed input channel or an error occurred. events=0x9
- [Java] Java program error: exception_ ACCESS_ VIOLATION (0xc0000005)
- Springboot uses druid to log in MySQL. An error occurred: access denied: errorcode 1045, state 28000
- Error 1935. An error occurred during the installation of arcgis10.3 and HResult: 0x80070bc9