I've read several questions here on Stack Overflow, Microsoft docs, and cplusplus.com. Some claim that exit()
terminates the program normally, just as a return 0;
would from main. Others claim that exit()
doesn't call all the destructors, etc. So I was wondering if someone could help.
edit: As someone asked, I added some blocks of code for which I would like to terminate the program. I use C++20
HKEY newKey;
RegOpenKey(HKEY_CURRENT_USER, R"(Software\Microsoft\Windows\CurrentVersion\Run)", &newKey);
LONG result = RegSetValueEx(newKey, value, 0, REG_SZ, (LPBYTE)filePath, lstrlen(filePath));
RegCloseKey(newKey);
if(result != ERROR_SUCCESS){
MessageBox(nullptr, "Could not add to startup", "Error", MB_ICONERROR);
exit(1);
}
int i = line.find(':');
if(i == std::string::npos){
MessageBox(nullptr, "File is incorrectly formatted", "Error", MB_ICONERROR);
exit(1);
}
info.open(infoPath);
if(info.fail()){
MessageBox(nullptr, "info.txt did not open", "Error", MB_ICONERROR);
exit(1);
}
I link the posts I've read about this:
https://cplusplus.com/forum/beginner/4589/
How to exit program execution in C++?
https://learn.microsoft.com/en-us/cpp/cpp/program-termination?view=msvc-170
https://cplusplus.com/reference/cstdlib/exit/
Thanks in advance