Windows C++ MFC migration: AfxGetThread Assertion. Why does win32u.dll load before mfc140d.dll in some cases? - c++

I have customer code written for Visual Studio 6.0 MFC which has a simple GUI and launches an EXE with arguments. This code was ported from VS6.0 to VS2019 about 2 years ago and works in a production environment on several systems. We now have a new system where the code fails to function... and I'm starting to dig.
The code is throwing exception in appcore.cpp line 196
It is crashing at AfxGetThread() now that I have been able to get VS2019 to find "appcore.cpp".
This is new information.. I will be searching on AfxGetThread next... so this question likely to be a duplicate now.
One difference I have detected is the order where the Visual Studio 2019 debuger loads symbols. I can't say for certain that this is an indication of the actual DLL load order at runtime, but it appears to be. The screenshot below is the SYMBOL load order where a difference is detected between the working and non working instance of the application.
In the image below we have a Tortoise SVN Diff of two ASCII files. One is the DLL symbol load order on the left when the application works. The second is the DLL symbol load order on the right when the application fails to work. Line 7 is the divergance, where in the failig case the library win32u.dll is pulled in before mfc140d.dll.
The customer code uses some Apache log4cxx libraries which I need to investigate, but at this point in the load sequence I'm not 100% sure differences in those libraries or *.h files used at build time could influence the DLL loading order.
So this is the puzzle I'm looking at.
I will include some links to relevant StackOverflow questions that are similar in my search for an answer to this question.
Possibly Useful Links:
https://learn.microsoft.com/en-us/cpp/porting/porting-guide-mfc-scribble?view=msvc-170

The DLLs are searched in order in different locations: Standard Search Order for Desktop Applications
Most likely the DLLs on the failing machine are missing or they are in the wrong location, so Windows grabs something else.
Make sure all the dependencies are installed in the correct folders.

In this case, the crash in appcore.cpp was due to the code having 2 CWinApp derived objects in the code. And the crash occurs at construction time.
The first hurdle is to get VisualStudio2019 to find appcore.cpp and be able to step into this code. I browsed to C:\Program Files(x86)\VisualStudio and searched for "appcore.cpp". This provided the trail of breadcrumbs to get Visual Studio2019 the correct path when it asked for the file.
In my case the path is:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30037\atlmfc\src\appcore.cpp
The second hurdle was to put a breakpoint at the ASSERT point, as the FIRST time the program comes up, this ASSERT is OK. At least in my case... the first constructor of the CWinApp object succeeded. So in my case the offensive code which was unexpectadly constructing a CWinApp derived object ran first. Then the second iteration through the ASSERT would happen.
By placing a breakpoint at the ASSERT, you can re-launch and look at the stack trace of the successful object to determine if it's expected or not.
In my case there was a *.h fix required to get ancient Visual Studio 6.0 MFC code to build and link successfully. I don't have the exact secret, but it's essentially getting a *.h to specify the proper WIN_VER minimum windows version before afx.h is included. For the code that failed, an incorrect *.h was included that included the fix PLUS objects that derived CWinApp.
As near as I can tell, the reason this worked on some sites and not others was due to a regression in the code that was built on that particular system.
The other "strange" behavior was that on the system with the bad *.h include the DLL symbol load order was different. I was able to replicate this bad behavior on more than one system running the "bad" Exe code. Then I did the same on working *.exe code. The strange thing was that the "bad" exe had reasonably correct sources in the workspace. So there were source control issues leading to this belief that it worked on one node and not others.
The runtime behavior I was able to catch matched obsolete code committed to source control.

Related

How to get wxWidgets working in Visual Studio 2012

I've got a problem for which I would very much like a solution please:
All I'm trying to do is use wxWidgets in Visual Studio 2012 (in C++), and make it accessible to my code project. That shouldn't be so hard, should it? But as usual, even though there are programming tutorials for it, wxWidgets is evidently one of those things that has no proper documentation just to simply get it running, or if it does then I sure can't find it.
I downloaded the installer for Windows and installed it. When it finished, it gave me a message that said:
wxWidgets was installed successfully!
Please note: before using wxWidgets in your application,
you must build the library using the same compiler you
use for building your program.
Please see docs/msw/install.md file in the installation
directory for the detailed instructions about how to build
wxWidgets and use it from your application.
It should be noted that I think I remember something on the website or somewhere saying that I can just include a header file into my project and forego the DLL altogether (which is what I'd prefer to do, if possible), though I may possibly be misremembering that or confusing it with something else.
Anyway, when I opened the docs/msw/install.md file, I found (amongst other things) these instructions:
--- From the IDE
Ready to use project files are provided for VC++ versions 8, 9,
10, 11, 12, 14, 15, 16 and 17 (also known as MSVS 2005, 2008, 2010, 2012,
2013, 2015, 2017, 2019 and 2022 respectively).
Simply open wx_vcN.sln (for N=8, 9, 10, 11, 12, 14, 15, 16 or 17) file,
select the appropriate configuration (Debug or Release, static or DLL)
and build the solution. Notice that when building a DLL configuration,
you may need to perform the build several times because the projects
are not always built in the correct order, and this may result in link
errors. Simply do the build again, up to 3 times, to fix this.
Of course, it doesn't even say WHERE the "wx_vcN.sln" files are, but I ran a file search and found them in C:\wxWidgets-3.1.6\build\msw and I opened the one called wx_vc11.sln, because that seems to be the one that corresponds to VS2012, right?
Anyway, it took a LONG time to load, because it had to keep scanning through thousands of files and so forth, but when it was finally finished, I did the "Rebuild All" command thrice in a row, like it says to do (which also took a while).
But I'm not sure where it even put the actual DLL file. It says that it's in C:\wxWidgets-3.1.6\build\msw....\lib\vc_lib\wxmsw31ud_xrc.lib, but where the hell is "...."? It doesn't even bother to specify the entire path! What kind of nonsense is that?! I just want the file so that I can put it into my project! Or better yet, just a .h file that takes care of everything would be great. Supposedly there's one to include called "wx/wx.h", which then includes everything else, I think, but I don't know if that one indirectly also requires a DLL.
I always have problems with DLLs, anyway, though I'll use one if I absolutely have to. You'd think it would be the easiest thing to import a DLL into a project, but for some reason, I've never been able to find any sort of tutorial that explains a specific, step-by-step way to do it which is guaranteed to work, so on the rare occasion that I actually need to use a DLL (99% of the time I can just rely on my own code and the .h files included with Visual Studio), I always end up muddling through it until it FINALLY works by sheer accident (if I'm lucky). Mostly, it just reports errors for one reason or another, that it's not properly included or compatible, or that the header files that I'm using which reference it keep showing the same errors, as though I hadn't even imported the DLL file at all! I've been programming for about 30 years, and I work on things like AI and compression and cryptographic algorithms. I don't say this to brag, but to point out that if someone as experienced as me has trouble getting a DLL to attach properly to a roject, then there's something severely wrong with the whole process, or the documentation for it! I HATE using DLLS, because it's always a HUGE chore, but I'll do it if there's absolutely no other way (though I'd appreciate step-by-step instructions, if you don't mind).
But enough about that. In any case, I have an example project in which wxWidgets is being used in conjunction with Vulkan, and though it has compilation errors (because Vulkan didn't properly install on my machine (when I try to run the sample programs it says they're missing DLLs - big surprise) and as it turns out, I don't think it's compatible with my video card, anyway (which might possibly be why it didn't install properly), so I think I'll have to resort to OpenGL instead - PLEASE tell me that's compatible with wxWidgets!), but even in that example project, it has errors where it's trying to include and use wxWidgets stuff (like "wx/wx.h"), even though that's supposed to be a complete and already working project! I can't find any DLL files or the header files in question anywhere in the project directories!
So anyway, there's got to be an easier way to get this all set up, just to the point that I can make a simple test program using wxWidgets, and compile it. I'd REALLY like to be able to get at least that much working sometime tomorrow morning, so I'd greatly appreciate any help you can give me! Thank you.
#ThisIsMe,
First of all you need to educate yourself on properly using C++.
When you acquire the C++ code it contains of 2 portion - header file and source code.
Header file is what you call a declaration you declare you classes there - class name, member variable and method.
In the source files You define what you declared and you put an actual code there.
Now with that in mind:
wxWidgets contains both header and source files. When you downloaded the installer and did the installation it just copied the "text file" (headers and sources) into the folder of you choice.
Now as you can imagine text files are not executable - you need to compile them into the binary files And that is exactly what you did. (I don't know why you did it 3 times though - I guess you got an errors after building 1st and 2nd time).
Now, you said you have a project that (either uses wxWidgets or you want to incorporate wxWidgets into - which one is it, BTW?). The answer to this question will help direct you to a proper solution.
Now, you said that you tried the Vulkan and it fails to run the demo. How did it fails? Did it produce an error? What was it? Did it even run?
Now wxWidgets does support OpenGL as can be seen by looking at the documentation and checking the different samples that comes with the library, but I'm just curious if what you are seeing from Vulkan is really an incompatibility with the Video Card.
So, we need you to answer the questions I put and so we can help you further. Just keep in mind - C++ is not for the beginner programmers and if you are struggling with understanding the difference between compilation, installation , headers and sources - it will be very hard for you.
Maybe you should start with something simpler - like wxPython and forget about C++ for now.

Glew Run Time/Linker Error

No sure why, but every time I run my OpenGL program on my home computer a message box pops up at runtime stating "The procedure entry point glewInit could not be located int he dynamic link library glew32.dll". Now I am almost certain I installed everything properly, I followed the instructions from the glew webpage and I can even get the program to run on my work computer. I've tried searching online for solutions to this error but most people with this error have it because they forgot to include the dll file in the linker dependencies (Which is not my issue.) Does anyone have any insight on how to correct or circumvent this error? My current operating system is Windows 7 Professional edition (2009).
Quick solution of last resort is to look for an other glew32.dll on your system shipped with an other application (you can easily find such an application over the web).
The problem appears when mixing headers/libs versions. It will compile, but while the older versions declares glewInit as entry point, the newer ones (1.9 for sure) declares _glewInit#0 in release configuration.
Confusing your last compilation output with an old debug exe can also produce such a situation.
Not sure what the problem was, but I redownloaded glew, I think I might have been missing a header file (not glew.h) and that forced it to blow up with a less than helpful error message.

What VS2010 C Project settings cause exes to require Compatibility Mode

I've been running and compiling a program on my Windows 7 64-bit machine for several months now, but recently I had to change several VC project settings of the static libs that it uses and now the generated executable file requires me to run it in "Windows XP Compatibility Mode".
Compiled on Windows7 64-bit machine with Visual Studio 2010 SP1
The program I am generating is being built in Win32, debug mode.
The static lib projects specify Target Machine /X86.
When I run the program from the debugger, it start up and runs, however if running via the windows icon, it requires XP compatibility mode.
When trying to start outside of the debugger the EXE shows up in task manager for a second then goes away.
I've tried using Microsoft Application Verifier on it, however I don't know what to look for in the output.
I've been unable to find any details on how to troubleshoot this issue so if anyone has any ways if finding what could be causing this recent Compatibility Mode requirement I'd love to hear how it was fixed.
I have the source/projects/solutions for the majority of the static libs that I link against, as well as the exe file generated, however some of the external dependencies I only have the .lib,.dll, and .h files for. This means I can change (most) of the project settings for the dependencies if neccessary, but I need to know which ones to look for.
Thanks
To be honest, don't be afraid to make another project and copy the code files, even if it's 5 projects. You need to cut the problem in half. If it works with the new projects then it's the project files, if not, it's the code. Making projects isn't that hard really, though I'm sure a source of much consternation and something people avoid. If its the projects you can diff the files and see what happened by process of elimination. If you are really worried, copy the entire solution to another folder; always make backups.
The problem is that you probably won't be able to hoist enough information up to us to get a meaningful answer unless get lucky, and all the answers will be shots in the dark.
So I'm goign to take this question as "this happens, what can I do about it". The strategy above will get you out of it, if this used to work before. This exercise will arm you for the future and will be more productive in the long term. Go look at UAC and manifest files, aka Vista+ difference tht dramatically changes load and run behaviour (Linker Commands, Vista Migration Guide) if you need one thing to look at, but try the above process.
----
Other generic things to try:
1) another machine
2) another install of VS
3) a simple project with one window that does nothing to prove everything else in your tool chain and environment is ok.
4) planting message boxes along the code path with different messages so you know where its crapping out.
5) turing on pdb in release and runnign outside of debugger. If craps out, then try debugging and see if still craps out, but you get to see where.
6) assume that your code is unstable and you were getting lucky when it used to work. (this one is no fun). Many times things work in debug and not in release due to mem layout being different. If your progam is large you can find creative ways to use #if's whatever to elimitate code from running while haivng the whole thing still load. You can find the code that causes the bad behaviour.
7) turn off UAC and error reporting if its on, see if changes.
8) go find the "run without debugging" menu button in Visual Studio, so you don't have to go run it with the icon. That's an accident waiting to happen, and eliminates one more environmental difference. It looks like the run with debugging button, but it's hollow, a plain green triangle. It's under debug menu set. My oppinion is that it has done more harm than good to not have that on the bar by default as its confused many many people to think launching wiht VS means always using the debugger.
and so on....

D3DX11EffectsD.lib not showing up after build (vs2010)

I am starting to learn DX11 and running into trouble with the effects framework. I know it was released as source and I have to build it, but the output from the build is not what I expected.
According to the research I've done on this question, the output from the build should be
D3DX11EffectsD.lib for debug
D3DX11Effects.lib for release
However, when I look into the 'Effects11\Debug' directory after building the project, I only see a file Effects11.lib (well, an Effects11 Object Library file which I assume is a .lib, I'm new to c++), and the exact same file in 'Effects11\Release'. Whats going on here? I've never used VS 2010 for c++ before now but I think I am building the solution correctly.
Is this a matter of renaming the files or have I done something wrong without realizing it? There really isn't much documentation on building and linking libraries in vs 2010 that I could find. Anybody have any suggestions?
Thanks
If you compiled exactly what you got off the web, then I think it would be just a naming convention problem.
You should try compiling it into your end application and see if it yells about debugging symbols missing.
You can also go into the build settings (it has been a while since I have used visual studio for anything other than C# so I don't know exactly where that menu option would be (I assume right clicking on the project should yield some useful results)...I generally do my C++ stuff on linux) and check to see what the built targets are for debug and release. If it turns out that the names are the same for both, but the build targets (i.e. the folder and a few other options, like including debugging symbols) are different then you should be good and it is just a naming problem.
Also, if the files are the exact same size it is likely that they are the same since the debug file should be at least a bit larger than the release one.
If it turns out that they are the same file, try re-downloading or re-extracting the source and just compiling the project again without any changes and see if that gets any results.

Linking error in vc++

I am very new to VC++ and I am running the program on VC++ for the first time.
I strictly followed the instructions given in Microsoft Programming Visual C++ book and created one project as instructions given.
About the ex03a.exe:
I saw in the path "...\Ex03a\Debug\" and in that no file exists such as ex03a.exe.
I tested my vc++ by executing a simple 'Test.cpp' file. I was able to run the simple c++ program and I got the output. And Test.exe is there in '\Test\Debug\Test.exe'
My Question:
How could I get rid off the error.
Almost always when VS says it isn't able to open a file, it's about opening it for writing.
And almost always this doesn't work because the file is locked.
And almost always this is because the file is an executable that is currently running :-)
This is a specialty of Windows - an exe is not simply loaded, it's locked for all of it's run time. This is probably due to the fact that exe files (actually called portable executables, for whatever reason) contain not only code, but usually also an arbitrary number of resources (like images, etc.), and changing the file on the fly would make the aplication crash hard when it attempts to read one of those resources at run time.
Therefore, I suggest looking for a way to exit / close / terminate the application, so it isn't running any longer, so the file is not locked anymore, so in this case the linker can do its work.
The error message, btw., isn't that intuitive from my point of view - this problem being SO standard, it could at least attempt to tell you anything about this possible source of the problem - afaik, this hasn't been improved until now, probably because most developers have seen this before, found out why it happened, and therefore do not have any more problems with it.
I see that in that screenshot, that you are running multiple versions of VC6.
Now You get that error if you run the newly compiled exe of that program without shutting down the previous compiled exe.
VC tries to overrwrite the exe that is currenty running but encounters that exact error.
Always close the program when you are done.