Re-execute a program after machine exception - c++

I have a pre-compiled exe (native C++11) which crashes (access violation error) at some point in iterative process. I can not afford debugging it and re-compile it again for now.
I thought of a dirty solution. I will make another program that is responsible for executing that exe and when it stopped working, I simply re-execute it again.
Is it possible? How can I know that the program was stopped?
Note: I am on Windows and doing development using MSVS.

I have found a solution with help of #Richard Hodges.
Make a new program with the this code:
#include <Windows.h>
#include <string>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int main(int argc, const char**argv) {
while (true) {
TCHAR ProcessName[256];
STARTUPINFO si;
PROCESS_INFORMATION pi;
wcscpy(ProcessName, L"FaultyProgram.exe");
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
ProcessName, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
return 0;
}
And the most important part is to disable UI Error message when a program crash by changing this value in the registry:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Windows Error Reporting
"DontShowUI"=dword:00000001
instead of:
"DontShowUI"=dword:00000000

Related

Creating "dir" command using CreateProcess function failed with error code 2

I was just playing with Win32-API and wanted to create a process using CreateProcess function. I used the following code from MSDN website:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
But surprisingly I can't create a dir process using this piece of code. Error code indicated that 'The system cannot find the file specified.'
I'm using Visual studio 2015 and Windows 7 64Bit. But when I run the same executable in Windows 10, everything is OK.
dir is not an external command that can be run. It's a command internal to the Windows Command Prompt. You'll need to call your program as myprogram "cmd /c dir" to do that.
Of course, there are better ways to iterate a directory than calling an external program, but that's a separate question.
After hours of going through 1500 lines of C code, it finally dawned on me what my problem was and why it worked on one Windows 10 system of mine but not on another. The system that it worked on, I really did have a DIR.EXE. But is was not the COMSPEC DIR that was running. I had DIR.EXEs in Git and MinGW folders.
Read this for how to correctly use CREATEPROCESS.
https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa

CreateProcess function issue

I'm trying to start an process from my code with createprocess function. The command line is ok, and the other exe, i can start it without problems from visual studio.
When I am trying to start it from the other process with createprocess it gives me - Runtime error this app has requested the runtime to terminate it in an unusual way.
What could be the problem ? How could I remove this problem ?
STARTUPINFO si;
PROCESS_INFORMATION pi;
bool bResult;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Cast System::String* __gc to char*
char* chAppName = (char*)(void*)Marshal::StringToHGlobalAnsi(AppName);
char* chCmdLine = (char*)(void*)Marshal::StringToHGlobalAnsi(CmdLine);
//Start the child process.
bResult = CreateProcess( chAppName, // No module name (use command line)
chCmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi); // Pointer to PROCESS_INFORMATION structure
Try eliminating the StringToHGlobalAnsi calls and hard coding them instead first. If that stops the problems, we can then sort out the StringToHGlobalAnsi calls.

Launch console app from console apps

I am using this piece of code to launch a process from a GUI app. but, according to the notes of this code it is not possible to launch a process from a console app. actually i want to do that, I want a console app to launch another console process, please do you have any idea how to do that?
// This technique must be used for "console-less" parents such as GUI
// applications or detached applications.
// Using the STARTUPINFO STARTF_USESTDHANDLES flag, requires that
// the CreateProcess fInheritHandles parameter be set TRUE so that
// the file handles specified in the STARTUPINFO structure will be
// inherited by the child.
// setup the child process's handles for stdin, stdout, & stderr.
STARTUPINFO childProcStartupInfo;
memset( &childProcStartupInfo, 0, sizeof(childProcStartupInfo));
childProcStartupInfo.cb = sizeof(childProcStartupInfo);
childProcStartupInfo.hStdInput = hFromParent; // stdin
childProcStartupInfo.hStdOutput = hToParent; // stdout
childProcStartupInfo.hStdError = hToParentDup; // stderr
childProcStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
childProcStartupInfo.wShowWindow = SW_HIDE;
// Now create the child process, inheriting handles
PROCESS_INFORMATION childProcInfo; /* for CreateProcess call */
bOk = CreateProcess(
NULL, // filename
pCmdLine, // full command line for child
NULL, // process security descriptor */
NULL, // thread security descriptor */
TRUE, // inherit handles? Also use if STARTF_USESTDHANDLES */
0, // creation flags */
NULL, // inherited environment address */
NULL, // startup dir; NULL = start in current */
&childProcStartupInfo, // pointer to startup info (input) */
&childProcInfo); // pointer to process info (output) */
did you try shellexecute? I think that works..
You can try:
ShellExecute(), ShellExecuteEx(), CreateProcess(), system(), _wsystem().
There are a few more, but one of these got to work for you!
Personally, I would go with CreateProcess and than wait for the process to quit (found on google this example: http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result). Notice that system()/_wsystem() are the easiest to use, but if you're not careful they can be exploited !!!
Hope it helps! :-)
Try this code:
#include <Windows.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* app_to_launch=new char[80];
strcpy(app_to_launch,"app.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
app_to_launch, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}

Unhandled Error with CreateProcess [duplicate]

This question already has an answer here:
CreateProcess method ends up with an error
(1 answer)
Closed 6 years ago.
I was reading about CreateProcess function in c++ and I wanted to try it. Basic idea of the code is to have my main execute another process (notepad). Really, it’s just the basic code. When I run the program, I get:
First-chance exception at 0x752bb763 in createprocess.exe: 0xC0000005: Access violation writing location 0x00be57b8.
Unhandled exception at 0x752bb763 in createprocess.exe: 0xC0000005: Access violation writing location 0x00be57b8.
When I make a break point for where the error occurs, I get taken to tidtable.c (which is for accessing threads, I guess).
Specifically in tidtable.c at CRTIMP PFLS_GETVALUE_FUNCTION __cdecl __set_flsgetvalue()
I really don’t know what or how to avoid this problem. The error occurs with the CreateProcess call (ie, it never outputs the “out of create”).
My code is:
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
#include <conio.h>
int main(VOID)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
//allocate memory
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
fprintf(stderr, "This is just a test");
//create child process
if (!CreateProcess(NULL,
L"C:\\Windows\\Notepad.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "create process failed");
return -1;
}
fprintf(stderr, "out of create");
//parent waits for child to complete
WaitForSingleObject(pi.hProcess, INFINITE);
fprintf(stderr, "after wait");
printf("Child Complete");
//close handle
CloseHandle(pi.hProcess);
// CloseHandle(pi.hthread);
}
If anyone knows how to overcome this problem, your help would be appreciated.
The problem is that the second parameter of the CreateProcess function is an in/out parameter.
If you specify it as a string like you did, it is a constant string and the function when it is called cannot write to the memory location, thus you have a memory access violation. The correct way is to call your function like this:
LPTSTR szCmdline = _tcsdup(TEXT("C:\\Windows\\Notepad.exe"));
//create child process
if (!CreateProcess(NULL,
szCmdline,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi))
{
fprintf(stderr, "create process failed");
return -1;
}
You may also want to read this blog article.
The 2nd arg to CreateProcess cannot be const or a literal string because the func attempts to modify the string. Copy the literal to a local array and then pass that as the 2nd arg.

Launching an application from a thread with C++

I need to launch a 3rd party program inside a thread, wait to get the results both from stdout/stderr with C++.
What methods are available?
Are they cross-platform? I mean, can I use them both for cl/gcc?
On Unix:
http://linux.die.net/man/3/execl
#include <sys/types.h>
#include <unistd.h>
void run_process (const char* path){
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0){
/* This is the parent process. */
int ret = waitpid(child_pid, NULL, 0);
if (ret == -1){
printf ("an error occurred in waitpid\n");
abort ();
}
}
else {
execl (path, path);
/* The execvp function returns only if an error occurs. */
printf ("an error occurred in execl\n");
abort ();
}
}
On Windows:
http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx
# include <windows.h>
void run_process (const char* path){
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
bool ret = = CreateProcess(
NULL, // No module name (use command line)
path, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
false, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
)
if (!ret){
printf("Error");
abort();
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
system should be platform independant, but you might want to stick with createprocess (win)/ exec (others) if there is a concern about running the program with the same security privledges.
There is a set of posix functions to launch an external executable - see exec - which are cross platform. To do some specific tasks on windows you may need to use windows specific createprocess.
These generally block so you would have to start them in a new thread. Threading is generally not cross platform, although you can use posix (pthreads) on windows.
An alternative is to use somthing like Qt or wxWidgets cross platform libraries.