Handle firefox error message “Cannot load XPCOM” using C++ - c++

I'm trying to open Mozilla Firefox using CreateProcess(). However, If Firefox is auto updating while I try to open it, I get the following error message:
Cannot load XPCOM
And I need to restart the application.
Here is the code I'm using:
path = MozillaExePath.c_str();
STARTUPINFO info = { sizeof(STARTUPINFO), NULL, NULL, "FireFox", 0,0,800, 600, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL };
PROCESS_INFORMATION processInfo;
if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
WaitForSingleObject(processInfo.hProcess, 3000);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
else
{
WriteLogFile("May be error with mozilla firefox...\n");
exit(1);
}
So, how can I handle that error message using C++?

Here is a different way to do this that is working for me:
#include <shellapi.h>
[...]
if (ShellExecute(NULL, TEXT("open"), TEXT("firefox.exe"), NULL, NULL, 0) <= HINSTANCE(32))
{
WriteLogFile("Could not open Mozilla Firefox...\n");
}
Reference:
https://learn.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shellexecutea

Related

CreateProcessA not working or returning any errors

I've tried to used both CreateProcessA and CreateProcess to create a instance of notepad, but to no success. CreateProcess always returns an error code of 2 when I run it, but CreateProccessA doesn't return anything at all.
This is what I have so far:
STARTUPINFOA startInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
ZeroMemory(&processInfo, sizeof(processInfo));
if (CreateProcessA(NULL, NULL,NULL,NULL,FALSE,NULL, NULL, "C:\\Windows\\notepad.exe", &startInfo, &processInfo)) {
DWORD Error = GetLastError();
MessageBoxA(NULL, "FAILED", "FAILED", MB_OK);
printf("%d", Error);
return 1;
}
Error 2 is ERROR_FILE_NOT_FOUND. You are passing the path to notepad.exe in the lpCurrentDirectory parameter, but it needs to be passed in the lpApplicationName or lpCommandLine parameter instead:
CreateProcessA("C:\\Windows\\notepad.exe", NULL, NULL, NULL, FALSE, NULL, NULL, NULL, &startInfo, &processInfo)
CreateProcessA(NULL, "C:\\Windows\\notepad.exe", NULL, NULL, FALSE, NULL, NULL, NULL, &startInfo, &processInfo)
Also, you are calling GetLastError() when CreateProcessA() is successful. You need to call it when CreateProcessA() fails instead:
if (!CreateProcessA(...)) { // <-- note the !
DWORD Error = GetLastError();
...
}
Lastly, the %d specifier of printf() expects an int, not a DWORD. Use %ul instead, which expects an unsigned long, which is what DWORD is defined as:
printf("%ul", Error);

Start CMD in CMD with CreateProcessWithTokenW

I have a console application which calls the CreateProcessWithTokenW() WinAPI function to create a new process which starts a cmd console. By calling it, it starts a new CMD Window. I want to spawn another cmd within the calling cmd window (not in a new window).
So I want to simulate the same behavior like if you start cmd and type "cmd".
ret = CreateProcessWithTokenW(pNewToken, 0, L"C:\\Windows\\System32\\cmd.exe", NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
Here is a minimal reproducible code snippet.
I added CreateProcess instead of CreateProcessWithToken....if i define 0 for 5th argument (dwCreationFlag) than it starts the CMD in the Powershell. But for CreateProcessWithToken the behavior is not the same.
Run this code with a elevated powershell (because it needs Se_Debug_Priv)
#include <stdio.h>
#include <Windows.h>
#include <WinBase.h>
#include <iostream>
#include <tchar.h>
int main() {
//DEFINE HERE PID OF winlogon.exe
DWORD pid = 940;
HANDLE currentProcess = {};
HANDLE AccessToken = {};
currentProcess = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
OpenProcessToken(currentProcess, TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY, &AccessToken);
HANDLE pToken = AccessToken;
SECURITY_IMPERSONATION_LEVEL seImpersonateLevel = SecurityImpersonation;
TOKEN_TYPE tokenType = TokenPrimary;
HANDLE pNewToken = new HANDLE;
DuplicateTokenEx(pToken, MAXIMUM_ALLOWED, NULL, seImpersonateLevel, tokenType, &pNewToken);
STARTUPINFO si = {};
PROCESS_INFORMATION pi = {};
//TEST1
//Creates a new window for both functions so the 5th seems to be ignored
CreateProcessWithTokenW(pNewToken, 0, L"C:\\Windows\\System32\\cmd.exe", NULL, 0, NULL, NULL, &si, &pi);
CreateProcessWithTokenW(pNewToken, 0, L"cmds.bat", NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
//TEST2
//Create a new windows, assumed behavior
CreateProcessW(L"C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
//Creates also a new window, NOT assumed behavior
CreateProcessW(L"C:\\Windows\\System32\\cmd.exe", NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
return 0;
}
Get rid of the CREATE_NEW_CONSOLE flag:
CREATE_NEW_CONSOLE
0x00000010
The new process has a new console, instead of inheriting the parent's console. This flag cannot be used with the DETACHED_PROCESS flag.
This flag is enabled by default.
That flash is what is forcing a new CMD window to be created. Without that, the new process will be created in the existing CMD window of the calling process.
As far as I'm concerned, you should use CREATE_NEW_CONSOLE. According to the code:
ret = CreateProcessWithTokenW(pNewToken, 0, L"C:\\Windows\\System32\\cmd.exe", NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
The problem is not with the use of CreateProcessWithTokenW () winapi. Could you please provide us with a minimal reproducible exampleto reproduce the issue.
Here is the code:
STARTUPINFOEX startup_info = {};
PROCESS_INFORMATION process_info = {};
BOOL CreateProcTokenRes = FALSE;
CreateProcTokenRes = CreateProcessWithTokenW(NewToken, 0, L"C:\\Windows\\system32\\cmd.exe", NULL, CREATE_NEW_CONSOLE, NULL, NULL, &startup_info, &process_info);
if (!CreateProcTokenRes)
{
_tprintf(L"Cannot Create Process With Token. Failed with Error Code: %d\n", GetLastError());
CloseHandle(NewToken);
For more details I suggest you could refer to the link:https://niiconsulting.com/checkmate/2019/11/token-manipulation-attacks-part-2-process-of-impersonation/

Parent process hangs when calling CreateProcess() in MFC

My code is,
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
info.dwFlags = STARTF_USESHOWWINDOW;
info.wShowWindow = TRUE;
if (CreateProcess("My_program.exe", command, NULL, NULL, TRUE,
CREATE_NEW_CONSOLE, NULL, NULL, &info, &processInfo))
{
WaitForSingleObject(processInfo.hProcess, INFINITE);
GetExitCodeProcess(processInfo.hProcess, &exit_code);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
When i call this function CreateProcess(), My_program.exe invokes and runs.
But my MFC Diolog box gets hang and it shows not responding. Can anyone please help me to avoid this.

Unable to redirect standard output using CreateProcess

I'm maintaining an MFC program and I'm launching a simple Win32 console program (just a "Hello World" program, source below) with CreateProcess and I'm unable to redirect the standard output of that program to a file.
This is the launching code, don't bother about the Fatal function, it just
displays a message and aborts the program, this is only test code.
HANDLE hfile = CreateFile("output.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
Fatal("Fatal error: CreateFile");
}
static const char TestText[] = "Test\r\n";
DWORD written;
if (!WriteFile(hfile, "Test\r\n", sizeof(TestText) - 1, &written, NULL))
{
Fatal("Fatal error: CreateProcess");
}
STARTUPINFO startupinfo = {0};
startupinfo.cb = sizeof(STARTUPINFO);
startupinfo.lpTitle = "Some Title";
startupinfo.dwFlags = STARTF_USESTDHANDLES;
startupinfo.hStdOutput = hfile;
PROCESS_INFORMATION processInfo;
if (!CreateProcess("S:\\Git\\TestRedirect\\TestConsole1\\Debug\\TestConsole1.exe", "cmdline", NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupinfo, &processInfo))
{
Fatal("Fatal error: CreateProcess");
}
if (WaitForSingleObject(processInfo.hProcess, 10000) != WAIT_OBJECT_0)
{
Fatal("Fatal error: WaitForSingleObject");
}
if (!CloseHandle(hfile))
{
Fatal("Fatal error: CloseHandle");
}
It almost works as expected:
it opens "output.txt"
it writes "Test\r\n" into "output.txt"
it launches TestConsole1.exe
the console window of TestConsole1.exe does not display "Hello Word", which is expected because the standard output is supposed to be redirected into "output.txt"
WaitForSingleObject waits for TestConsole1.exe to be completed
CloseHandle closes "output.txt"
Now I expect "output.txt" to contain this:
Test
Hello World!
but actually it's content is
Test
Source code of TestConsole1.exe:
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
printf("Hello World!\n");
Sleep(2000); // wait 2 seconds so I can see what happens
return 0;
}
your hfile is not inheritable - you need use SECURITY_ATTRIBUTES in call CreateFile
SECURITY_ATTRIBUTES sa = { sizeof(sa), 0, TRUE };
HANDLE hfile = CreateFile("output.txt", GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

How to hide Matlab Command Window and command prompt from calling system in C++

I execute the Matlab script file using system() which uses the command prompt and it's working fine. But i wish to hide everything and hope it runs in the background and only showing my GUI from the script file. Any idea?
This is my command in MSVS C++ (Note : i cut short the path name for simplicity purposes) :
system("\"\"C:\\matlab.exe\" -nodisplay -nosplash -nodesktop -r \"run('C:\\main.m');\"\"");
You could try CreateProcess instead of system. A simple example:
#include <windows.h>
#include <stdio.h>
int main() {
PROCESS_INFORMATION pi;
STARTUPINFO si = {
sizeof(si),
NULL, NULL, NULL,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
NULL, NULL, NULL, NULL
};
BOOL res = CreateProcess(
NULL,
"C:\\matlab.exe -nodisplay -nosplash -nodesktop -r \"run('C:\\main.m');\"",
NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL,
NULL, // starting directory (NULL means same dir as parent)
&si, &pi
);
if (res == FALSE) printf("CreateProcess failed\n");
return 0;
}
You might be better off using the MATLAB Engine API.