AFX_MANAGE_STATE(AfxGetStaticModuleState()) in DLL causes EXE to not exit? - c++

I have a project that consists of a DLL and a windows console application .exe.
The .exe calls the DLL. In the DLL I am creating a Dialog box.
AFX_MANAGE_STATE(AfxGetStaticModuleState());
MyDlg* dlg = new MyDlg;
dlg->Create(IDD_DIALOG1);
I had to add AFX_MANAGE_STATE(AfxGetStaticModuleState()) so it knew to look in the DLL for the resources. However this seems to be causing a problem when control is returned to the EXE.
When I "return 0" in the main() function of the EXE the console window does not go away. I have to physically close it in order for the EXE to stop running. If I remove AFX_MANAGE_STATE(AfxGetStaticModuleState()) from the DLL (along with all references to MyDlg since they won't work without it) everything works fine when control is passed back to the EXE.
Why would this be happening?

Fixed. My DLL is using DAO. I had to add AfxDaoTerm(); before exiting. Thank you

Related

Wish to terminate main.exe after execute DLL (C++)

I run my main.exe on a mobile device which executes my test.dll. Now after the dll has startet I need to wm_destroy the main.exe for exchange while the dll is still running. I'm pretty new to this field and I see it's against all logic. But maybe there is a way to change the main.exe while dll is running.
Thx in advance!
When a DLL is loaded it's linked into the address space of the main executable. It doesn't and isn't supposed to exist independently. DLLs don't "run"/"execute" independently.
What you want to do is not what DLLs for. You probably want to spawn a new, independent process, and from there send the WM_DESTROY to the parent process.
What purpose this can even serve? If dll is running by itself, it's an .exe and should have own WinMain, not just DllMain. Actually they have same format, both .dll and .exe are PEXE files and can have exports. It's a bit tricky to get address of exported function from running module that isn't child process, but it's how certain profiler tools work, including VS analyzer.
In Windows there is utility which sole purpose is to run a dll, rundll32, which you can spawn as child process, but it requires elevated rights.

How to debug an external process under C++ Builder?

I have a DLL that is written in C++ Builder. The DLL is built with "debug info" on.
I cannot run and debug this DLL from the IDE because it is loaded dynamically as a plugin in another process (main exe). My DLL needs to be copied into the main exe's folder first.
So, I attached the IDE to that process but I don't know what to do from here.
I know that if I click a button in the main process to load and use my DLL an AV is raised and a custom message is shown with some minor into about the error. The debugger won't step in when the AV is raised. Probably the error is caught at a higher level.
It would be nice if I could also attach the source-code of my DLL and put a breakpoint in it.
How do I get more info about that AV (its origins)?
(Basically any hint from those that did this type of debugging would be very helpful).
I have a DLL that is written in C++ Builder. The DLL is built with "debug info" on.
I cannot run and debug this DLL from the IDE because it is loaded as a plugin in another process.
Yes, you can.
Open the DLL project in the IDE, go into the project's Run parameters and set the desired EXE as the project's Host. This way, when you "run" the DLL project for debugging, the IDE will execute the Host instead and attach the debugger to that process. When the Host process loads your DLL into memory, you can then step through and debug the DLL's code as needed.
If the Host process is already running before you start your debugging, you can simply Attach the debugger to the Host process manually before it loads your DLL, and then the debugger will still be able to step through the DLL's code once the Host loads the DLL into memory.

Cefsharp CLR initialization crash in MFC project

I created a CLR class as DLL and loaded cefsharp,cefsharp winforms,cef core through Nuget package in vs 2015.When I run it in a sample MDI CFormView application it works.But when I try to run it in my main MDI application which loads COM DLL it crashes without any errors.The code never executes and crashes even before initialisation.I am using it like below
using namespace cefsharp;
using namespace cefsharp::winforms:
Cef:: Initialize (gcnew CefSettings());
The debugger never comes to this place and application crashes.I am really unable to debug since it doesn't even hit initinstance if I write that line but if I don't write that line the app runs.
The problem somehow resolved when I created a CLR inside my project and added the needed dependencies for it.Now I am able to load my MDI applications with different views and also Initialize the COM dll without encountering any crash.

Getting console output of Windows GUI application

I'm running a exe file that has cprintf calls in one of its DLLs, the exe is GUI application, I'm looking for a method to get the output of the cprintf calls.
Thanks.
This was solved by loading DLL on runtime that called AllocConsole() in DLLMain.

MFC DLL calls AfxWinInit and crashes application

I have an MFC .exe application, and I created another project for a DLL with MFC dynamically linked to it.
Now, when I import that DLL through LoadLibrary, it crashes my application, because when the importing is done, the DLL calls AfxWinInit().
Should the DLL call AfxWinInit()? How do I avoid it? Or is something else wrong?
In your MFC application WinMain() calls AfxWinMain(). The AfxWinInit() is called at the beginning of AfxWinMain(). So the initialization is done by framework for you. There is no need to initialize it again.
MFC DLLs provide their own entry point, so you're not supposed to write one yourself. If you plan to write a DLL with MFC support, I'd suggest you start with a fresh MFC DLL created by the app wizard and then move your code there.
For MFC applications that load extension DLLs, use AfxLoadLibrary instead of LoadLibrary.