ReactOS  0.4.15-dev-1632-g4e289ce
main.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Applications Manager Command-Line Launcher (rapps.com)
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Allow explorer / cmd to wait for rapps.exe when passing commandline arguments
5  * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include <windef.h>
9 #include <winbase.h>
10 #include <strsafe.h>
11 
13 {
14  DWORD dwExit;
15  STARTUPINFOW si = { sizeof(si) };
16  PROCESS_INFORMATION pi = { 0 };
17 
18  SetLastError(0);
19  if (!CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
20  {
21  fprintf(stderr, "Unable to create rapps.exe process...\n");
22  return -1;
23  }
24  CloseHandle(pi.hThread);
25  WaitForSingleObject(pi.hProcess, INFINITE);
26  GetExitCodeProcess(pi.hProcess, &dwExit);
27  CloseHandle(pi.hProcess);
28  return (int)dwExit;
29 }
30 
31 int wmain(int argc, wchar_t* argv[])
32 {
33  int iRet;
34  int n;
35  size_t arglen;
36  WCHAR RappsExe[MAX_PATH] = { 0 };
37  wchar_t* cmdline;
38 
39  GetModuleFileNameW(NULL, RappsExe, ARRAYSIZE(RappsExe));
40  arglen = wcslen(RappsExe);
41  if (arglen > 4 && !wcsicmp(RappsExe + arglen - 4, L".com"))
42  {
43  wcscpy(RappsExe + arglen - 4, L".exe");
44  }
45  else
46  {
47  fprintf(stderr, "Unable to build rapps.exe path...\n");
48  return -1;
49  }
50 
51  arglen += (1 + 2); // NULL terminator + 2 quotes
52 
53  for (n = 1; n < argc; ++n)
54  {
55  arglen += wcslen(argv[n]);
56  arglen += 3; // Surrounding quotes + space
57  }
58 
59  cmdline = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, arglen * sizeof(WCHAR));
60  if (cmdline)
61  {
62  wchar_t* ptr = cmdline;
63  size_t cchRemaining = arglen;
64 
65  StringCchPrintfExW(ptr, cchRemaining, &ptr, &cchRemaining, 0, L"\"%s\"", RappsExe);
66 
67  for (n = 1; n < argc; ++n)
68  {
69  StringCchPrintfExW(ptr, cchRemaining, &ptr, &cchRemaining, 0, L" \"%s\"", argv[n]);
70  }
71  }
72 
73  iRet = run_rapps(cmdline);
74  if (cmdline)
76  return iRet;
77 }
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:600
static int argc
Definition: ServiceArgs.c:12
#define CloseHandle
Definition: compat.h:598
BOOL WINAPI GetExitCodeProcess(IN HANDLE hProcess, IN LPDWORD lpExitCode)
Definition: proc.c:1168
GLdouble n
Definition: glext.h:7729
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
TCHAR * cmdline
Definition: stretchblt.cpp:32
#define argv
Definition: mplay32.c:18
int wmain(int argc, WCHAR *argv[])
Definition: main.c:269
STRSAFEAPI StringCchPrintfExW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPWSTR *ppszDestEnd, size_t *pcchRemaining, STRSAFE_DWORD dwFlags, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:585
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define FALSE
Definition: types.h:117
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
static PVOID ptr
Definition: dispmode.c:27
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
Definition: proc.c:4594
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
static refpint_t pi[]
Definition: server.c:96
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
#define wcsicmp
Definition: compat.h:15
int run_rapps(LPWSTR cmdline)
Definition: main.c:12
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
FILE * stderr
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define INFINITE
Definition: serial.h:102
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:594